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

5
我正在使用AVAssetWritercaptureOutput回调来录制视频和音频。问题是,在大约5%的情况下,我无法从第0秒生成缩略图(如果我尝试在视频中往后走,则没有问题)。我收到的错误信息如下:

Error Domain=AVFoundationErrorDomain Code=-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进行录制,并且缩略图总是生成的。 如果我将视频导出到相机胶卷中,在照片中也无法预览。
附注:我还在某个地方读到过,这可能与关键帧间隔有关,但我不确定如何处理。
1个回答

0

我解决了我的问题。我之前使用的是iOS 4.2.1,后来升级到了iOS 5.1.1,问题就消失了。


1
偶尔我会在iOS 8.3上看到这个。 - Alex
我在 iOS 10.3 中一直看到这个问题。 - raisedandglazed
10.3.3也有问题。 - zszen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接