I try to save four video streams with AVAssetWriter on the iPhone as .mp4. With three streams everything works fine, but the 4th mp4 file is always empty.
Here is a piece of my code:
-(void)writeImagesToMovie:(CVPixelBufferRef) buffer :(int) cameraID
{
AVAssetWriterInput* writerInput;
AVAssetWriterInputPixelBufferAdaptor *adaptor;
int* frameNumber;
switch (cameraID) {
case 1:
writerInput = writerInput1;
adaptor = adaptor1;
frameNumber =&frameNumber1;
break;
case 2:
writerInput = writerInput2;
adaptor = adaptor2;
frameNumber =&frameNumber2;
break;
case 3:
writerInput = writerInput3;
adaptor = adaptor3;
frameNumber =&frameNumber3;
break;
default:
writerInput = writerInput4;
adaptor = adaptor4;
frameNumber =&frameNumber4;
break;
}
if(writerInput.readyForMoreMediaData){
CMTime frameTime = CMTimeMake(1, 30);//150, 600
// CMTime = Value and Timescale.
// Timescale = the number of tics per second you want
// Value is the number of tics
// For us - each frame we add will be 1/4th of a second
// Apple recommend 600 tics per second for video because it is a
// multiple of the standard video rates 24, 30, 60 fps etc.
CMTime lastTime=CMTimeMake((int64_t)*frameNumber, 30);
CMTime presentTime=CMTimeAdd(lastTime, frameTime);
if (*frameNumber == 0) {presentTime = CMTimeMake(0, 30);}//600
// This ensures the first frame starts at 0.
// Give the Image to the AVAssetWriter to add to your video
[adaptor appendPixelBuffer:buffer withPresentationTime:presentTime];
*frameNumber=*frameNumber+1;
}
}
I call this method in a loop and give in each run a image, which should be written in one of the four mp4 files. The last called AVAssetWriterInput gets a image, but the file remains empty. If I change the order of the calls, always the last called AVAssetWriterInput leaves an empty file.
Are there any ideas?