IOS AVFoundation - 方形视频

时间:2013-09-11 13:14:11

标签: ios video avfoundation

更改视频格式的好方法是什么?

我正在做这样的事情来合并多个视频togheter并使用AVAssetExportSession导出它们:

AVMutableComposition *mixComposition = [AVMutableComposition composition];
    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    NSError * error = nil;
    AVURLAsset *assetClip;
    AVAssetTrack *clipVideoTrack;
    AVAssetTrack *clipAudioTrack;
    for (int i = [clipPaths count]-1; i >= 0; i--) {
        assetClip = [AVURLAsset URLAssetWithURL:[clipPaths objectAtIndex:i] options:nil];
        clipVideoTrack = [[assetClip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
        clipAudioTrack = [[assetClip tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

        [compositionVideoTrack insertTimeRange:clipVideoTrack.timeRange ofTrack:clipVideoTrack atTime:kCMTimeZero error:&error];
        [compositionAudioTrack insertTimeRange:clipAudioTrack.timeRange ofTrack:clipAudioTrack atTime:kCMTimeZero error:&error];
    } 

    NSString *cachesFolder = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"/"];
    NSString *finalPath = [cachesFolder stringByAppendingPathComponent:[self.currentFileName stringByAppendingString:@".mov"]];

    // Remove video file if it exists 
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:finalPath]) {
        [fileManager removeItemAtPath:finalPath error:&error];
    }

    NSURL *lastURL = [NSURL fileURLWithPath:finalPath];

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    exporter.outputURL = lastURL;
    [exporter exportAsynchronouslyWithCompletionHandler:^(void){
        switch(exporter.status) {
            case AVAssetExportSessionStatusFailed:
                NSLog(@"exporting failed"); 
                break;
            case AVAssetExportSessionStatusCompleted:
                NSLog(@"exporting completed"); 
                [[NSNotificationCenter defaultCenter] postNotificationName:@"didCompleteMovieMerging" object:nil];
                break;
            case AVAssetExportSessionStatusCancelled:
                NSLog(@"export cancelled");
                break;
        }
    }];

我看到你可以使用

AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(320, 320);
videoComposition.frameDuration = CMTimeMake(1, 30);

而不是AVAssetExportSession,但我找不到让这两种方法协同工作的方法。

2 个答案:

答案 0 :(得分:3)

您可以设置exporter.videoComposition = videoComposition。我从来没有试过没有layerInstructions传递它,但不知道它会如何行动。

Buuut即使你改变渲染,我也不认为你可以用它来裁剪框架,如果这是你想要实现的。从理论上讲,它会将你的框架挤压成渲染。也许我错了所以测试它。

到目前为止,我能找到的最快的方法是使用AVAssetWriter及其一对键 - [NSString stringWithString:AVVideoScalingModeResizeAspectFill],AVVideoScalingModeKey - 将裁剪为您传递的任何大小,但不控制裁剪位置< / p>

答案 1 :(得分:0)

您没有提供AVCaptureVideoPreviewLayer的框架,您可以在录制视频时预览视频,但我从问题的标题中猜测它的正方形。您可以使用此值(或任何您希望视频结束的尺寸)来计算CGAffineTransform,以便在拼接成整个曲目之前应用于每个曲目。

有关此操作的一个很好的示例,请参阅:

https://github.com/carsonmcdonald/iOSVideoCameraMultiStitchExample