[英]How to export arbitrary segment of video? “Operation Stopped” error when exporting last X seconds of video
[英]In Production Mode - I got "Operation Stopped" error when exporting video
当应用程序进入生产模式时,请帮我解决这个问题我每次制作视频时都会出现“操作停止”错误
它在开发模式下工作完美,但在生产中它给出了这个错误
实际上,过程是将多个视频合并为一个,并且有 4 个录制的视频以及前端和后端保险杠视频,并为该视频添加音轨。
合并过程就像所有视频轨道的 AVMutableVideoCompositionLayerInstruction 与 [front bumper, video1, video2, video3, end bumper] 之类的数组组合在一个 AVMutableVideoCompositionInstruction 中。
我也设置了“mainInstruction.timeRange”意味着主视频持续时间和音频持续时间都相同然后仍然发生此错误
当我在合并后导出最终视频时会发生此错误。
=> 将音频添加到轨道的代码
let audioMix = AVMutableAudioMix()
let musicAudioTrack: AVMutableCompositionTrack = mixComposition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: Int32(kCMPersistentTrackID_Invalid))!
do {
try musicAudioTrack.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: runningTime!), of: musicAsset.tracks(withMediaType: AVMediaType.audio)[0], at: CMTime.zero)
musicAudioTrack.preferredVolume = self.volumeSlider.value
let audioMixInputParams = AVMutableAudioMixInputParameters()
audioMixInputParams.trackID = musicAudioTrack.trackID
audioMixInputParams.setVolume(self.volumeSlider.value, at: CMTime.zero)
audioMix.inputParameters.append(audioMixInputParams)
} catch {
}
=> MainInstruction的代码
let mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: runningTime!)
mainInstruction.layerInstructions = [videoLayerInstructionFront!,videoLayerIntruction1!,videoLayerIntruction2,videoLayerIntruction3,videoLayerIntruction4,videoLayerInstructionEnd!]
let renderWidth = videoTrack.naturalSize.width
let renderHeight = videoTrack.naturalSize.height
let mainCompositionInst = AVMutableVideoComposition()
mainCompositionInst.frameDuration = CMTimeMake(value: 1, timescale: 60)
mainCompositionInst.renderScale = 1.0
mainCompositionInst.renderSize = CGSize(width: renderWidth, height: renderHeight)
mainCompositionInst.instructions = [mainInstruction]
let exporter: AVAssetExportSession = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)!
exporter.outputURL = self.template.finalURL
exporter.outputFileType = AVFileType.mov
exporter.audioMix = audioMix
exporter.shouldOptimizeForNetworkUse = true
exporter.videoComposition = mainCompositionInst
exporter.exportAsynchronously(completionHandler: {
DispatchQueue.main.async{
self.exportDidFinish(session: exporter)
}
})
您必须检查音频时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.