我们为用户录制的视频添加字幕,但我们的AVAssetExportSession对象的导出失败是非确定性的:有时它会起作用,有时它不会。目前还不清楚如何重现错误。
我们注意到出口期间资产跟踪似乎丢失了。
在导出之前,有两个轨道(一个用于音频,一个用于视频),如预期的那样。但是,在exportDidFinish
中检查同一文件网址的曲目数量会显示0个曲目。所以出口过程似乎有些问题。
更新:注释掉exporter.videoComposition = mutableComposition
可修复错误,但当然不会对视频应用任何转换。所以问题似乎在于创建AVMutableVideoComposition
,这会在导出期间导致下游问题。关于AVMutableVideoComposition
的文档和教程很少,所以即使您没有解决方案但可以推荐Apple以外的参考资料,这将有所帮助。
错误:
错误域= AVFoundationErrorDomain代码= -11841"操作已停止" UserInfo = 0x170676e80 {NSLocalizedDescription =操作已停止, NSLocalizedFailureReason =视频无法合成。}
代码:
let videoAsset = AVURLAsset(URL: fileUrl, options: nil)
let mixComposition = AVMutableComposition()
let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
let audioTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID(kCMPersistentTrackID_Invalid))
let sourceVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo)[0] as! AVAssetTrack
let sourceAudioTrack = videoAsset.tracksWithMediaType(AVMediaTypeAudio)[0] as! AVAssetTrack
videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceVideoTrack, atTime: kCMTimeZero, error: nil)
audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, videoAsset.duration), ofTrack: sourceAudioTrack, atTime: kCMTimeZero, error: nil)
// Create something mutable???
// -- Create instruction
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: sourceVideoTrack)
instruction.layerInstructions = [videoLayerInstruction]
let mutableComposition = AVMutableVideoComposition()
//mutableComposition.renderSize = videoTrack.naturalSize
mutableComposition.renderSize = CGSize(width: 320, height: 320)
mutableComposition.frameDuration = CMTimeMake(1, 60)
mutableComposition.instructions = [instruction]
// Animate
mutableComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, inLayer: parentLayer)
// -- Get path
let fileName = "/editedVideo-\(arc4random() % 10000).mp4"
let allPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsPath = allPaths[0] as! NSString
let exportPath = docsPath.stringByAppendingFormat(fileName)
let exportUrl = NSURL.fileURLWithPath(exportPath as String)!
println("Tracks before export: \(mixComposition.tracks.count). File URL: \(exportUrl)")
// -- Remove old video?
if NSFileManager.defaultManager().fileExistsAtPath(exportPath as String) {
println("Deleting existing file\n")
NSFileManager.defaultManager().removeItemAtPath(exportPath as String, error: nil)
}
// -- Create exporter
let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
exporter.videoComposition = mutableComposition
exporter.outputFileType = AVFileTypeMPEG4
exporter.outputURL = exportUrl
exporter.shouldOptimizeForNetworkUse = true
// -- Export video
exporter.exportAsynchronouslyWithCompletionHandler({
self.exportDidFinish(exporter)
})
func exportDidFinish(exporter: AVAssetExportSession) {
println("Exported video with status: \(getExportStatus(exporter))")
// Save video to photo album
let assetLibrary = ALAssetsLibrary()
assetLibrary.writeVideoAtPathToSavedPhotosAlbum(exporter.outputURL, completionBlock: {(url: NSURL!, error: NSError!) in
println("Saved video to album \(exporter.outputURL)")
if (error != nil) {
println("Error saving video")
}
})
// Check asset tracks
let asset = AVAsset.assetWithURL(exporter.outputURL) as? AVAsset
println("Tracks after export: \(asset!.tracks.count). File URL: \(exporter.outputURL)")
}
问题:
1)导致问题的原因是什么,以及解决方案是什么?
2)关于如何一致地重现错误的建议,希望有助于调试问题?
答案 0 :(得分:19)
似乎可以解决的问题是确保assetTrack
中的AVMutableVideoCompositionLayerInstruction
参数不是来自AVURLAsset
对象,而是来自addMutableTrackWithMediaType
返回的视频对象。< / p>
换句话说,这一行:
let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: sourceVideoTrack)
应该是:
let videoLayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
哎呀。几个小时无尽的挫败感,因为有时第一条线有效,有时它没有。
仍然想将赏金奖给某人。
如果您可以解释为什么第一行无法确定性地失败,而不是每次都失败,或者为AVMutableComposition及其相关类提供更深入的教程 - 为了向用户录制的视频添加文本叠加层 - 赏金是你的全部。 :)
答案 1 :(得分:6)
我猜你的一些视频&#39; sourceVideoTrack
是:
另一方面,可变轨道videoTrack
保证了正确的时间范围(按照AVMutableVideoCompositionInstruction
的指示),因此它始终有效。
答案 2 :(得分:3)
我使用AVAssetExportPresetPassthrough
导出预设而非使用特定分辨率或AVAssetExportHighestQuality
...
let exportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough)
这应该使用导出文件中导入视频的分辨率。
答案 3 :(得分:0)
如果U设置宽度或高度为零可能导致崩溃操作已停止,NSLocalizedFailureReason =视频无法合成
self.mutableVideoComposition.renderSize = CGSizeMake(assetVideoTrack.naturalSize.height,assetVideoTrack.naturalSize.width);
答案 4 :(得分:0)
迟到聚会了,但这对我有用。导出将“随机”失败。因此,然后调试视频轨道的长度和音频轨道的长度。
我注意到当音频轨道长于视频轨道时,导出将失败。
所以我做了这个改变:
let assetVideoTrack = asset.tracks(withMediaType: .video).first!
let assetAudioTrack = asset.tracks(withMediaType: .audio).first!
var validTimeRange:CMTimeRange
if assetVideoTrack.timeRange.duration.value > assetAudioTrack.timeRange.duration.value {
validTimeRange = assetVideoTrack.timeRange
} else {
validTimeRange = assetAudioTrack.timeRange
}
所以我将在这里使用该值:
let instruction = AVMutableVideoCompositionInstruction()
instruction.layerInstructions = [layerInstructions]
instruction.timeRange = validTimeRange
这为我解决了问题。现在可以100%地起作用。
导出的视频看起来不错,而录制的音频听起来很棒。
问题的答案:
1)是什么原因引起的,解决方案是什么?
2)关于如何一致地重现错误的建议,希望可以帮助调试问题?
对我来说以下是
视频和音频轨道之间的持续时间略有不同。在instruction.timeRange
中使用较短的时间将导致导出失败。
将instruction.timeRange
设置为两个音轨的较短时间,导出将失败。