我想將幾個視頻剪輯合併爲一個使用AVFoundation。 我可以在下面如何使用AVFoundation結合具有不同方向的視頻剪輯
AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime startTime = kCMTimeZero;
/*videoClipPaths is a array of paths of the video clips recorded*/
//for loop to combine clips into a single video
for (NSInteger i=0; i < [videoClipPaths count]; i++) {
NSString *path = (NSString*)[videoClipPaths objectAtIndex:i];
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
[url release];
AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
//set the orientation
if(i == 0)
{
[compositionVideoTrack setPreferredTransform:videoTrack.preferredTransform];
}
ok = [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:videoTrack atTime:startTime error:nil];
ok = [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, [asset duration]) ofTrack:audioTrack atTime:startTime error:nil];
startTime = CMTimeAdd(startTime, [asset duration]);
}
//export the combined video
NSString *combinedPath = /* path of the combined video*/;
NSURL *url = [[NSURL alloc] initFileURLWithPath: combinedPath];
AVAssetExportSession *exporter = [[[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPreset640x480] autorelease];
exporter.outputURL = url;
[url release];
exporter.outputFileType = [[exporter supportedFileTypes] objectAtIndex:0];
[exporter exportAsynchronouslyWithCompletionHandler:^(void){[self combineVideoFinished:exporter.outputURL status:exporter.status error:exporter.error];}];
創建使用該代碼使用AVMutableComposition一個視頻,如果所有的視頻片段被記錄在同一方向(縱向或橫向)上面的代碼工作正常。但是,如果我在剪輯中混合了方向,則最終的視頻會將其一部分向右(或向左)旋轉90度。
我想知道是否有一種方法可以在創作時將所有剪輯轉換爲相同的方向(例如第一個剪輯的方向)。從我在XCode的文檔AVMutableVideoCompositionLayerInstruction
似乎可以用來轉化AVAsset
閱讀,但我不知道如何創建和幾個不同層的指令適用於相應的剪輯,並在成分(AVMutableComposition*
)
任何幫助,然後使用不勝感激!
它似乎工作,但是我沒有遇到後來應用的變換問題 – Song
奇怪。我沒有試圖合併兩個文件,我只是想讓AVAssetExportSession保持視頻方向。你應該只能調用'[compositionVideoTrack setPreferredTransform:transform]',但它不起作用。使用你的方法也不適合我。但使用*兩個*確實有效。聽起來像一個框架錯誤。我也在使用導出會話設置'AVAssetExportPresetPassthrough'。 – Dex
我的時間也很奇怪。通常情況下,最後1秒左右的視頻會被截斷。 – Dex