我记录使用视频+音频AVAssetWriter
和captureOutput
回调。 问题是,在案件约5%,我无法生成从第二零一缩略图(如果我尝试走得更远视频,那么就没有问题)。 我得到的错误是:
错误域= AVFoundationErrorDomain代码= -11832 “无法打开” 的UserInfo = 0x4b31b0 {NSLocalizedFailureReason =该媒体不能使用,NSUnderlyingError = 0x4effb0 “操作无法完成。(OSStatus错误-12431)”,NSLocalizedDescription =无法打开}
下面是我使用的代码:
AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);
AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
{
if (result != AVAssetImageGeneratorSucceeded)
{
[self NSLogPrint:[NSString stringWithFormat:@"couldn't generate thumbnail, error:%@", error]];
}
UIImage *thumbImg=[[UIImage imageWithCGImage:im] retain];
[image setImage:thumbImg];
[thumbImg release];
[generator release];
};
CGSize maxSize = CGSizeMake(177, 100);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
下面是我如何设置我的视频AVAssetWriterInput:
- (BOOL) setupAssetWriterVideoInput:(CMFormatDescriptionRef)currentFormatDescription
{
float bitsPerPixel;
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(currentFormatDescription);
int numPixels = dimensions.width * dimensions.height;
int bitsPerSecond;
// Assume that lower-than-SD resolutions are intended for streaming, and use a lower bitrate
if ( numPixels < (640 * 480) )
bitsPerPixel = 4.05; // This bitrate matches the quality produced by AVCaptureSessionPresetMedium or Low.
else
bitsPerPixel = 11.04; // This bitrate matches the quality produced by AVCaptureSessionPresetHigh.
bitsPerSecond = numPixels * bitsPerPixel;
NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInteger:dimensions.width], AVVideoWidthKey,
[NSNumber numberWithInteger:dimensions.height], AVVideoHeightKey,
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey,
[NSNumber numberWithInteger:30], AVVideoMaxKeyFrameIntervalKey,
nil], AVVideoCompressionPropertiesKey,
nil];
if ([_videoWriter canApplyOutputSettings:videoCompressionSettings forMediaType:AVMediaTypeVideo])
{
self._videoWriterInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings];
self._videoWriterInput.expectsMediaDataInRealTime = YES;
self._videoWriterInput.transform = [self returnOrientation];
if ([_videoWriter canAddInput:self._videoWriterInput])
[_videoWriter addInput:self._videoWriterInput];
else
{
NSLog(@"Couldn't add asset writer video input.");
return NO;
}
}
else
{
NSLog(@"Couldn't apply video output settings.");
return NO;
}
return YES;
}
我知道,错误意味着有当时没有图像,但为什么会出现这种情况?
我已经使用AVCaptureMovieFileOutput还测试记录和缩略图总是产生。 另外,如果我出口的视频,为相机胶卷,预览还没有可用的照片。
PS:我也读的地方,这可能是与关键帧间隔,但我不知道怎么样。