gpt4 book ai didi

ios - 如何使用带有 IOS SDK 的 AVAssetWriter 创建方形视频?

转载 作者:行者123 更新时间:2023-11-28 19:06:24 24 4
gpt4 key购买 nike

我想在 IOS SDK 中使用 AVAssetWriter(GPUImageMovieWriter) 创建方形视频。如果我将视频输出设置调整为 640*640,我得到的结果是 640*640 大小的视频,分辨率为 480*640。如何更改预设用于方形视频输出。请帮助我。

最佳答案

可能有更好的方法来做到这一点,但下面的代码应该可以工作。我假设您正在使用 Brad Larson 的 GPUImage 框架。请注意,此代码将保存一个 480x480 方形视频。

//Square cropping
UIImage *testImage = [filter imageFromCurrentlyProcessedOutput];
double camWidth = testImage.size.width;
double camHeight = testImage.size.height;
double offset;
double normalizedOffset;
CGRect normalizedCropFrame;

if(camWidth > camHeight) {
offset = (camWidth - camHeight) / 2.0;
normalizedOffset = offset / camWidth;
normalizedCropFrame = CGRectMake(normalizedOffset, 0.0, camHeight/camWidth, 1.0);
}
else {
offset = (camHeight - camWidth) / 2.0;
normalizedOffset = offset / camHeight;
normalizedCropFrame = CGRectMake(0.0, normalizedOffset, 1.0, camWidth/camHeight);
}
squareCrop = [[GPUImageCropFilter alloc] initWithCropRegion:normalizedCropFrame];

//pause camera while setting up movie writer
[videoCamera pauseCameraCapture];

//set up movie writer
pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [[NSURL alloc] initFileURLWithPath:pathToMovie];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 480.0)];

//start recording
[filter addTarget:squareCrop];
[squareCrop addTarget:movieWriter];
videoCamera.audioEncodingTarget = movieWriter;
[movieWriter startRecording];
[videoCamera resumeCameraCapture];

关于ios - 如何使用带有 IOS SDK 的 AVAssetWriter 创建方形视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065101/

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