将景观视频剪裁成肖像视频

时间:2015-07-17 15:27:36

标签: ios avfoundation video-processing

我想裁剪横向视频的异常值,使其具有纵向视频的尺寸比率。

我正在读这篇文章:How do I use AVFoundation to crop a video

但我不确定如何设置变换 - 这个答案现在已经超过四年了,所以我不确定它如何应用于今天可用的更大屏幕尺寸。 TNX!

1 个答案:

答案 0 :(得分:0)

这似乎可以解决问题:

AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
float width = 1080.0/1920.0 * size.height;
videoComposition.renderSize = CGSizeMake(width, size.height);
videoComposition.frameDuration = CMTimeMake(1, 30);

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );

AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform translate = CGAffineTransformMakeTranslation((width - size.width)/2, 0);
translate = CGAffineTransformScale(translate, 1, 1);

[transformer setTransform:translate atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject:transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];

encoder.videoComposition = videoComposition;