2011-07-04 26 views
27

我想將幾個視頻剪輯合併爲一個使用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*

任何幫助,然後使用不勝感激!

回答

25

這就是我所做的。然後我使用AVAssetExportSession創建實際文件。但是我警告你,CGAffineTransforms有時會延遲應用,所以在視頻轉換之前你會看到一兩個原始圖像。我不知道爲什麼會發生這種情況,不同的視頻組合會產生預期的結果,但有時會出現這種情況。

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

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; videoComposition.frameDuration = CMTimeMake(1,30); videoComposition.renderScale = 1.0; 

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack]; 

// Get only paths the user selected NSMutableArray *array = [NSMutableArray array]; for(NSString* string in videoPathArray){ 
if(![string isEqualToString:@""]){ 
    [array addObject:string]; 
} 

self.videoPathArray = array; 

float time = 0; 

for (int i = 0; i<self.videoPathArray.count; i++) { 

    AVURLAsset *sourceAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:[videoPathArray objectAtIndex:i]] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]]; 

    NSError *error = nil; 

    BOOL ok = NO; 
    AVAssetTrack *sourceVideoTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 

    CGSize temp = CGSizeApplyAffineTransform(sourceVideoTrack.naturalSize, sourceVideoTrack.preferredTransform); 
    CGSize size = CGSizeMake(fabsf(temp.width), fabsf(temp.height)); 
    CGAffineTransform transform = sourceVideoTrack.preferredTransform; 

    videoComposition.renderSize = sourceVideoTrack.naturalSize; 
    if (size.width > size.height) { 
     [layerInstruction setTransform:transform atTime:CMTimeMakeWithSeconds(time, 30)]; 
    } else { 

     float s = size.width/size.height; 

     CGAffineTransform new = CGAffineTransformConcat(transform, CGAffineTransformMakeScale(s,s)); 

     float x = (size.height - size.width*s)/2; 

     CGAffineTransform newer = CGAffineTransformConcat(new, CGAffineTransformMakeTranslation(x, 0)); 

     [layerInstruction setTransform:newer atTime:CMTimeMakeWithSeconds(time, 30)]; 
    } 

    ok = [compositionVideoTrack insertTimeRange:sourceVideoTrack.timeRange ofTrack:sourceVideoTrack atTime:[composition duration] error:&error]; 

    if (!ok) { 
     // Deal with the error. 
     NSLog(@"something went wrong"); 
    } 

    NSLog(@"\n source asset duration is %f \n source vid track timerange is %f %f \n composition duration is %f \n composition vid track time range is %f %f",CMTimeGetSeconds([sourceAsset duration]), CMTimeGetSeconds(sourceVideoTrack.timeRange.start),CMTimeGetSeconds(sourceVideoTrack.timeRange.duration),CMTimeGetSeconds([composition duration]), CMTimeGetSeconds(compositionVideoTrack.timeRange.start),CMTimeGetSeconds(compositionVideoTrack.timeRange.duration)); 

    time += CMTimeGetSeconds(sourceVideoTrack.timeRange.duration); 
    } 

instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; instruction.timeRange = compositionVideoTrack.timeRange; 

videoComposition.instructions = [NSArray arrayWithObject:instruction]; 

這就是我所做的。然後我使用AVAssetExportSession創建實際文件。但是我警告你,CGAffineTransforms有時會延遲應用,所以在視頻轉換之前你會看到一兩個原始圖像。我不知道爲什麼會發生這種情況,不同的視頻組合會產生預期的結果,但有時會出現這種情況。

+0

它似乎工作,但是我沒有遇到後來應用的變換問題 – Song

+2

奇怪。我沒有試圖合併兩個文件,我只是想讓AVAssetExportSession保持視頻方向。你應該只能調用'[compositionVideoTrack setPreferredTransform:transform]',但它不起作用。使用你的方法也不適合我。但使用*兩個*確實有效。聽起來像一個框架錯誤。我也在使用導出會話設置'AVAssetExportPresetPassthrough'。 – Dex

+0

我的時間也很奇怪。通常情況下,最後1秒左右的視頻會被截斷。 – Dex

相關問題