首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAssetWriter忽略变换值

AVAssetWriter忽略变换值
EN

Stack Overflow用户
提问于 2014-04-06 18:13:09
回答 1查看 1.5K关注 0票数 6

我正在使用绑定到AVCaptureSession的现有AVCaptureStillImageOutput来获取静态图像。然后我需要将它们写入AVAssetWriter,最后以1秒的间隔获得充满帧的视频文件。

一切正常,除了在纵向模式下输出视频尺寸。当设备处于横向模式时-一切都很好,因为captureStillImageAsynchronouslyFromConnection生成尺寸为1920x1080 (例如)的CMSampleBuffer,但当设备处于纵向模式时,它仍然会生成相同尺寸(1920x1080)的旋转CMSampleBuffer。我可以旋转AVAssetWriterInput的.transform属性的最终输出视频,它工作得很好,但最终视频有错误的尺寸(1920x1080),但应该是1080x1920。

我发现这个问题出在captureStillImageAsynchronouslyFromConnection's CMSampleBuffer中,它总是有横向维度。然后,AVAssetWriter的输入忽略配置的宽度和高度,并使用CMSampleBuffer的尺寸。

有谁知道怎么解决这个问题吗?

注意:我知道可以使用vImage或核心图形函数来旋转捕获的缓冲区,但出于性能考虑,我希望避免使用这种方法。我的问题看起来像是iOS中的配置问题或错误...

代码语言:javascript
复制
- (void) setupLongLoopWriter
{
self.currentFragment.filename2 = [VideoCapture getFilenameForNewFragment];
NSString *path = [NSString stringWithFormat:@"%@/%@", [GMConfig getVideoCapturesPath], self.currentFragment.filename2];

CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(self.currentCamera.activeFormat.formatDescription);

CGSize videoWriterFrameSize = CGSizeMake(dimensions.width, dimensions.height);

float rotationAngle = 0.0;

switch ((UIDeviceOrientation)[self.currentFragment.orientation unsignedIntValue])
{
    case UIDeviceOrientationUnknown:
    case UIDeviceOrientationPortrait:
    case UIDeviceOrientationFaceUp:
    case UIDeviceOrientationFaceDown:
        rotationAngle = DEGREES_TO_RADIANS(90);
        videoWriterFrameSize = CGSizeMake(dimensions.height, dimensions.width);
        break;

    case UIDeviceOrientationPortraitUpsideDown:
        rotationAngle = DEGREES_TO_RADIANS(-90.0);
        videoWriterFrameSize = CGSizeMake(dimensions.height, dimensions.width);
        break;

    case UIDeviceOrientationLandscapeLeft:
        rotationAngle = 0.0;
        break;

    case UIDeviceOrientationLandscapeRight:
        rotationAngle = DEGREES_TO_RADIANS(180.0);
        break;
}

    //  NSLog(@"%.0fx%.0f", videoWriterFrameSize.width, videoWriterFrameSize.height);

NSError *error = nil;

self.currentFragment.longLoopWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:path]
                                                       fileType:AVFileTypeQuickTimeMovie
                                                          error:&error];
NSParameterAssert(self.currentFragment.longLoopWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               AVVideoScalingModeResizeAspect, AVVideoScalingModeKey,
                               [NSNumber numberWithInt:videoWriterFrameSize.width], AVVideoWidthKey,
                               [NSNumber numberWithInt:videoWriterFrameSize.height], AVVideoHeightKey,
                               nil];

AVAssetWriterInput* writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo
                                                                     outputSettings:videoSettings];
writerInput.expectsMediaDataInRealTime = YES;

if (rotationAngle != 0.0)
    writerInput.transform = CGAffineTransformMakeRotation (rotationAngle);


NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                                       [NSNumber numberWithInt:kCVPixelFormatType_32ARGB], kCVPixelBufferPixelFormatTypeKey, nil];

self.currentFragment.longLoopWriterAdaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];

NSParameterAssert(writerInput);
NSParameterAssert([self.currentFragment.longLoopWriter canAddInput:writerInput]);


[self.currentFragment.longLoopWriter addInput:writerInput];

if( [self.currentFragment.longLoopWriter startWriting] == NO )
    NSLog(@"Failed to start long loop writing!");

[self.currentFragment.longLoopWriter startSessionAtSourceTime:kCMTimeZero];
}

- (void) captureLongLoopFrame
{
if ([GMConfig sharedConfig].longLoopFrameDuration == 0.0) {
    [self.longLoopCaptureTimer invalidate];
    self.longLoopCaptureTimer = nil;

    return;
}

if (self.captureSession.isRunning == NO || self.currentFragment.longLoopWriterAdaptor == nil)
    return;

[self.shutterOutput captureStillImageAsynchronouslyFromConnection:[self.shutterOutput.connections objectAtIndex:0]
                                                completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
    if (imageDataSampleBuffer != NULL && error == nil) {
        /*
        CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(CMSampleBufferGetFormatDescription(imageDataSampleBuffer));
        CGSize videoWriterFrameSize = CGSizeMake(dimensions.width, dimensions.height);

        NSLog(@"Image buffer size: %.0fx%.0f", videoWriterFrameSize.width, videoWriterFrameSize.height);
        */

        double offset = [[NSDate date] timeIntervalSinceDate:self.currentFragment.start];
        CMTime presentationTime = CMTimeMake(offset*1000, 1000);

        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);




        if (self.currentFragment.longLoopWriterAdaptor.assetWriterInput.readyForMoreMediaData == YES) {
            if( [self.currentFragment.longLoopWriterAdaptor appendPixelBuffer:imageBuffer withPresentationTime:presentationTime] == NO ) {
                NSLog(@"Error adding frame to long loop!");
            }
        }

        NSLog(@"Long loop updated at %0.1f", CMTimeGetSeconds(presentationTime));
    }
}];
}
EN

回答 1

Stack Overflow用户

发布于 2016-03-17 22:31:38

我只在纵向模式下录制,通过使用AVCaptureConnection上的setVideoOrientation直接从相机获取旋转的像素数据以传递给AVAssetWriter (参见https://developer.apple.com/library/ios/qa/qa1744/_index.html),我能够避免这个问题。

代码语言:javascript
复制
for (AVCaptureConnection *connection in [videoOutput connections] ) {
            for ( AVCaptureInputPort *port in [connection inputPorts] ) {
                if ( [[port mediaType] isEqual:AVMediaTypeVideo] ) {

                    [connection setVideoOrientation:AVCaptureVideoOrientationPortrait];

                }
            }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22892648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档