2013-03-01 60 views
3

在我的應用程序需要合併的音頻和視頻,然後我需要在媒體播放器來播放音頻文件合併音頻和視頻。我如何合併IOS中的音頻和視頻。是否有任何源代碼。請建議我一些想法如何使用AVMutableCompositionTrack

在此先感謝

+0

做一些研究 - 比如觀看視頻WWDC +示例代碼。 – 2013-03-02 02:04:18

回答

10

使用此

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audioUrl options:nil]; 
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil]; 

AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio 
                        preferredTrackID:kCMPersistentTrackID_Invalid]; 
[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration) 
            ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] 
            atTime:kCMTimeZero error:nil]; 

AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                        preferredTrackID:kCMPersistentTrackID_Invalid]; 
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
           ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
           atTime:kCMTimeZero error:nil]; 

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                     presetName:AVAssetExportPresetHighestQuality]; 

NSString* videoName = @"export.mov"; 

NSString *exportPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName]; 
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
{ 
    [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil]; 
} 

_assetExport.outputFileType = @"com.apple.quicktime-movie"; 
_assetExport.outputURL = exportUrl; 
_assetExport.shouldOptimizeForNetworkUse = YES; 

[_assetExport exportAsynchronouslyWithCompletionHandler: 
^(void) {  
      // your completion code here 
    }  
} 
]; 
+0

我試過這段代碼。它不起作用,並且在上面的編碼中也有一個錯誤。更換initWithURL與URLAssetWithURL – btmanikandan 2013-03-02 17:38:28

+0

我用這個代碼用1秒的音頻文件和3S的視頻文件。結果是一個帶有黑色屏幕1s音頻的視頻,其次是2s黑色屏幕,接着是3s視頻。你知道什麼可能導致這個問題?我只是想將音頻添加到視頻的開頭。 – 2014-07-01 18:30:56

+0

我只合併一個音頻/視頻文件。問題是,我的視頻文件爲40秒,音頻文件爲28秒。因此,對於剩餘的12(40-28)秒 - 我想在音頻文件中從0秒開始重複播放。我怎麼樣?有沒有直接的方法來做到這一點? – Hemang 2015-06-18 11:23:37

1

http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios訪問本教程用於合併音頻和視頻文件

+0

注意,[只有鏈路答案](http://meta.stackoverflow.com/tags/link-only-answers/info)氣餒,SO答案應該是一個解決方案的搜索的結束點(相對於而另一個引用的中途停留時間往往會隨着時間推移而過時)。請考慮在此添加獨立的摘要,並將鏈接保留爲參考。 – kleopatra 2013-11-01 11:44:40

2

您可以通過創建易變的成分合並視頻。

AVMutableComposition* composition = [[AVMutableComposition alloc]init]; 
AVURLAsset* video1 = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:path1]options:nil]; 

NSArray *pathComponents = [NSArray arrayWithObjects: 
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],@"MyAudio.m4a",nil]; 

    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents]; 
AVAsset *audioAsset = [AVAsset assetWithURL:outputFileURL]; 

//Create mutable composition of audio type 
AVMutableCompositionTrack *audioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 

[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,video1.duration) 
ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 

    AVMutableCompositionTrack* composedTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

[composedTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, video1.duration) 
ofTrack:[[video1 tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
atTime:kCMTimeZero error:nil]; 


AVAssetExportSession*exporter = [[AVAssetExportSession alloc]initWithAsset:composition presetName:AVAssetExportPresetHighestQuality]; 

[exporter exportAsynchronouslyWithCompletionHandler:^{ 
      case AVAssetExportSessionStatusFailed: 
       NSLog(@"Failed to export video"); 
       break; 
      case AVAssetExportSessionStatusCancelled: 
       NSLog(@"export cancelled"); 
       break; 

} 

對於視頻合併訪問本教程。 http://iosbucket.blogspot.in/2015/04/mp4-conversion-and-video-merging-in-ios.html

您還可以找到用於合併視頻示例項目。