我的代码很简单,我正在拍摄一张图片并将其作为提取帧的视频的标题幻灯片。但由于某种原因, appendPixelBuffer 一直返回false。 所有帧都设置为1200乘1200的精确高度/宽度。
从AVAssetWritter返回的错误是:
错误域= AVFoundationErrorDomain代码= -11823“无法保存”UserInfo = 0x10c5c4b40 {NSUnderlyingError = 0x113716cf0“操作无法完成。(OSStatus error -12412。)”,NSLocalizedRecoverySuggestion =再次尝试保存。,NSLocalizedDescription =不能保存}
我的代码如下:
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];
答案 0 :(得分:3)
好的,在我的iPhone上进行了一些文件搜索后,我发现我试图存储视频的目录已经存在。最简单的解决方法是将所有这些存储到临时目录中,并在发布时清除它,并在发生崩溃时关闭。