ios - 一段时间后 AVAssetWriterInputPixelBufferAdaptor pixelBufferPool 变为 NULL

标签 ios iphone avassetwriter

我在 AVAssetWriterInputPixelBufferAdaptor 中使用 pixelBufferPool 来创建像素缓冲区以与 append 方法一起使用。创建4个buffer后,pixelBufferPool属性变为NULL;

我这样设置我的编写器、输入和适配器:

- (BOOL) setupRecorder {
    NSError *error = nil;
    if([[NSFileManager defaultManager] fileExistsAtPath:[[self tempFileURL] path]])
        [[NSFileManager defaultManager] removeItemAtURL:[self tempFileURL] error:&error];


    assetWriter = [[AVAssetWriter alloc] initWithURL: [self tempFileURL] 
                                            fileType:AVFileTypeQuickTimeMovie
                                               error:&error]; 
    if (error) {
        NSLog(@"Error creating asset writer: %@", error);
        [assetWriter release];
        return NO;
    }
    // writer

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

    assetWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo 
                                                      outputSettings:videoSettings];

    NSDictionary *bufferAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                                      [NSNumber numberWithInt:kCVPixelFormatType_32BGRA], kCVPixelBufferPixelFormatTypeKey, 
                                      nil];

    adaptor = [[AVAssetWriterInputPixelBufferAdaptor alloc] initWithAssetWriterInput:assetWriterInput sourcePixelBufferAttributes:bufferAttributes];  
    [adaptor retain];
    assetWriterInput.expectsMediaDataInRealTime = YES;
    [assetWriter addInput:assetWriterInput];

    return YES;
}

我用这个分发像素缓冲区:

- (CVPixelBufferRef) createPixelBufferRef {
    CVPixelBufferPoolRef pixelBufferPool = adaptor.pixelBufferPool;
    CVPixelBufferRef pixelBuffer = NULL;
    CVReturn cvReturn = CVPixelBufferPoolCreatePixelBuffer(NULL, pixelBufferPool, &pixelBuffer);
    if(cvReturn != kCVReturnSuccess)
        NSLog(@"CVPixelBuffePoolCreatePixelBuffer: %d", cvReturn);
    bufferCreatedCount++;
    return pixelBuffer;
}

当我完成将像素缓冲区传递给 appendPixelBuffer 后,我使用 CVPixelBufferRelease 释放像素缓冲区。在变为 NULL 之前,我绝不会调用 markAsFinished、endSessionAtSourceTime 或 finishWriting。此外,适配器本身不会变为 NULL。

我读过的大多数帖子都谈到由于适配器配置错误而从一开始就没有池,但我的有,但时间很短。还有其他人看到这种行为吗?

最佳答案

我遇到了同样的问题。正如我发现的那样,如果您放入 appendPixelBuffer:withPresentationTime: 的一些 CMTime 相等,就会发生这种情况。例如,如果您使用的 CMTimeMakeWithSeconds 的时间刻度太粗略,就会发生这种情况。

关于ios - 一段时间后 AVAssetWriterInputPixelBufferAdaptor pixelBufferPool 变为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6414138/

相关文章:

iphone - UITextfield 的行为类似于 email.app 中的 "to:"UItextfield

ios - 通过 AVMutableVideoComposition 和 CAAnimation 输出的黑色视频

ios - 在多个分区中拆分视频

ios - 为什么使用 AVCaptureSession、AVAssetWriter 和 AVPlayer 录制和重放视频会卡顿?

iOS Mach-O – 使 __TEXT 段暂时可写

c# - UITableViewDelegate 中删除单元格时发生 NSInternalInconsistencyException 错误/Xamarin.iOS

ios - 如何使用Swift语言弹出VC跳转前两个View Controller?

iphone - 使用 pushViewController 进行内存管理

iphone - IOS:我可以在xcode中通过恢复id获取对象吗?

iphone - 核心数据删除对象函数