This code to write video+audio through AVAssetWriter and AVAssetWriterInputs is not working. Why?
In startVideoRecording I call (I assume you are calling this at some point)
[_capSession startRunning] ;
In stopVideoRecording I do not call
[_videoWriterInput markAsFinished];
[_videoWriter endSessionAtSourceTime:lastSampleTime];
The markAsFinished is more for use with the block style pull method. See requestMediaDataWhenReadyOnQueue:usingBlock in AVAssetWriterInput for an explanation. The library should calculate the proper timing for interleaving the buffers.
You do not need to call endSessionAtSrouceTime. The last time stamp in the sample data will be used after the call to
[_videoWriter finishWriting];
I also explicitly check for the type of capture output.
else if( captureOutput == _audioOutput) {
[self newAudioSample:sampleBuffer];
}
Here is what I have. The audio and video come through for me. It is possible I changed something. If this does not work for you then I will post everything I have.
-(void) startVideoRecording
{
if( !_isRecording )
{
NSLog(@"start video recording...");
if( ![self setupWriter] ) {
NSLog(@"Setup Writer Failed") ;
return;
}
[_capSession startRunning] ;
_isRecording = YES;
}
}
-(void) stopVideoRecording
{
if( _isRecording )
{
_isRecording = NO;
[_capSession stopRunning] ;
if(![_videoWriter finishWriting]) {
NSLog(@"finishWriting returned NO") ;
}
//[_videoWriter endSessionAtSourceTime:lastSampleTime];
//[_videoWriterInput markAsFinished];
//[_audioWriterInput markAsFinished];
NSLog(@"video recording stopped");
}
}
First, do not use [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
, as it is not the native format of the camera. use [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]
Also, you should always check before calling startWriting that it isn't already running. You do not need to set session end time, as stopWriting will do that.