AVAssetWriter finishWriting failing and producing videos that can only be watched for 50%-Collection of common programming errors
I am currently capturing video and audio using AVCaptureSession and have set up the pipeline to write out the data to disk using an AVAssetWriter. For short videos this is working fine absolutely no issues.
However if the length of the recorded media goes over some arbitrary time, usually around 2 minutes then the call to finish writing fails but still manages to produce a video file that only plays back around 50% of its content. I continuously call isReadyForMoreMediaData and appendSampleBuffer during writing and they always returns true. I seem to get no notification that something has gone wrong until the finishWriting call.
The AVAssetWriters Error is set to the following after the failed finishWriting call
Capture failed to end with status 3 and error Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x210b7880 {NSLocalizedFailureReason=An unknown error occurred (268451843), NSUnderlyingError=0x210b1ee0 "The operation couldn,Äôt be completed. (OSStatus error 268451843.)", NSLocalizedDescription=The operation could not be completed}
Not the most useful error in the world…
This is followed shortly by a call to the AVCaptureSessionRuntimeErrorNotification listener that I have set up which contains the error
Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo=0x21011940 {NSLocalizedRecoverySuggestion=Try again later., NSLocalizedDescription=Cannot Complete Action}
Again not very helpful…
This is the code i Use to configure the AVAssetWriter input for audio
//\Configure Audio Writer Input
AudioChannelLayout acl;
bzero(&acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
NSDictionary* audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
[ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,
[ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
[ NSData dataWithBytes: &acl length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
[ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
nil];
m_audioWriterInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings] retain];
This is the code I use to configure the AVAssetWriterInput for video
videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInt:640], AVVideoWidthKey,
[NSNumber numberWithInt:480], AVVideoHeightKey,
nil];
m_videoWriterInput = [[AVAssetWriterInput
assetWriterInputWithMediaType:AVMediaTypeVideo
outputSettings:videoSettings] retain];
m_videoWriterInput.expectsMediaDataInRealTime = YES;
This is the code i use to initialise the AVAssetWriter
m_audioAndVideoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:outputPath] fileType:AVFileTypeQuickTimeMovie error:&error];
m_audioAndVideoWriter.shouldOptimizeForNetworkUse = YES;
[m_audioAndVideoWriter addInput:m_videoWriterInput];
[m_audioAndVideoWriter addInput:m_audioWriterInput];
Has anyone experienced similar issues? Or know what stuff goes on inside the finishWriting call that might cause this issue? or maybe someone knows some novel ways of debugging this issue?
Thanks