IOS在视频之间添加过渡

时间:2012-11-13 10:55:15

标签: ios video transition avassetexportsession avmutablecomposition

我正在尝试通过组合两个视频来创建视频

在我尝试在视频之间添加转换之前,它正常工作

它为我提供了一个无法播放的视频文件。

代码:

AVMutableComposition* mixComposition = [AVMutableComposition composition];

    NSString* a_inputFileName = @"v1.mp4";
    NSString* a_inputFilePath = [NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,a_inputFileName];
    NSURL *    a_inputFileUrl = [NSURL fileURLWithPath:a_inputFilePath];

    NSString* b_inputFileName = @"v2.mp4";
    NSString* b_inputFilePath = [NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,b_inputFileName];
    NSURL*    b_inputFileUrl = [NSURL fileURLWithPath:b_inputFilePath];

    NSString* outputFileName = @"outputFile.mp4";
    NSString* outputFilePath = [NSString stringWithFormat:@"%@/%@",documentsDirectoryPath,outputFileName];
    NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
        [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];



    CMTime nextClipStartTime = kCMTimeZero;

    AVURLAsset* a_videoAsset = [[AVURLAsset alloc]initWithURL:a_inputFileUrl options:nil];
    CMTimeRange a_timeRange = CMTimeRangeMake(kCMTimeZero,a_videoAsset.duration);
    AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [a_compositionVideoTrack insertTimeRange:a_timeRange ofTrack:[[a_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

    nextClipStartTime = CMTimeAdd(nextClipStartTime, a_timeRange.duration);
    nextClipStartTime = CMTimeSubtract(nextClipStartTime, CMTimeMake(1, 1));

    AVURLAsset* b_videoAsset = [[AVURLAsset alloc]initWithURL:b_inputFileUrl options:nil];
    CMTimeRange b_timeRange = CMTimeRangeMake(kCMTimeZero, b_videoAsset.duration);
    [a_compositionVideoTrack insertTimeRange:b_timeRange ofTrack:[[b_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

    AVMutableVideoCompositionInstruction * instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    instruction.timeRange = CMTimeRangeMake(kCMTimeZero, mixComposition.duration);

    AVMutableVideoCompositionLayerInstruction * firstlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:[[a_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]];
    [firstlayerInstruction setOpacityRampFromStartOpacity:1.0f toEndOpacity:0.0f timeRange:CMTimeRangeMake(CMTimeMake(3,1), CMTimeMake(1, 1))];


    AVMutableVideoCompositionLayerInstruction * secondlayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:[[b_videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]];
    [secondlayerInstruction setOpacity:1.0f atTime:kCMTimeZero];

    instruction.layerInstructions = [NSArray arrayWithObjects:firstlayerInstruction,secondlayerInstruction, nil];

    AVMutableVideoComposition * videoComp = [AVMutableVideoComposition videoComposition];
    videoComp.instructions = [NSArray arrayWithObject:instruction];
    [videoComp setRenderSize:CGSizeMake(320, 480)];
    [videoComp setFrameDuration:CMTimeMake(1, 30)];       


    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
    _assetExport.videoComposition = videoComp;
    _assetExport.outputFileType = AVFileTypeMPEG4;
    _assetExport.outputURL = outputFileUrl;

    [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {
         [self saveVideoToAlbum:outputFilePath];
     }
     ];

如果我删除此行:_assetExport.videoComposition = videoComp; 它工作正常,但视频之间没有过渡。

编辑: 我发现exportsession的状态一直停留在导出nd它永远不会完成,我不知道为什么。

提前THX。

0 个答案:

没有答案