We are trying to understand how to control/specify the frame rate for videos that we are encoding with AVAssetReader
and AVAssetWriter
. Specifically, we are are using AVAssetReader
and AVAssetWriter
to transcode/encode/compress a video that we have accessed from the photo/video gallery. We are able to control things like bit rate, aspect ratio changes, etc., but cannot figure out how to control the frame rate. To be specific, we'd like to be able to take as input a 30 FPS video that's 5 minutes long and emit a 5 minute video at 15 FPS.
Our current loop that processes sample buffers is:
[writer startWriting];
[writer startSessionAtSourceTime:kCMTimeZero];
[videoReader startReading];
[videoWriterInput requestMediaDataWhenReadyOnQueue:videoEncoderQueue usingBlock:
^{
while ([videoWriterInput isReadyForMoreMediaData]) {
CMSampleBufferRef sampleBuffer;
if ([videoReader status] == AVAssetReaderStatusReading
&& (sampleBuffer = [videoReaderTrackOutput copyNextSampleBuffer])) {
if (sampleBuffer) {
BOOL result = [videoWriterInput appendSampleBuffer:sampleBuffer];
CFRelease(sampleBuffer);
if (!result) {
[videoReader cancelReading];
break;
}
}
} else {
// deal with status other than AVAssetReaderStatusReading
[videoWriterInput markAsFinished];
// [...]
break;
}
}
}];
How do we augment or change this so that we could control the frame rate of the created video? We cannot seem to find a sample in SO or anywhere else that clearly explains how to do this. I think we're supposed to use CMTime
and probably some other methods other than the ones in the code sample above, but the details aren't clear.