1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      iOS中如何給視頻添加背景音樂同時(shí)保留原音-創(chuàng)新互聯(lián)

      這篇文章給大家分享的是有關(guān)iOS中如何給視頻添加背景音樂同時(shí)保留原音的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

      創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè),為客戶提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開發(fā),品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。

      代碼:

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

      感謝各位的閱讀!關(guān)于“iOS中如何給視頻添加背景音樂同時(shí)保留原音”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

      另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


      新聞名稱:iOS中如何給視頻添加背景音樂同時(shí)保留原音-創(chuàng)新互聯(lián)
      URL分享:http://www.ef60e0e.cn/article/djopod.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        中卫市| 乌什县| 南昌市| 梓潼县| 金溪县| 福安市| 达尔| 四子王旗| 探索| 凤阳县| 南开区| 瑞金市| 大余县| 阳信县| 万宁市| 德安县| 龙川县| 江川县| 许昌县| 湖口县| 开封县| 吉林市| 沐川县| 伊吾县| 新源县| 滨海县| 泾源县| 阿合奇县| 松滋市| 宁国市| 绍兴市| 正镶白旗| 屏东市| 榆林市| 西林县| 武邑县| 绍兴县| 平泉县| 谢通门县| 富宁县| 陆丰市|