iOS视频编辑之添加音轨的方法
之前各种事情在身,发现好久没更新文章了,临近年末,就把最近做的视频处理相关的内容整理一下吧~
最近在做视频编辑处理相关的开发,其中之一就是音视频合成,需求是用户可以选择将相册中的视频,然后将一段音乐片段加入其中,并可以实时调整视频原声以及添加的音乐音量,最后合成为一个视频。
分析
首先对于视频处理,万能的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"]]];
2 声明并实例化音视频处理的核心类
@property (nonatomic, readwrite, strong) avmutablecomposition *composition; @property (nonatomic, readwrite, strong) avmutablevideocomposition *videocomposition; @property (nonatomic, readwrite, strong) avmutableaudiomix *audiomix; avmutablecomposition *composition = [avmutablecomposition composition]; avmutablevideocomposition *videocomposition = [avmutablevideocomposition videocomposition]; avmutableaudiomix *audiomix = [avmutableaudiomix audiomix];
3 创建1条视频处理轨道以及2条音频处理轨道(视频原声+添加的音乐这俩条音轨)
avmutablecompositiontrack *compositionvideotracks = [composition addmutabletrackwithmediatype:avmediatypevideo preferredtrackid:kcmpersistenttrackid_invalid]; avmutablecompositiontrack *compositionaudiotracks[2]; compositionaudiotracks[0] = [composition addmutabletrackwithmediatype:avmediatypeaudio preferredtrackid:kcmpersistenttrackid_invalid]; compositionaudiotracks[1] = [composition addmutabletrackwithmediatype:avmediatypeaudio preferredtrackid:kcmpersistenttrackid_invalid];
4 根据之前创建好的视频以及音频资源(avurlasset)实例化一条视频轨道以及2条音频轨道
avassettrack *videotrack = [[asset trackswithmediatype:avmediatypevideo] objectatindex:0]; [compositionvideotracks inserttimerange:self.videotimerange oftrack:videotrack attime:kcmtimezero error:&error]; avassettrack *audiotrack = [[asset trackswithmediatype:avmediatypeaudio] objectatindex:0]; [_comtrack1 inserttimerange:self.videotimerange oftrack:audiotrack attime:kcmtimezero error:&error]; avassettrack *musictrack = [[self.musicasset trackswithmediatype:avmediatypeaudio] objectatindex:0]; [_comtrack2 inserttimerange:self.videotimerange oftrack:musictrack attime:kcmtimezero error:&error];
5 配置avmutableaudiomix参数,注意这里的trackid一定得是上面创建的avmutablecompositiontrack对应的trackid,而不是avassettrack中的trackid,之前使用avassettrack出过很奇怪的问题,而后在*上找到了这个解决方案
nsmutablearray<avaudiomixinputparameters *> *trackmixarray = [nsmutablearray<avaudiomixinputparameters *> array]; { avmutableaudiomixinputparameters *trackmix1 = [avmutableaudiomixinputparameters audiomixinputparameterswithtrack:_comtrack1]; trackmix1.trackid = _comtrack1.trackid; [trackmix1 setvolume:_videovolume attime:kcmtimezero]; [trackmixarray addobject:trackmix1]; avmutableaudiomixinputparameters *trackmix2 = [avmutableaudiomixinputparameters audiomixinputparameterswithtrack:_comtrack2]; trackmix2.trackid = _comtrack2.trackid; [trackmix2 setvolume:_musicvolume attime:kcmtimezero]; [trackmixarray addobject:trackmix2]; } audiomix.inputparameters = trackmixarray;
6 构建avplayeritem, 设置asset以及最重要的audiomix,然后交给avplayer就可以同时播放视频与音乐了!
- (avplayeritem *)playeritem { if (!_currentitem) { avplayeritem *playeritem = [avplayeritem playeritemwithasset:self.composition]; playeritem.videocomposition = self.videocomposition; playeritem.audiomix = self.audiomix; _currentitem = playeritem; } return _currentitem; }
7 播放时调整音量,这里其实和第5步一样,重新配置avmutableaudiomix参数后赋值给avplayeritem,设置音乐音量同理
- (void)setvideovolume:(cgfloat)volume { nsmutablearray *allaudioparams = [nsmutablearray array]; avmutableaudiomixinputparameters *audioinputparams = [avmutableaudiomixinputparameters audiomixinputparameters]; [audioinputparams settrackid:_comtrack1.trackid]; _videovolume = volume; [audioinputparams setvolume:_videovolume attime:kcmtimezero]; [allaudioparams addobject:audioinputparams]; avmutableaudiomixinputparameters *audioinputparams2 = [avmutableaudiomixinputparameters audiomixinputparameters]; [audioinputparams2 settrackid:_comtrack2.trackid]; [audioinputparams2 setvolume:_musicvolume attime:kcmtimezero]; [allaudioparams addobject:audioinputparams2]; avmutableaudiomix *audiomix = [avmutableaudiomix audiomix]; [audiomix setinputparameters:allaudioparams]; [_currentitem setaudiomix:audiomix]; }
导出实现
这里直接使用avassetexportsession来导出视频,与设置avplayeritem的audiomix属性相同,将audiomix设置给avassetexportsession实例即可导出混合的视频了
nsurl *outputfileurl = [nsurl fileurlwithpath:outputpath]; avassetexportsession *_assetexport =[[avassetexportsession alloc]initwithasset:self.composition presetname:avassetexportpreset1280x720]; _assetexport.outputfiletype = avfiletypempeg4; _assetexport.audiomix = _currentitem.audiomix; _assetexport.outputurl = outputfileurl; _assetexport.shouldoptimizefornetworkuse = yes; [_assetexport exportasynchronouslywithcompletionhandler:^{ // }];
最后贴上demo地址 https://github.com/lucifron1994/videomixaudiodemo
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。