iOS視頻編輯之添加音軌的方法
之前各種事情在身,發(fā)現(xiàn)好久沒更新文章了,臨近年末,就把最近做的視頻處理相關(guān)的內(nèi)容整理一下吧~
最近在做視頻編輯處理相關(guān)的開發(fā),其中之一就是音視頻合成,需求是用戶可以選擇將相冊(cè)中的視頻,然后將一段音樂片段加入其中,并可以實(shí)時(shí)調(diào)整視頻原聲以及添加的音樂音量,最后合成為一個(gè)視頻。
分析
首先對(duì)于視頻處理,萬能的ffmpeg肯定可以實(shí)現(xiàn),但依賴ffmpeg并用一段magic一樣的語句維護(hù)擴(kuò)展都十分有限,對(duì)ffmpeg結(jié)構(gòu)不熟悉的話大量c的api也會(huì)無從下手,適合熟悉ffmpeg并且對(duì)AVFoundation陌生者使用。
其次的最優(yōu)方案就是AVFoundation了,作為蘋果音視頻編輯的利器可謂十分強(qiáng)大,官方有一 demo利用AVAudioEngine來實(shí)現(xiàn)音頻的混音,甚至可以對(duì)pcm數(shù)據(jù)進(jìn)行編輯,但是缺點(diǎn)也很明顯:1 和視頻沒什么關(guān)系,還得啟一個(gè)AVAudioPlayerNode來播放(那還不如單獨(dú)用AVAudioPlayer得了) 2 并沒有對(duì)音頻如“美聲,變音”之類的需求。所以不作為考慮范圍,不過可以實(shí)現(xiàn)一些特殊音效還是很厲害的,感興趣可以下來官方demo-Using AVAudioEngine for Playback, Mixing and Recording (AVAEMixerSample) 看看。
我最后選用的方案就是AVAudioMix,熟悉AVPlayer以及AVPlayerItem的話可能會(huì)注意到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是可以在播放的時(shí)設(shè)置,需要注意的就是trackID需要對(duì)應(yīng)。
補(bǔ)充:可能有人覺得最簡(jiǎn)單的是同時(shí)創(chuàng)建一個(gè)AVPlayer負(fù)責(zé)播放視頻,一個(gè)AVAudioPlayer播放音樂;當(dāng)然這種方法是可以實(shí)現(xiàn)基本需求,但完美出同步這倆個(gè)播放器的狀態(tài)會(huì)是一個(gè)問題,而且最終還是要經(jīng)歷混音寫文件過程,從邏輯上看十分糟糕。
播放實(shí)現(xiàn)
為了表述清晰下面省略AVPlayer等沒太大關(guān)系的代碼,同樣也可以下載我的 demo 來查看所有內(nèi)容。
流程如下:
1 創(chuàng)建視頻以及音頻的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 聲明并實(shí)例化音視頻處理的核心類
@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 創(chuàng)建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 根據(jù)之前創(chuàng)建好的視頻以及音頻資源(AVURLAsset)實(shí)例化一條視頻軌道以及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參數(shù),注意這里的trackID一定得是上面創(chuàng)建的AVMutableCompositionTrack對(duì)應(yīng)的trackID,而不是AVAssetTrack中的trackID,之前使用AVAssetTrack出過很奇怪的問題,而后在StackOverFlow上找到了這個(gè)解決方案
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 構(gòu)建AVPlayerItem, 設(shè)置asset以及最重要的audioMix,然后交給AVPlayer就可以同時(shí)播放視頻與音樂了!
- (AVPlayerItem *)playerItem {
if (!_currentItem) {
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:self.composition];
playerItem.videoComposition = self.videoComposition;
playerItem.audioMix = self.audioMix;
_currentItem = playerItem;
}
return _currentItem;
}
7 播放時(shí)調(diào)整音量,這里其實(shí)和第5步一樣,重新配置AVMutableAudioMix參數(shù)后賦值給AVPlayerItem,設(shè)置音樂音量同理
- (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];
}
導(dǎo)出實(shí)現(xiàn)
這里直接使用AVAssetExportSession來導(dǎo)出視頻,與設(shè)置AVPlayerItem的audioMix屬性相同,將audioMix設(shè)置給AVAssetExportSession實(shí)例即可導(dǎo)出混合的視頻了
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)技巧之狀態(tài)欄字體顏色的設(shè)置方法
有時(shí)候我們需要根據(jù)不同的背景修改狀態(tài)欄字體的顏色,下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)技巧之狀態(tài)欄字體顏色的設(shè)置方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧2018-08-08
淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑
這篇文章主要介紹了淺談iOS解析HTMl標(biāo)簽以及開發(fā)中的一些坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
IOS 中 new 和 alloc init 的對(duì)比
這篇文章主要介紹了IOS 中 new 和 alloc init 的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-02-02
ios使用NSProxy實(shí)現(xiàn)消息轉(zhuǎn)發(fā)
本文主要介紹了ios使用NSProxy實(shí)現(xiàn)消息轉(zhuǎn)發(fā),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼
本文是腳本之家小編給大家分享的iOS UITextView 首行縮進(jìn) 撤銷輸入 反撤銷輸入的實(shí)現(xiàn)代碼,需要的朋友參考下吧2017-09-09
ios啟動(dòng)頁強(qiáng)制豎屏(進(jìn)入App后允許橫屏與豎屏)
最近工作遇到這樣一個(gè)需要,當(dāng)進(jìn)入啟動(dòng)頁需要強(qiáng)制豎屏,而進(jìn)入APP后就允許橫屏與豎屏,通過查找相關(guān)的資料找到了解決的方法,所以將實(shí)現(xiàn)的方法整理后分享出來,需要的朋友們可以參考借鑒,下面來一起看看吧。2017-03-03
iOS手勢(shì)識(shí)別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢(shì)依賴,自定義手勢(shì))
這篇文章主要介紹了iOS手勢(shì)識(shí)別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢(shì)依賴,自定義手勢(shì)),具有一定的參考價(jià)值,有需要的可以參考一下。2016-11-11

