繁体   English   中英

如何使用带有IOS SDK的AVAssetWriter创建Square Video?

[英]How to create Square Video using AVAssetWriter with IOS SDK?

我想使用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];

没有使用GPUImageCropFilter的方法是最简单的方法

gpuimagevideocamera.m

 -(void)processVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer{
if (capturePaused)return;
CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
int bufferWidth ; 
int bufferHeight; 
bufferHeight=480;
bufferWidth=480;
CMTime currentTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);----etc}

您的.m文件

 GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc]
               initWithSessionPreset:AVCaptureSessionPreset640x480
               cameraPosition:AVCaptureDevicePositionBack];

GpuimageMovieWriter.m

outputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                  AVVideoCodecH264, AVVideoCodecKey,
                  [NSNumber numberWithInt:640.0], AVVideoWidthKey,
                  [NSNumber numberWithInt:640.0], AVVideoHeightKey,
                  AVVideoScalingModeResizeAspectFill,AVVideoScalingModeKey,nil];



assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:outputSettings];
assetWriterVideoInput.expectsMediaDataInRealTime = _encodingLiveVideo;

// You need to use BGRA for the video in order to get realtime encoding. I use a color-swizzling shader to line up glReadPixels' normal RGBA output with the movie input's BGRA.
NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey,
                                                       [NSNumber numberWithInt:640], kCVPixelBufferWidthKey,
                                                       [NSNumber numberWithInt:640], kCVPixelBufferHeightKey,
                                                       nil];
//    NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictiona ry dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey,
//                                                           nil];

assetWriterPixelBufferInput = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:assetWriterVideoInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];

[assetWriter addInput:assetWriterVideoInput];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM