gpt4 book ai didi

ios - 导出视频总是失败

转载 作者:行者123 更新时间:2023-11-30 13:52:32 24 4
gpt4 key购买 nike

我正在尝试将视频与录制的声音复合。所以我编写了这样的代码

func saveMediaCompaundWithMovie(videoAsset: AVAsset) {
let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sample", ofType: "mov")!)
let videoAsset = AVAsset(URL: url)

let mutableComposition = AVMutableComposition()
let videoCompositionTrack = mutableComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
let videoAssetTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first
let range = CMTimeRange(start: kCMTimeZero, duration: videoAssetTrack!.timeRange.duration)
do { try videoCompositionTrack.insertTimeRange(range, ofTrack: videoAssetTrack!, atTime: kCMTimeZero) } catch {}
let videoSize = videoAssetTrack?.naturalSize

let audioCompositionTrack = mutableComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
let audioAsset = AVAsset(URL: self.recorder.url) //MARK: self.recorder.url is voice data.And I checked this data is correctly played.
let audioAssetTrack = audioAsset.tracksWithMediaType(AVMediaTypeAudio).first
let maxDuration = audioAssetTrack!.timeRange.duration
do { try audioCompositionTrack.insertTimeRange(range, ofTrack: audioAssetTrack!, atTime: kCMTimeZero) } catch {}

let exporter = AVAssetExportSession(asset: mutableComposition, presetName: AVAssetExportPresetHighestQuality)
let documents = NSFileManager.defaultManager().URLsForDirectory(.DocumentationDirectory, inDomains: .UserDomainMask)[0].path
let fileName = NSURL(string: documents!)?.URLByAppendingPathComponent("compound").URLByAppendingPathExtension("mov")
exporter?.outputURL = fileName

let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRange(start: kCMTimeZero, duration: maxDuration)
let videoComp = AVMutableVideoComposition()
videoComp.renderSize = videoSize!
videoComp.frameDuration = CMTimeMake(1, 30)
videoComp.instructions = [instruction]

exporter?.outputFileType = AVFileTypeQuickTimeMovie
exporter?.shouldOptimizeForNetworkUse = true
exporter?.videoComposition = videoComp
exporter?.timeRange = range
exporter?.exportAsynchronouslyWithCompletionHandler({ () -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
switch (exporter!.status) {
case AVAssetExportSessionStatus.Completed:
print("suceed to save")
break
case AVAssetExportSessionStatus.Cancelled:
print("canceled")
break
case AVAssetExportSessionStatus.Failed:
print("failed")
print(exporter?.error?.localizedDescription)
print(exporter?.error?.userInfo.description)
print(exporter?.error?.localizedFailureReason)
print(exporter?.error?.localizedRecoverySuggestion)
break
default:
break;
}
})
})

}

然后 AVAssetExportSessionStatus 总是失败。控制台日志是这样的

failed
Optional("Operation Stopped")
Optional("[NSLocalizedDescription: Operation Stopped, NSLocalizedFailureReason: The video could not be composed.]")
Optional("The video could not be composed.")

有人能注意到错误的部分吗?我希望你能帮忙。谢谢。

最佳答案

尝试替换此行

let fileName = NSURL(string: documents!)?.URLByAppendingPathComponent("compound").URLByAppendingPathExtension("mov")

有了这个

let fileName = URL(fileURLWithPath: (documents?.appending("/compound.MOV"))!)

我也遇到了同样的问题。以这种方式设置输出 URL 就可以正常工作了。但不确定根本原因。

关于ios - 导出视频总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34087668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com