2012-04-23 32 views
2

我使用下面的代碼,試圖合併存儲在文件夾2個M4V文件:合併兩個M4V電影文件使用AVMutableComposition - 影片不會合並

CMTime insertionPoint = kCMTimeZero; 
NSError * error = nil; 

AVMutableComposition *composition = [AVMutableComposition composition]; 

AVURLAsset* asset = [AVURLAsset URLAssetWithURL: [assetURLArray objectForKey:kIntroVideo] options:nil]; 

if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) 
          ofAsset:asset 
          atTime:insertionPoint 
          error:&error]) 
{ 
    NSLog(@"error: %@",error); 
} 

insertionPoint = CMTimeAdd(insertionPoint, asset.duration); 

AVURLAsset* asset2 = [AVURLAsset URLAssetWithURL: [assetURLArray objectForKey:kMainVideo] options:nil]; 

if (![composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset2.duration) 
          ofAsset:asset2 
          atTime:insertionPoint 
          error:&error]) 
{ 
    NSLog(@"error: %@",error); 
} 

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

NSString *exportVideoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/FinishedVideo.m4v"]; 

NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath]; 
exportSession.outputURL = exportURL; 
exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
[exportSession exportAsynchronouslyWithCompletionHandler:^{ 
    switch (exportSession.status) { 
     case AVAssetExportSessionStatusFailed:{ 
      NSLog (@"FAIL"); 
      break; 
     } 
     case AVAssetExportSessionStatusCompleted: { 
      NSLog (@"SUCCESS"); 
} 
}; 
}]; 
} 

的問題是,兩個視頻不會合並正確。整個合併的電影持續時間是正確的,但是視頻永遠不會轉換到第二部電影,並在其持續時間內繼續顯示第一部電影的最後一幀。奇怪的是,我可以聽到背景中播放的第二個視頻的音頻。

有沒有人有任何想法是什麼錯?

編輯 - 奇怪的是,如果我合併兩個完全相同長度的剪輯,它的作品。

編輯 - 試圖改變文件擴展名爲.mov與同樣的問題。

+0

任何想法都將不勝感激。 – GuybrushThreepwood 2012-04-24 09:12:19

+0

您是否嘗試過使用不同的視頻文件?另外,當你說它與兩個相同長度的剪輯一起工作時,那些實際上是兩個不同的剪輯,還是它們是同一剪輯的兩個實例? – Sean 2012-04-24 15:13:20

+0

肖恩 - 感謝您的幫助 - 同一片段的兩個實例。 – GuybrushThreepwood 2012-04-24 15:42:02

回答

1

好的 - 所以我最終通過使用單個AVMutableComposition曲目,然後爲音頻設置了一個可變組合,併爲視頻設置了一個可變組合。

+0

你能告訴我如何將asseturl存入assetURLArray @ Ohnomycoco – morroko 2013-12-30 11:08:13

+0

我有這個問題。但與您的不同,第二項資產保持空白。所以,如果我添加兩個5秒的視頻。 10秒的新視頻已成功導出,但最後5秒顯示空白視頻。有什麼建議麼。 – Nil 2014-09-04 10:44:09

1

你還沒有將組合設置爲exportSession。

行後:

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

添加此行

exportSession.videoComposition = composition;

這應該解決您的問題。