title: Swift Merge audio and video files into one video
body: <p>I wrote a program in Swift.I want to merge a video with an audio file, but got this error.</p>
  
  <blockquote>
    <p>"failed Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo=0x17da4230 {NSLocalizedDescription=Operation Stopped, NSLocalizedFailureReason=The operation is not supported for this media.}"</p>
  </blockquote>
  
  <p><strong>code</strong>  </p>
  
  <pre><code>func mergeAudio(audioURL: NSURL, moviePathUrl: NSURL, savePathUrl: NSURL) {
      var composition = AVMutableComposition()
      let trackVideo:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID())
      let trackAudio:AVMutableCompositionTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: CMPersistentTrackID())
      let option = NSDictionary(object: true, forKey: "AVURLAssetPreferPreciseDurationAndTimingKey")
      let sourceAsset = AVURLAsset(URL: moviePathUrl, options: option as [NSObject : AnyObject])
      let audioAsset = AVURLAsset(URL: audioURL, options: option as [NSObject : AnyObject])
  
      let tracks = sourceAsset.tracksWithMediaType(AVMediaTypeVideo)
      let audios = audioAsset.tracksWithMediaType(AVMediaTypeAudio)
  
      if tracks.count &gt; 0 {
          let assetTrack:AVAssetTrack = tracks[0] as! AVAssetTrack
          let assetTrackAudio:AVAssetTrack = audios[0] as! AVAssetTrack
  
          let audioDuration:CMTime = assetTrackAudio.timeRange.duration
          let audioSeconds:Float64 = CMTimeGetSeconds(assetTrackAudio.timeRange.duration)
  
          trackVideo.insertTimeRange(CMTimeRangeMake(kCMTimeZero,audioDuration), ofTrack: assetTrack, atTime: kCMTimeZero, error: nil)
          trackAudio.insertTimeRange(CMTimeRangeMake(kCMTimeZero,audioDuration), ofTrack: assetTrackAudio, atTime: kCMTimeZero, error: nil)
      }
  
      var assetExport: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetPassthrough)
      assetExport.outputFileType = AVFileTypeMPEG4
      assetExport.outputURL = savePathUrl
      self.tmpMovieURL = savePathUrl
      assetExport.shouldOptimizeForNetworkUse = true
      assetExport.exportAsynchronouslyWithCompletionHandler { () -&gt; Void in
          switch assetExport.status {
          case AVAssetExportSessionStatus.Completed:
              let assetsLib = ALAssetsLibrary()
              assetsLib.writeVideoAtPathToSavedPhotosAlbum(savePathUrl, completionBlock: nil)
              println("success")
          case  AVAssetExportSessionStatus.Failed:
              println("failed \(assetExport.error)")
          case AVAssetExportSessionStatus.Cancelled:
              println("cancelled \(assetExport.error)")
          default:
              println("complete")
          }
      }
  
  }
  </code></pre>
  
  <p>In my idea media type like mpeg4 is wrong.
  Where is the problem? What am i missing?</p>