最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS視頻添加背景音樂同時(shí)保留原音

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

話不多說,請(qǐng)看代碼:

//抽取原視頻的音頻與需要的音樂混合 
-(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)建一個(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(@"無法刪除文件,錯(cuò)誤信息:%@",[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)建一個(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); 
} 

//通過文件路徑建立和添加音頻素材 
- (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)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件

    iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件

    這篇文章主要介紹了iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • iOS中UIActionSheet動(dòng)態(tài)添加按鈕

    iOS中UIActionSheet動(dòng)態(tài)添加按鈕

    這篇文章主要介紹了iOS中UIActionSheet動(dòng)態(tài)添加按鈕功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-06-06
  • 支持Xcode10和適配iPhone XS Max、iPhone XR的方法

    支持Xcode10和適配iPhone XS Max、iPhone XR的方法

    這篇文章主要介紹了支持Xcode10和適配iPhone XS Max、iPhone XR的方法,文中通過示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • 詳解iOS獲取通訊錄的4種方式

    詳解iOS獲取通訊錄的4種方式

    這篇文章主要為大家詳細(xì)介紹了iOS獲取通訊錄的4種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • iOS實(shí)現(xiàn)PDF文件瀏覽功能

    iOS實(shí)現(xiàn)PDF文件瀏覽功能

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)PDF文件瀏覽功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iPhone X官方文檔的適配學(xué)習(xí)詳解

    iPhone X官方文檔的適配學(xué)習(xí)詳解

    本篇文章主要介紹了iPhone X官方文檔的適配學(xué)習(xí)詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • iOS Xcode自定義代碼塊及遷移的實(shí)現(xiàn)方法

    iOS Xcode自定義代碼塊及遷移的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于iOS Xcode自定義代碼塊及遷移的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用iOS Xcode具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 零基礎(chǔ)學(xué)習(xí)iOS直播之采集

    零基礎(chǔ)學(xué)習(xí)iOS直播之采集

    直播的采集由采集的設(shè)備(攝像頭、話筒)不同分為視頻采集和音頻采集,本篇文章會(huì)分別介紹,需要的朋友一起來看下吧
    2016-12-12
  • iOS AFNetworking中cookie重定向代碼

    iOS AFNetworking中cookie重定向代碼

    這篇文章主要介紹了iOS AFNetworking中cookie重定向代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • iOS開發(fā)中蘋果輸入手機(jī)號(hào)變用戶的名字

    iOS開發(fā)中蘋果輸入手機(jī)號(hào)變用戶的名字

    今天我們的用戶輸入手機(jī)號(hào)之后變成了用戶的名字,沒辦法獲取驗(yàn)證碼,因?yàn)槭謾C(jī)格式不對(duì)。下面通過本文給大家分享開發(fā)中蘋果輸入手機(jī)號(hào)變用戶的名字,需要的朋友可以參考下
    2017-05-05

最新評(píng)論

保康县| 平邑县| 胶南市| 惠安县| 龙陵县| 清镇市| 富裕县| 威海市| 突泉县| 洪江市| 综艺| 肥东县| 南郑县| 台东县| 广德县| 大新县| 石景山区| 阳泉市| 合肥市| 灵璧县| 锡林郭勒盟| 宝应县| 邵东县| 双峰县| 永丰县| 延吉市| 新宁县| 惠州市| 武宁县| 永安市| 鸡西市| 阿瓦提县| 南皮县| 天长市| 新巴尔虎右旗| 大足县| 正阳县| 孟津县| 漠河县| 清远市| 永州市|