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

标签 iphone ios video avfoundation avassetwriter

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

Error Domain=AVFoundationErrorDomain Code=-11832 "Cannot Open" UserInfo=0x4b31b0 {NSLocalizedFailureReason=This media cannot be used., NSUnderlyingError=0x4effb0 "The operation couldn’t be completed. (OSStatus error -12431.)", NSLocalizedDescription=Cannot Open}

这是我使用的代码:

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 测试了录制,并且始终会生成缩略图。 此外,如果我将视频导出到相机胶卷,则预览也无法在照片中使用。

P.S:我也在某处读到这可能与关键帧间隔有关,但我不确定如何。

最佳答案

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

关于iphone - 从使用 AVAssetWriter 创建的视频生成缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11485608/

相关文章:

ios - Restkit — 如何映射具有可变键名的 json 对象

java - 使用 Java 运行视频文件(不在 JPanel 中)

css - 带有自定义控件的 HTML5 视频

iphone - 如何将ical添加到iPhone应用程序

iphone - iOS:从另一个 View 将新单元格添加到表格 View

iphone - 如何获取 UITableView 的可见部分?

ios - Realm 对象服务器 - 何时进行同步?

ios - 发现意外的 Mach-O header 代码 : 0x72613c21 while generating ipa file

ios - 在 uitableviewcell 中实现视频的最佳方式是什么?

iphone - 适用于 Android 和 iPhone 的 OpenGL