之前各种事情在身,发现好久没更新文章了,临近年末,就把最近做的视频处理相关的内容整理一下吧~
最近在做视频编辑处理相关的开发,其中之一就是音视频合成,需求是用户可以选择将相册中的视频,然后将一段音乐片段加入其中,并可以实时调整视频原声以及添加的音乐音量,最后合成为一个视频。
分析
首先对于视频处理,万能的ffmpeg肯定可以实现,但依赖ffmpeg并用一段magic一样的语句维护扩展都十分有限,对ffmpeg结构不熟悉的话大量c的api也会无从下手,适合熟悉ffmpeg并且对AVFoundation陌生者使用。
其次的最优方案就是AVFoundation了,作为苹果音视频编辑的利器可谓十分强大,官方有一 demo利用AVAudioEngine来实现音频的混音,甚至可以对pcm数据进行编辑,但是缺点也很明显:1 和视频没什么关系,还得启一个AVAudioPlayerNode来播放(那还不如单独用AVAudioPlayer得了) 2 并没有对音频如“美声,变音”之类的需求。所以不作为考虑范围,不过可以实现一些特殊音效还是很厉害的,感兴趣可以下来官方demo-Using AVAudioEngine for Playback, Mixing and Recording (AVAEMixerSample) 看看。
我最后选用的方案就是AVAudioMix,熟悉AVPlayer以及AVPlayerItem的话可能会注意到AVAudioMix 是作为属性存在于AVPlayerItem的分类中。
/*!
@property audioMix
@abstract Indicates the audio mix parameters to be applied during playback
@discussion
The inputParameters of the AVAudioMix must have trackIDs that correspond to a track of the receiver's asset. Otherwise they will be ignored. (See AVAudioMix.h for the declaration of AVAudioMixInputParameters and AVPlayerItem's asset property.)
*/
@property (nonatomic, copy, nullable) AVAudioMix *audioMix;
"Indicates the audio mix parameters to be applied during playback" 表明audioMix是可以在播放的时设置,需要注意的就是trackID需要对应。
补充:可能有人觉得最简单的是同时创建一个AVPlayer负责播放视频,一个AVAudioPlayer播放音乐;当然这种方法是可以实现基本需求,但完美出同步这俩个播放器的状态会是一个问题,而且最终还是要经历混音写文件过程,从逻辑上看十分糟糕。
播放实现
为了表述清晰下面省略AVPlayer等没太大关系的代码,同样也可以下载我的 demo 来查看所有内容。
流程如下:
1 创建视频以及音频的AVURLAsset
AVURLAsset *videoAsset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"demo" ofType:@"mp4"]]];
AVURLAsset *musicAsset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"music" ofType:@"mp3"]]];










