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

时间:2013-11-19 06:54:19

标签: ios iphone objective-c gpuimage avassetwriter

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

2 个答案:

答案 0 :(得分:2)

可能有更好的方法来执行此操作,但以下代码应该可以正常工作。我假设你正在使用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];

答案 1 :(得分:2)

在不使用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];