기존 이미지를 가져 오기 위해 AVCaptureSession에 바인딩 된 기존 AVCaptureStillImageOutput을 사용하고 있습니다. 그런 다음 AVAssetWriter에 기록하고 마지막으로 1 초 간격으로 프레임이 채워진 비디오 파일을 가져와야합니다.AVAssetWriter가 변환 값을 무시합니다.
세로 모드의 출력 비디오 크기를 제외한 모든 기능이 정상적으로 작동합니다. 가로 모드의 장치 인 경우 - captureStillImageAsynchronouslyFromConnection은 1920x1080 크기의 CMSampleBuffer를 생성하지만 세로 모드 인 경우 동일한 크기 (1920x1080)의 회전 된 CMSampleBuffer를 계속 생성합니다. AVAssetWriterInput .transform 속성을 사용하여 최종 출력 비디오를 회전 할 수는 있지만 정상적으로 작동하지만 최종 비디오의 크기는 1920x1080입니다. 그러나 1080x1920이어야합니다.
문제는 항상 landscape 차원이있는 captureStillImageAsynchronouslyFromConnection의 CMSampleBuffer에 있음을 알게되었습니다. 그런 다음 AVAssetWriter의 입력은 구성된 너비와 높이를 무시하고 CMSampleBuffer의 크기를 사용합니다.
아무도 그 문제를 해결할 방법을 알고 있습니까?
주의 : 캡처 한 버퍼를 vImage 또는 Core Graphics 기능으로 회전 할 수 있다는 것을 알고 있지만 성능 고려 사항 때문에이 방법을 사용하지 마십시오. 내 문제는
- (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));
}
}];
}
솔루션을 찾았습니까? – tna0y
오리엔테이션이 제대로 감지되는지 확인 했습니까? 제대로 감지되지 않으면 [[UIDevice currentDevice] orientation]을 확인할 수 있습니다. – Rami
가로 프레임의 경우이 문제가 없으며 세로 프레임의 경우에만 문제가 있습니까? 아니면 다른 방향일까요? – uchiha