【问题标题】:How do I use AVFoundation to crop a video如何使用 AVFoundation 裁剪视频
【发布时间】:2011-07-09 01:37:37
【问题描述】:

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

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

任何指导将不胜感激。

【问题讨论】:

  • 您能否让输出看起来像预览层一样清晰?这是一个问题,匹配 AVLayerVideoGravityResizeAspectFill 的锐度。

标签: ios avfoundation


【解决方案1】:

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

我假设您希望裁剪后的视频占据输出的全尺寸/宽度 - 例如,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){}];

【讨论】:

  • 亚当,谢谢你的帖子。我实际上就是这样做的,并打算发布答案,但你打败了我!不管怎样,谢谢你的帮助......我认为其他人肯定会受益。
  • 您能否详细说明获取 AVAsset 输入。您是否在使用 AVCaptureMovieFileOutput 保存电影文件后对其运行?
  • AVAsset 是 Apple 的包罗万象的类,它包装了您可以提供给 AVFoundation 的任何类型的输入。在我的脑海中,您想查看 AVF 中存在的各种子类(转到 Apple 的任何 AVF 文档页面,然后单击任何指向“类参考”的链接,您将获得完整列表)-它们都被命名为“AV[something]Asset”IIRC。
  • 我们能否举一个“使视频增长的变换,有效地导致裁剪”的示例?
  • @AnkitKumarGupta 你有一个有效的例子——你回复了它。如果您有不同的问题,请提出一个新问题。这就是这个网站的目的。
【解决方案2】:

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

videolayerInstruction setCropRectangle:atTime:

_麦克

【讨论】:

  • 这没有提供预期的结果。供参考。此设置将从原始视频像素大小裁剪一个矩形。与设备上的屏幕尺寸无关。
  • @nibeck,您能否提供一个代码块来演示如何使用它?
  • 我试过这个。但对我不起作用。不得不切换回翻译转换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 2013-09-13
相关资源
最近更新 更多