简体   繁体   English

使用AVAssetReader和AVAssetWriter将* .mp3与* .mov文件合并

[英]Merging *.mp3 with *.mov file using AVAssetReader and AVAssetWriter

I was able to create a *.mov file using the AVAssetWriter. 我能够使用AVAssetWriter创建一个* .mov文件。 I used screenshot images and audio CMSampleBuffer i got from using AVCaptureSession and passed it to the inputs for the AVAssetWriter and it worked. 我使用AVCaptureSession获得的截图图像和音频CMSampleBuffer,并将其传递给AVAssetWriter的输入,并且它工作正常。

Now I would like to merge the *.mov file with a music from the ipod/iphone music library (most likely *.mp3). 现在我想将* .mov文件与来自ipod / iphone音乐库的音乐合并(很可能是* .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. 所以我最初的想法是使用AVAssetReader从* .mp3和* .mov文件中获取样本,并将样本传递给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. 我的第一次尝试是使用2个AVAssetReaders,每个文件一个。 I used AVAssetReaderAudioMixOutput as an output for the 1st reader and then an AVAssetReaderVideoCompositionOutput for the 2nd one. 我使用AVAssetReaderAudioMixOutput作为第一个读取器的输出,然后使用AVAssetReaderVideoCompositionOutput作为第二个读取器的输出。 The problem is using the AVAssetReaderVideoCompositionOutput requires that you pass it an AVVideoComposition. 问题是使用AVAssetReaderVideoCompositionOutput要求您传递AVVideoComposition。 I had problems creating AVVideoComposition/AVMutableVideoComposition so I gave up on this solution. 我在创建AVVideoComposition / AVMutableVideoComposition时遇到了问题,所以我放弃了这个解决方案。 I dont get why I need to pass an AVVideoComposition, I just want to get samples of the movie. 我不明白为什么我需要传递一个AVVideoComposition,我只是想得到电影的样本。

So my 2nd attempt is similar but instead of using AVAssetReaderVideoCompositionOutput as an output for the 2nd reader, I used 2 AVAssetReaderTrackOutput instead. 所以我的第二次尝试是类似的,但我没有使用AVAssetReaderVideoCompositionOutput作为第二个阅读器的输出,而是使用了2个AVAssetReaderTrackOutput。 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. 所以现在我有2个阅读器,共有3个输出,我尝试使用输出中的样本并将其传递给AVAssetWriter以创建* .mov文件。 I was able to run it, without errors/crashes. 我能够运行它,没有错误/崩溃。 But I cant play the resulting *.mov file. 但是我无法播放生成的* .mov文件。 Its play duration is 0.So again, no luck with the AVAssetReader+AVAssetWriter idea. 它的播放持续时间为0.再次,AVAssetReader + AVAssetWriter想法没有运气。

So my question/s: Can you use AVAssetReader and AVAssetWriter to merge two files? 所以我的问题:您可以使用AVAssetReader和AVAssetWriter合并两个文件吗? Has anybody tried doing this and make it to work? 有没有人试过这样做并让它起作用? Should I just use AVMutableVideoComposition instead? 我应该只使用AVMutableVideoComposition吗?

I will post my code if necessary. 如有必要,我会发布我的代码。

You could use AVAssetReader and AVAssetWriter to merge the two files, but that's probably further along the control/power curve than you need. 您可以使用AVAssetReader和AVAssetWriter合并这两个文件,但这可能比您需要的控制/功率曲线更进一步。

An AVAssetExportSession is probably a better fit as you can merge audio and video tracks in a few lines of code: AVAssetExportSession可能更合适,因为您可以在几行代码中合并音频和视频轨道:

  1. create an AVComposition containing the mp3 and the mov as AVCompositionTracks 创建一个包含mp3和mov作为AVCompositionTracks的AVComposition
  2. export the AVComposition with AVAssetExportSession. 使用AVAssetExportSession导出AVComposition。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM