Reputation: 1477
My code is simple, I'm taking a image and putting it as the title slide of a video that's frames were extracted. But for some reason the appendPixelBuffer keeps returning false. All frames are set to the exact height/width of 1200 by 1200.
The error being returned from the AVAssetWritter is:
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}
My code is below:
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];
Upvotes: 4
Views: 2062
Reputation: 1477
Okay, after doing some file searching on my iPhone, I found that the directory that I was trying to store the video at already existed. The easiest fix is to store all of this into a temp directory and to clear it on launch and close in the event of a crash.
Upvotes: 3