我的代码很简单,我正在拍摄图像并将其作为提取帧的视频的标题幻灯片。但出于某种原因,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/