AVFoundation AVAssetExportSession在尝试使用图层指令翻转视频时会生成空白视频

时间:2013-03-08 20:33:02

标签: objective-c avfoundation avassetexportsession

我正在尝试使用AVFoundation框架以正确的方式翻转视频。出于某种原因,下面的代码每次都会产生一个空白视频。

我注意到当我没有为AVAssetExportSession应用videoComposition设置时,它确实传递了视频(它在Photo LIbrary中可见并可播放),但由于未应用图层指令,因此不会翻转视频。所以我假设问题是在传递给合成的图层指令中,但它已正确设置。我错过了什么,或者没有看到什么?

以下是代码:

    NSURL *assetURL = [NSURL fileURLWithPath:path];
    AVAsset *movieAsset = [AVAsset assetWithURL:assetURL];
    NSLog(@"Asset: %0.1f",CMTimeGetSeconds(movieAsset.duration));
    NSLog(@"Asset Preferred Transform %@", NSStringFromCGAffineTransform(movieAsset.preferredTransform));



    //Output Composition
    AVMutableComposition *outputComposition = [[AVMutableComposition alloc] init];
    AVMutableCompositionTrack *videoTrack = [outputComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                           preferredTrackID:kCMPersistentTrackID_Invalid];
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, movieAsset.duration)
                        ofTrack:[[movieAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:kCMTimeZero
                          error:nil];
    [videoTrack setPreferredTransform:CGAffineTransformMakeScale(1, -1)];

    AVMutableVideoCompositionLayerInstruction *videoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
    [videoLayerInstruction setTransform:CGAffineTransformMakeScale(1, -1) atTime:kCMTimeZero];

    AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    videoInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, movieAsset.duration);
    videoInstruction.layerInstructions = [NSArray arrayWithObjects:videoLayerInstruction, nil];


    AVMutableVideoComposition *outputVideoComposition = [AVMutableVideoComposition videoComposition];
    outputVideoComposition.instructions = [NSArray arrayWithObjects:videoInstruction, nil];
    outputVideoComposition.frameDuration = CMTimeMake(1, 30);
    outputVideoComposition.renderSize = CGSizeMake(480, 640);


    //Export
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:outputComposition presetName:AVAssetExportPresetHighestQuality] ;
    exporter.videoComposition = outputVideoComposition;
    NSString *newPath = [documentsDirectory stringByAppendingPathComponent:[self.filePath stringByAppendingString:@"-fb.mov"]];
    [[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
    exporter.outputURL = [NSURL fileURLWithPath:newPath];
    exporter.outputFileType = AVFileTypeQuickTimeMovie;

    NSLog(@"Starting export");



    [exporter exportAsynchronouslyWithCompletionHandler:^(void){

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Video exported");
            NSLog(@"%@", newPath);
            NSURL *newAssetURL = [NSURL fileURLWithPath:newPath];
            AVAsset *newMovieAsset = [AVAsset assetWithURL:newAssetURL];
            NSLog(@"New Asset %@",newMovieAsset);
            NSLog(@"New Asset Duration: %0.1f",CMTimeGetSeconds(newMovieAsset.duration));
            NSLog(@"New Asset Preferred Transform %@", NSStringFromCGAffineTransform(newMovieAsset.preferredTransform));


            UISaveVideoAtPathToSavedPhotosAlbum(newPath, nil, nil, nil);}}];

2 个答案:

答案 0 :(得分:2)

您为outputVideoComposition提供的渲染大小不正确。它应该是根据iPhone屏幕

答案 1 :(得分:0)

我遇到了同样的问题,它正在将视频旋转出导帧。旋转锚的东西在左上角。

要修复,请应用其他转换,例如:

CGAffineTransformMakeTranslation(exportsVideoWidth,exportsVideoHeight);

希望这有帮助,如果您有任何问题,请与我联系。