• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 裁剪录制的视频帧

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:56:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试裁剪视频帧。我使用 AVFoundation 框架录制了视频。我需要将拍摄的图像和录制的视频裁剪成方形。我已经为 UIImage 工作了,现在我正在尝试裁剪视频。

- (UIImage *)squareImageWithImageUIImage *)image scaledToSizeCGSize)newSize {
  // Cropping UIImage is working fine.
  CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0,0, image.size.width, image.size.width));
  UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
  CGImageRelease(imageRef);
  return result;
}

我尝试裁剪视频,但结果是更改了整个视频帧而不进行裁剪,并且看起来被拉伸(stretch)了。

_writer = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeQuickTimeMovie error:nil];
NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
                          AVVideoCodecH264, AVVideoCodecKey,
                          [NSNumber numberWithInt: 320], AVVideoWidthKey,
                          [NSNumber numberWithInt: 320], AVVideoHeightKey,
                          nil];
_videoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings];
[_writer addInput:_videoInput];

谁能帮我解决这个视频裁剪问题?提前致谢。



Best Answer-推荐答案


下面是我用来裁剪视频的代码。我们需要设置 AVVideoCompressionPropertiesKey,其中包含 AVVideoCleanApertureKey 键用于视频设置。

我推荐了:How to crop video into square iOS with AVAssetWriter

NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithInt:cx], AVVideoCleanApertureWidthKey,
                                            [NSNumber numberWithInt:cx], AVVideoCleanApertureHeightKey,
                                            [NSNumber numberWithInt:10], AVVideoCleanApertureHorizontalOffsetKey,
                                            [NSNumber numberWithInt:10], AVVideoCleanApertureVerticalOffsetKey,
                                            nil];
NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [NSNumber numberWithInt:3], AVVideoPixelAspectRatioHorizontalSpacingKey,
                                          [NSNumber numberWithInt:3],AVVideoPixelAspectRatioVerticalSpacingKey,
                                          nil];
NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithInt:960000], AVVideoAverageBitRateKey,
                               [NSNumber numberWithInt:1],AVVideoMaxKeyFrameIntervalKey,
                               videoCleanApertureSettings, AVVideoCleanApertureKey,
                               //videoAspectRatioSettings, AVVideoPixelAspectRatioKey,
                               //AVVideoProfileLevelH264Main30, AVVideoProfileLevelKey,
                               nil];
NSString *targetDevice = [[UIDevice currentDevice] model];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoScalingModeResizeAspectFill,AVVideoScalingModeKey,
                               AVVideoCodecH264, AVVideoCodecKey,
                               codecSettings,AVVideoCompressionPropertiesKey,
                               [NSNumber numberWithInt:cx], AVVideoWidthKey,
                               [NSNumber numberWithInt:cx], AVVideoHeightKey,
                               nil];
_videoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];

关于ios - 裁剪录制的视频帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786292/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap