ios 如何使用 AVFoundation 裁剪视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5198245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:07:15  来源:igfitidea点击:

How do I use AVFoundation to crop a video

iosavfoundation

提问by haider

I'm trying to use AVFoundation to crop videos I'm recording. So lets say i create a AVCaptureVideoPreviewLayer and set the frame to be 300x300.

我正在尝试使用 AVFoundation 来裁剪我正在录制的视频。所以假设我创建了一个 AVCaptureVideoPreviewLayer 并将框架设置为 300x300。

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer     layerWithSession:session];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.delegate = self;
captureVideoPreviewLayer.frame = CGRectMake(0,0, 300, 300);
[previewView.layer addSublayer:captureVideoPreviewLayer];

The user sees the video cropped. I'd like to save the video exactly the way the user is viewing it. Using AVCaptureMovieFileOutput, the video obviously gets saved without cropping. I was considering using a AVCaptureVideoDataOutput to intercept the frames and crop them myself, but I was wondering if there is a more efficient way to do this, perhaps with AVExportSession and using an AVVideoComposition.

用户看到视频被裁剪。我想完全按照用户观看的方式保存视频。使用 AVCaptureMovieFileOutput,视频显然无需裁剪即可保存。我正在考虑使用 AVCaptureVideoDataOutput 来拦截帧并自己裁剪它们,但我想知道是否有更有效的方法来做到这一点,也许使用 AVExportSession 并使用 AVVideoComposition。

Any guidance would be appreciated.

任何指导将不胜感激。

回答by Adam

Soemthing like this. 99% of this code just sets it up to do a custom CGAffineTransform, and then save out the result.

像这样的东西。此代码的 99% 只是将其设置为执行自定义 CGAffineTransform,然后保存结果。

I'm assuming that you want the cropped video to take up full size/width of the output - so that e.g a Scale Affine is the correct solution (you zoom in on the video, giving the effect of having cropped + resized).

我假设您希望裁剪后的视频占据输出的全尺寸/宽度 - 例如,Scale Affine 是正确的解决方案(您放大视频,产生裁剪 + 调整大小的效果)。

AVAsset* asset = // your input

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

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

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

AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform finalTransform = // setup a transform that grows the video, effectively causing a crop
    [transformer setTransform:finalTransform atTime:kCMTimeZero];
    instruction.layerInstructions = [NSArray arrayWithObject:transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];

exporter = [[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = videoComposition;
exporter.outputURL=url3;
exporter.outputFileType=AVFileTypeQuickTimeMovie;

[exporter exportAsynchronouslyWithCompletionHandler:^(void){}];

回答by nibeck

ios7 added a specific Layer instruction just for cropping.

ios7 添加了一个特定的图层指令只是为了裁剪。

videolayerInstruction setCropRectangle:atTime:

_mike

_麦克风