我尝试在 iPhone 上使用 AVAssetWriter 将四个视频流保存为 .mp4。使用三个流,一切正常,但第 4 个 mp4 文件始终为空。
这是我的一段代码:
-(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;
}
}
我在循环中调用此方法,并在每次运行时给出一个图像,该图像应写入四个 mp4 文件之一。最后调用的 AVAssetWriterInput 获取图像,但文件仍然为空。如果我更改调用顺序,最后调用的 AVAssetWriterInput 总是会留下一个空文件。
有什么想法吗?