gpt4 book ai didi

ios - 裁剪录制的视频帧

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:43:06 25 4
gpt4 key购买 nike

我正在尝试裁剪视频帧。我使用 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;
}

我尝试裁剪视频,但结果是在没有裁剪的情况下改变了整个视频帧,而且看起来被拉伸(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];

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

最佳答案

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com