裁剪录制的视频帧

时间:2014-05-21 14:32:51

标签: ios objective-c avfoundation avassetwriter avvideocomposition

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

- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)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;
}

我尝试裁剪视频但导致更改整个视频帧而不进行裁剪,看起来很紧张。

_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];

有人可以帮我解决视频的裁剪问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:4)

以下是用于裁剪视频的代码。我们需要设置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];