2

我正在尝试使用 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);}}];
4

2 回答 2

2

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

于 2013-03-14T05:47:34.587 回答
0

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

要修复,请应用另一个转换,如下所示:

CGAffineTransformMakeTranslation(exportingVideoWidth, exportingVideoHeight);

希望这会有所帮助,如果您有任何问题,请告诉我。

于 2013-03-19T06:52:28.627 回答