ios - 什么会导致 appendPixelBuffer 返回 false?

标签 ios avfoundation avassetwriter

我的代码很简单,我正在拍摄图像并将其作为提取帧的视频的标题幻灯片。但出于某种原因,appendPixelBuffer 一直返回 false。 所有框架都设置为 1200 x 1200 的精确高度/宽度。

从 AVAssetWritter 返回的错误是:

Error Domain=AVFoundationErrorDomain Code=-11823 "Cannot Save" UserInfo=0x10c5c4b40 {NSUnderlyingError=0x113716cf0 "The operation couldn’t be completed. (OSStatus error -12412.)", NSLocalizedRecoverySuggestion=Try saving again., NSLocalizedDescription=Cannot Save}

我的代码如下:

NSError *error = nil;

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:
                              [NSURL fileURLWithPath:[VideoHandler movieLocation]] fileType:AVFileTypeQuickTimeMovie
                                                          error:&error];
NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt:1200], AVVideoWidthKey,
                               [NSNumber numberWithInt:1200], AVVideoHeightKey,
                               nil];

AVAssetWriterInput* videoWriterInput = [AVAssetWriterInput
                                        assetWriterInputWithMediaType:AVMediaTypeVideo
                                        outputSettings:videoSettings];


AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
                                                 assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
                                                 sourcePixelBufferAttributes:nil];

NSParameterAssert(videoWriterInput);
NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
videoWriterInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:videoWriterInput];

//Start a session:
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];

CVPixelBufferRef buffer = NULL;

int frameCount = 0;
for (int i = (int)assets.count; i > 0; i --) {
    UIImage *img = [SelectedImage getImage];
    buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:[img size]];

    BOOL append_ok = NO;
    int j = 0;
    while (!append_ok && j < 30)
    {
        if (adaptor.assetWriterInput.readyForMoreMediaData)
        {
            printf("appending %d attemp %d\n", frameCount, j);

            //int fps = [[imageDurations objectAtIndex:frameCount] intValue];
            CMTime frameTime = CMTimeMake(frameCount, (int32_t)15 / [imageDurations count]);
            append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];

            if(buffer)
                CVBufferRelease(buffer);
            [NSThread sleepForTimeInterval:0.05];
        }
        else
        {
            printf("adaptor not ready %d, %d\n", frameCount, j);
            NSDate *maxDate = [NSDate dateWithTimeIntervalSinceNow:0.1];
            [[NSRunLoop currentRunLoop] runUntilDate:maxDate];
        }
        j++;
    }
    if (!append_ok) {
        printf("error appending image %d times %d\n", frameCount, j);
    }

    frameCount++;
}

//Finish the session:
[videoWriterInput markAsFinished];
[videoWriter finishWriting];

最佳答案

好的,在我的 iPhone 上进行了一些文件搜索后,我发现我试图存储视频的目录已经存在。最简单的修复方法是将所有这些存储到一个临时目录中,并在启动时清除它,并在发生崩溃时关闭。

关于ios - 什么会导致 appendPixelBuffer 返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20983298/

相关文章:

objective-c - IOS 上的波形

objective-c - 使用 AVFoundation 的视频的第一帧

swift - 在 Swift 中使用 AVAssetWriter 将 AVCaptureVideoDataOutput 保存到电影文件

video - iPhone RGBA 到 ARGB

avfoundation - 帮助我理解 AVAssetWriter 中的 CMTime

ios - 在不同线程上调用的类的实例方法可以使用类的属性吗?

iOS Swift 文本填充屏幕

iphone - 如何从录制的视频中获取帧作为图像 -AVFoundation(图像作为缩略图)

ios - 缺少包产品 <包依赖项名称>

ios - 如何在 iOS 中将 Facebook/Twitter 写入地址簿?