Search code examples
iosvideoswift5avassetexportsessionaudio-video-sync

In Production Mode - I got "Operation Stopped" error when exporting video


Please help me to solve this issue when app goes on production mode i got "Operation stopped" error every time when i make video

  • its works perfect on development mode but in production its gives this error

  • Actually process is merging multiple video to one and there 4 recorded video and front and end bumper videos and also add audio track to that video.

  • And merging process be like all video track's AVMutableVideoCompositionLayerInstruction are combine in one AVMutableVideoCompositionInstruction with array like [front bumper, video1, video2, video3, end bumper].

  • also i have set "mainInstruction.timeRange" means main video duration and audio duration both are same then still this error happens

  • and this error happens when i exporting final video after merging.

=> code for adding audio to track

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 {
                
            }

=> Code for MainInstruction

  • runningTime is duration of final video
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)
                }
            })

  • Finally here, when I export my final video there is an error "Operation stopped".

Solution

  • you just need to check the audio time.