欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS視頻添加背景音樂同時保留原音

 更新時間:2017年03月30日 10:23:36   作者:BearsG  
本文主要介紹了iOS視頻添加背景音樂同時保留原音的實現(xiàn)方法。具有很好的參考價值,下面跟著小編一起來看下吧

話不多說,請看代碼:

//抽取原視頻的音頻與需要的音樂混合 
-(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)]; 

 //本地要插入的音樂 
 NSString *bundleDirectory =[[NSBundlemainBundle]bundlePath]; 
 NSString *path = [bundleDirectorystringByAppendingPathComponent:@"30secs.mp3"]; 
 NSURL *assetURL2 =[NSURLfileURLWithPath:path]; 
 //獲取設(shè)置完的本地音樂素材 
 [selfsetUpAndAddAudioAtPath:assetURL2toComposition:compositionstart:startTimedura:trackDurationoffset:CMTimeMake(0,44100)]; 

 //創(chuàng)建一個可變的音頻混合 
 AVMutableAudioMix *audioMix =[AVMutableAudioMixaudioMix]; 
 audioMix.inputParameters =[NSArrayarrayWithArray:audioMixParams];//從數(shù)組里取出處理后的音頻軌道參數(shù) 

 //創(chuàng)建一個輸出 
 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(@"錯誤,信息: %@", 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(@"無法刪除文件,錯誤信息:%@",[error localizedDescription]); 
 } 

 //聲音來源路徑(最終混合的音頻) 
 NSURL *audio_inputFileUrl =self.mixURL; 

 //視頻來源路徑 
 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);//聲音長度截取范圍==視頻長度 
 AVMutableCompositionTrack*b_compositionAudioTrack = [mixCompositionaddMutableTrackWithMediaType:AVMediaTypeAudiopreferredTrackID:kCMPersistentTrackID_Invalid]; 
 [b_compositionAudioTrackinsertTimeRange:audio_timeRangeofTrack:[[audioAssettracksWithMediaType:AVMediaTypeAudio]objectAtIndex:0]atTime:nextClipStartTimeerror:nil]; 

 //創(chuàng)建一個輸出 
 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); 
} 

//通過文件路徑建立和添加音頻素材 
- (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]; 
 [trackMixsetVolume:0.8fatTime:startTime]; 

 //素材加入數(shù)組 
 [audioMixParamsaddObject:trackMix]; 

 //Insert audio into track //offsetCMTimeMake(0, 44100) 
 ok = [trackinsertTimeRange:tRangeofTrack:sourceAudioTrackatTime:kCMTimeInvaliderror:&error]; 
} 

 #pragma mark - 保存路徑 
-(NSString*)getLibarayPath 
{ 
 NSFileManager *fileManager =[NSFileManagerdefaultManager]; 
 NSArray* paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
 NSString* path = [pathsobjectAtIndex:0]; 
 NSString *movDirectory = [pathstringByAppendingPathComponent:@"tmpMovMix"]; 
 [fileManagercreateDirectoryAtPath:movDirectorywithIntermediateDirectories:YESattributes:nilerror:nil]; 
 return movDirectory; 
} 

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • iOS中UIScrollView嵌套UITableView的實踐教程

    iOS中UIScrollView嵌套UITableView的實踐教程

    在UIScrollView嵌套UITableView的問題相信大家都遇到過,小編最近在工作中就遇到了這個問題,所以這篇文章主要介紹了iOS中UIScrollView嵌套UITableView的相關(guān)資料,文中介紹的方法是通過自己的實踐所得來的,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • iOS開發(fā)教程之APP內(nèi)部切換語言的實現(xiàn)方法

    iOS開發(fā)教程之APP內(nèi)部切換語言的實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之APP內(nèi)部切換語言的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • iOS中修改UISearchBar圓角的小技巧分享

    iOS中修改UISearchBar圓角的小技巧分享

    UISearchBar也是iOS開發(fā)常用控件之一,點進去看看里面的屬性barStyle、text、placeholder等等。下面這篇文章主要給大家分享了iOS中修改UISearchBar圓角的小技巧,文中給出了詳細的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。
    2017-05-05
  • 舉例講解iOS中延遲加載和上拉刷新/下拉加載的實現(xiàn)

    舉例講解iOS中延遲加載和上拉刷新/下拉加載的實現(xiàn)

    這篇文章主要介紹了舉例講解iOS中延遲加載和上拉刷新/下拉加載的實現(xiàn),語言依然為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-09-09
  • iOS10通知框架UserNotification理解與應(yīng)用

    iOS10通知框架UserNotification理解與應(yīng)用

    在iOS10系統(tǒng)中,通知被整合進了UserNotification框架,除了使通知的處理脫離了UIApplication,通知功能的相關(guān)開發(fā)更加結(jié)構(gòu)化與模塊化外,還新增開放了許多更加靈活的開發(fā)接口,現(xiàn)在,開發(fā)者可以為通知定義UI末班,添加媒體附件,需要的朋友可以參考下
    2016-09-09
  • IOS 基礎(chǔ)之設(shè)置 tableview 的分割線

    IOS 基礎(chǔ)之設(shè)置 tableview 的分割線

    這篇文章主要介紹了IOS 基礎(chǔ)之設(shè)置 tableview 的分割線的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • iOS狀態(tài)欄的顯示與隱藏的示例代碼

    iOS狀態(tài)欄的顯示與隱藏的示例代碼

    這篇文章主要介紹了iOS狀態(tài)欄的顯示與隱藏的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • iOS實現(xiàn)支持小數(shù)的星星評分組件實例代碼

    iOS實現(xiàn)支持小數(shù)的星星評分組件實例代碼

    程序中需要打分的功能,在網(wǎng)上找了幾個,都不是很滿意。所以自己動手實現(xiàn)了一個,下面這篇文章主要給大家介紹了關(guān)于利用iOS實現(xiàn)支持小數(shù)的星星評分組件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • iOS如何利用一句話完成轉(zhuǎn)場動畫

    iOS如何利用一句話完成轉(zhuǎn)場動畫

    這篇文章主要給大家介紹了關(guān)于iOS如何利用一句話完成轉(zhuǎn)場動畫的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • 全面解析Objective-C中的block代碼塊的使用

    全面解析Objective-C中的block代碼塊的使用

    這篇文章主要介紹了Objective-C中的block代碼塊的使用,包括閉包等重要特性的講解,需要的朋友可以參考下
    2015-11-11

最新評論