我在 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/