0

I want to merge video captured by AVCaptureSession() and audio recorded by AVAudioRecorder() into one mp4 file ..

I tried this solution Swift Merge audio and video files into one video

and this Merging audio and video Swift

but both of them didn't work. They both gave me the same error : Thread1 : Signal SIGABRT

So what is the right way to merge audio and video ?

UPDATE:-

I found where the app crashes every time. It crashes on the code line that tries to get an AVAssetTrack from AVAsset

func merge(audio:URL, withVideo video : URL){

    // create composition
    let mutableComposition = AVMutableComposition()

    // Create the video composition track.
    let mutableCompositionVideoTrack : AVMutableCompositionTrack = mutableComposition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)

    // Create the audio composition track.
    let mutableCompositionAudioTrack : AVMutableCompositionTrack = mutableComposition.addMutableTrack(withMediaType: AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)

    // create media assets and tracks
    let videoAsset = AVAsset(url: video)  
    let videoAssetTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] // ** always crashes here ** //

    let audioAsset = AVAsset(url: audio) 
    let audioAssetTrack = audioAsset.tracks(withMediaType: AVMediaTypeAudio)[0] // ** always crashes here ** //

....

}
  • Always check for count of elements in the array before subscript the items. like: if videoAsset.tracks(withMediaType: AVMediaTypeVideo).count > 0 { let videoAssetTrack = videoAsset.tracks(withMediaType: AVMediaTypeVideo)[0] } – ninjaproger Jul 03 '17 at 18:20
  • You can also implement a safe subscript, that returns an optional (https://stackoverflow.com/questions/25329186/safe-bounds-checked-array-lookup-in-swift-through-optional-bindings) – darksider Nov 15 '17 at 10:19

0 Answers0