




下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】iOS中如何給視頻添加背景音樂(lè)同時(shí)保留原音
代碼://抽取原視頻的音頻與需要的音樂(lè)混合
-(void)addmusic:(id)sender
{
[MBProgressHUDshowHUDAddedTo:self.viewanimated:YES];
AVMutableComposition
*composition
=[AVMutableCompositioncomposition];
audioMixParams
=[[NSMutableArrayalloc]initWithObjects:nil];
//錄制的視頻
NSURL
*video_inputFileUrl
=[NSURLfileURLWithPath:self.videoPath];
AVURLAsset
*songAsset
=[AVURLAssetURLAssetWithURL:video_inputFileUrloptions:nil];
CMTime
startTime
=CMTimeMakeWithSeconds(0,songAsset.duration.timescale);
CMTime
trackDuration
=songAsset.duration;
//獲取視頻中的音頻素材
[selfsetUpAndAddAudioAtPath:video_inputFileUrltoComposition:compositionstart:startTimedura:trackDurationoffset:CMTimeMake(14*44100,44100)];
//本地要插入的音樂(lè)
NSString
*bundleDirectory
=[[NSBundlemainBundle]bundlePath];
NSString
*path
=
[bundleDirectorystringByAppendingPathComponent:@"30secs.mp3"];
NSURL
*assetURL2
=[NSURLfileURLWithPath:path];
//獲取設(shè)置完的本地音樂(lè)素材
[selfsetUpAndAddAudioAtPath:assetURL2toComposition:compositionstart:startTimedura:trackDurationoffset:CMTimeMake(0,44100)];
//創(chuàng)建一個(gè)可變的音頻混合
AVMutableAudioMix
*audioMix
=[AVMutableAudioMixaudioMix];
audioMix.inputParameters
=[NSArrayarrayWithArray:audioMixParams];//從數(shù)組里取出處理后的音頻軌道參數(shù)
//創(chuàng)建一個(gè)輸出
AVAssetExportSession
*exporter
=[[AVAssetExportSessionalloc]
initWithAsset:composition
presetName:AVAssetExportPresetAppleM4A];
exporter.audioMix
=
audioMix;
exporter.outputFileType=@"com.apple.m4a-audio";
NSString*
fileName
=[NSStringstringWithFormat:@"%@.mov",@"overMix"];
//輸出路徑
NSString
*exportFile
=[NSStringstringWithFormat:@"%@/%@",[selfgetLibarayPath],
fileName];
if([[NSFileManagerdefaultManager]fileExistsAtPath:exportFile])
{
[[NSFileManagerdefaultManager]removeItemAtPath:exportFileerror:nil];
}
NSLog(@"是否在主線程1%d",[NSThreadisMainThread]);
NSLog(@"輸出路徑===%@",exportFile);
NSURL
*exportURL
=[NSURLfileURLWithPath:exportFile];
exporter.outputURL
=
exportURL;
self.mixURL
=exportURL;
[exporterexportAsynchronouslyWithCompletionHandler:^{
int
exportStatus
=(int)exporter.status;
switch
(exportStatus){
caseAVAssetExportSessionStatusFailed:{
NSError
*exportError
=exporter.error;
NSLog(@"錯(cuò)誤,信息:
%@",
exportError);
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
break;
}
caseAVAssetExportSessionStatusCompleted:{
NSLog(@"是否在主線程2%d",[NSThreadisMainThread]);
NSLog(@"成功");
//最終混合
[selftheVideoWithMixMusic];
break;
}
}
}];
}
//最終音頻和視頻混合
-(void)theVideoWithMixMusic
{
NSError
*error
=nil;
NSFileManager
*fileMgr
=[NSFileManagerdefaultManager];
NSString
*documentsDirectory
=[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString
*videoOutputPath
=[documentsDirectorystringByAppendingPathComponent:@"test_output.mp4"];
if
([fileMgrremoveItemAtPath:videoOutputPatherror:&error]!=YES)
{
NSLog(@"無(wú)法刪除文件,錯(cuò)誤信息:%@",[error
localizedDescription]);
}
//聲音來(lái)源路徑(最終混合的音頻)
NSURL
*audio_inputFileUrl
=self.mixURL;
//視頻來(lái)源路徑
NSURL
*video_inputFileUrl
=
[NSURLfileURLWithPath:self.videoPath];
//最終合成輸出路徑
NSString
*outputFilePath
=[documentsDirectorystringByAppendingPathComponent:@"final_video.mp4"];
NSURL
*outputFileUrl
=
[NSURLfileURLWithPath:outputFilePath];
if([[NSFileManagerdefaultManager]fileExistsAtPath:outputFilePath])
[[NSFileManagerdefaultManager]removeItemAtPath:outputFilePatherror:nil];
CMTime
nextClipStartTime
=kCMTimeZero;
//創(chuàng)建可變的音頻視頻組合
AVMutableComposition*
mixComposition
=[AVMutableCompositioncomposition];
//視頻采集
AVURLAsset*
videoAsset
=[[AVURLAssetalloc]initWithURL:video_inputFileUrloptions:nil];
CMTimeRange
video_timeRange
=CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack*a_compositionVideoTrack
=
[mixCompositionaddMutableTrackWithMediaType:AVMediaTypeVideopreferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrackinsertTimeRange:video_timeRangeofTrack:[[videoAssettracksWithMediaType:AVMediaTypeVideo]objectAtIndex:0]atTime:nextClipStartTimeerror:nil];
//聲音采集
AVURLAsset*
audioAsset
=[[AVURLAssetalloc]initWithURL:audio_inputFileUrloptions:nil];
CMTimeRange
audio_timeRange
=CMTimeRangeMake(kCMTimeZero,videoAsset.duration);//聲音長(zhǎng)度截取范圍==視頻長(zhǎng)度
AVMutableCompositionTrack*b_compositionAudioTrack
=
[mixCompositionaddMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrackinsertTimeRange:audio_timeRangeofTrack:[[audioAssettracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]atTime:nextClipStartTimeerror:nil];
//創(chuàng)建一個(gè)輸出
AVAssetExportSession*
_assetExport
=[[AVAssetExportSessionalloc]initWithAsset:mixCompositionpresetName:AVAssetExportPresetMediumQuality];
_assetExport.outputFileType
=AVFileTypeQuickTimeMovie;
_assetExport.outputURL
=outputFileUrl;
_assetExport.shouldOptimizeForNetworkUse=YES;
self.theEndVideoURL=outputFileUrl;
[_assetExportexportAsynchronouslyWithCompletionHandler:
^(void
)
{
[MBProgressHUDhideHUDForView:self.viewanimated:YES];
//播放
NSURL*url
=
[NSURLfileURLWithPath:outputFilePath];
MPMoviePlayerViewController
*theMovie
=[[MPMoviePlayerViewControlleralloc]initWithContentURL:url];
[selfpresentMoviePlayerViewControllerAnimated:theMovie];
theMovie.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
[theMovie.moviePlayerplay];
}
];
NSLog(@"完成!輸出路徑==%@",outputFilePath);
}
//通過(guò)文件路徑建立和添加音頻素材
-
(void)setUpAndAddAudioAtPath:(NSURL*)assetURLtoComposition:(AVMutableComposition*)composition
start:(CMTime)startdura:(CMTime)duraoffset:(CMTime)offset{
AVURLAsset
*songAsset
=[AVURLAssetURLAssetWithURL:assetURLoptions:nil];
AVMutableCompositionTrack
*track
=[compositionaddMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid];
AVAssetTrack
*sourceAudioTrack
=[[songAssettracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0];
NSError
*error
=nil;
BOOL
ok
=NO;
CMTime
startTime
=
start;
CMTime
trackDuration
=
dura;
CMTimeRange
tRange
=CMTimeRangeMake(startTime,trackDuration);
//設(shè)置音量
//AVMutableAudioMixInputParameters(輸入?yún)?shù)可變的音頻混合)
//audioMixInputParametersWithTrack(音頻混音輸入?yún)?shù)與軌道)
AVMutableAudioMixInputParameters
*trackMix
=[AVMutableAudioMixInputParametersaudioMixInputParametersWithTrack:track];
[trackMix
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 房地產(chǎn)開(kāi)發(fā)合作協(xié)議合同
- 三農(nóng)田改造方案設(shè)計(jì)指南
- 建筑木工分包合同
- 上海聲屏障施工方案
- 防水安全生產(chǎn)施工方案
- pvc地板膠施工方案
- 燜渣坑施工方案
- 余姚耐磨地坪施工方案
- 自建房水泥欄桿施工方案
- 青島市eps線條施工方案
- 2024-2025學(xué)年第二學(xué)期天域全國(guó)名校協(xié)作體高三3月聯(lián)考 語(yǔ)文試卷(含答案)
- 2025年中考百日誓師活動(dòng)教師代表發(fā)言(三)
- 中國(guó)家用通風(fēng)電器具制造行業(yè)分析報(bào)告
- 生物-天一大聯(lián)考2025屆高三四省聯(lián)考(陜晉青寧)試題和解析
- 天津2025年天津市住房公積金管理中心招聘9人筆試歷年參考題庫(kù)附帶答案詳解-1
- 區(qū)間價(jià)格突破策略(TB版)
- 高中主題班會(huì) 遠(yuǎn)離背后“蛐蛐”課件-高二下學(xué)期人際交往主題班會(huì)
- DeepSeek科普課件深度解析
- 大模型應(yīng)用服務(wù)平臺(tái)建設(shè)研究
- 2025年度智慧養(yǎng)老服務(wù)平臺(tái)開(kāi)發(fā)與運(yùn)營(yíng)服務(wù)合同
- 2025年湖南科技職業(yè)學(xué)院高職單招語(yǔ)文2018-2024歷年參考題庫(kù)頻考點(diǎn)含答案解析
評(píng)論
0/150
提交評(píng)論