![](/img/trans.png)
[英]AVAssetWriter startSessionAtSourceTime not accepting CMTIme value
[英]AVAssetWriter ignores transform value
我正在使用綁定到AVCaptureSession的現有AVCaptureStillImageOutput來獲取靜態圖像。 然后我需要將它們寫入AVAssetWriter,最后以1秒的間隔獲得填充幀的視頻文件。
除了在縱向模式下輸出視頻尺寸外,一切正常。 當設備處於橫向模式時 - 一切都很好,因為captureStillImageAsynchronouslyFromConnection會生成尺寸為1920x1080(例如)的CMSampleBuffer,但是當設備處於縱向模式時,它仍會生成具有相同尺寸(1920x1080)的旋轉CMSampleBuffer。 我可以使用AVAssetWriterInput的.transform屬性旋轉最終輸出視頻,它工作正常,但最終視頻的尺寸錯誤(1920x1080),但應該是1080x1920。
我發現問題出在captureStillImageAsynchronouslyFromConnection的CMSampleBuffer中,它始終具有橫向尺寸。 然后AVAssetWriter的輸入忽略配置的寬度和高度並使用CMSampleBuffer的尺寸。
有人知道如何解決這個問題嗎?
注意:我知道可以使用vImage或Core Graphics函數旋轉捕獲的緩沖區,但出於性能考慮,我想避免使用此方法。 我的問題看起來像配置問題或iOS中的錯誤...
- (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));
}
}];
}
我只以縱向模式錄制,我可以通過使用AVCaptureConnection上的setVideoOrientation直接從攝像機獲取旋轉像素數據來傳遞給AVAssetWriter來避免此問題(請參閱https://developer.apple.com/library/ios/qa/ qa1744 / _index.html )
for (AVCaptureConnection *connection in [videoOutput connections] ) {
for ( AVCaptureInputPort *port in [connection inputPorts] ) {
if ( [[port mediaType] isEqual:AVMediaTypeVideo] ) {
[connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.