从使用AVAssetWriter创建的视频生成缩略图

Diu*_*eus 5 iphone video avfoundation ios avassetwriter

我正在使用AVAssetWritercaptureOutput回调来录制视频和音频。问题在于,在大约5%的情况下,我无法从第二个零开始生成缩略图(如果我尝试在视频中走得更远,则没有问题)。我得到的错误是:

错误域= AVFoundationErrorDomain代码= -11832“无法打开” UserInfo = 0x4b31b0 {NSLocalizedFailureReason =无法使用该介质。NSUnderlyingError = 0x4effb0“操作无法完成。(OSStatus错误-12431。)” }

这是我正在使用的代码:

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];
Run Code Online (Sandbox Code Playgroud)

这是我设置视频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;
}
Run Code Online (Sandbox Code Playgroud)

我知道该错误意味着当时没有图像,但是为什么会这样呢?

我还使用AVCaptureMovieFileOutput测试了录制,并且始终会生成缩略图。另外,如果我将视频导出到相机胶卷,则“照片”中也不会提供预览。

PS:我也读过一些地方,这可能与关键帧间隔有关,但我不确定如何做。

小智 0

我解决了我的问题。我在 iOS 4.2.1 上运行,决定升级到 iOS 5.1.1,问题就消失了。