Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging *.mp3 with *.mov file using AVAssetReader and AVAssetWriter

I was able to create a *.mov file using the AVAssetWriter. I used screenshot images and audio CMSampleBuffer i got from using AVCaptureSession and passed it to the inputs for the AVAssetWriter and it worked.

Now I would like to merge the *.mov file with a music from the ipod/iphone music library (most likely *.mp3). So my initial idea is to use AVAssetReader to get samples from the *.mp3 and *.mov file and pass the samples to an AVAssetWriter. Will this work? Just by reading the documentation i think that its possible.

I played with the idea for a bit but i cant make it to work.

My first attempt is to use 2 AVAssetReaders, one for each file. I used AVAssetReaderAudioMixOutput as an output for the 1st reader and then an AVAssetReaderVideoCompositionOutput for the 2nd one. The problem is using the AVAssetReaderVideoCompositionOutput requires that you pass it an AVVideoComposition. I had problems creating AVVideoComposition/AVMutableVideoComposition so I gave up on this solution. I dont get why I need to pass an AVVideoComposition, I just want to get samples of the movie.

So my 2nd attempt is similar but instead of using AVAssetReaderVideoCompositionOutput as an output for the 2nd reader, I used 2 AVAssetReaderTrackOutput instead. So now I have 2 readers with total of 3 outputs, and I tried using the sample from the outputs and pass it to an AVAssetWriter to create a *.mov file. I was able to run it, without errors/crashes. But I cant play the resulting *.mov file. Its play duration is 0.So again, no luck with the AVAssetReader+AVAssetWriter idea.

So my question/s: Can you use AVAssetReader and AVAssetWriter to merge two files? Has anybody tried doing this and make it to work? Should I just use AVMutableVideoComposition instead?

I will post my code if necessary.

like image 732
calampunay Avatar asked Nov 18 '25 23:11

calampunay


1 Answers

You could use AVAssetReader and AVAssetWriter to merge the two files, but that's probably further along the control/power curve than you need.

An AVAssetExportSession is probably a better fit as you can merge audio and video tracks in a few lines of code:

  1. create an AVComposition containing the mp3 and the mov as AVCompositionTracks
  2. export the AVComposition with AVAssetExportSession.
like image 102
Rhythmic Fistman Avatar answered Nov 21 '25 11:11

Rhythmic Fistman