IOS AVFoundation - 正方形视频

3

如何更改视频格式?

我正在进行类似以下的操作,将多个视频合并并使用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个回答

3
您可以设置exporter.videoComposition = videoComposition。但我从未尝试过不使用layerInstructions进行传递,不确定它的作用如何。
但是,即使您更改了rendersize,我认为您也无法通过它来裁剪帧,如果这就是您想要实现的目标。理论上,它会将您的帧压缩到该rendersize中。也许我错了,所以请测试一下。
到目前为止,我能找到的最快速的方法是使用AVAssetWriter及其一对键--[NSString stringWithString: AVVideoScalingModeResizeAspectFill],AVVideoScalingModeKey--,它将裁剪到您传递的任何大小,虽然没有裁剪放置的控制。

0

你没有提供用于预览录制视频的AVCaptureVideoPreviewLayer的框架,但我猜测根据问题的标题,它是正方形的。您可以使用此值(或您希望视频最终到达的尺寸)来计算在将每个轨道拼接成整体之前应用于每个轨道的CGAffineTransform。

关于此操作的一个很好的示例,请参见:

https://github.com/carsonmcdonald/iOSVideoCameraMultiStitchExample


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接