Record multiple frames as Keyframes(I-frame) using FFmpegFrameRecorder

262 views
Skip to first unread message

Saurabh Agrawal

unread,
Apr 3, 2014, 10:56:18 AM4/3/14
to jav...@googlegroups.com
Hi,
I have to record multiple frames for a video as keyframes.

Normally I do this:

public void onPreviewFrame(byte[] data, Camera camera) {
            /* get video data */
    if (yuvIplimage != null && recording) {
        yuvIplimage.getByteBuffer().put(data);
recorder.setTimestamp(t);
        recorder.record(yuvIplimage);         
    }
}
        This records frames for videos just fine but only has one keyframe per sec.

I also tried setting the keyframe flag for all the frames:

public void onPreviewFrame(byte[] data, Camera camera) {
            /* get video data */
    if (yuvIplimage != null && recording) {
        yuvIplimage.getByteBuffer().put(data);
recorder.setTimestamp(t);
      Frame frame = new Frame();
frame.image = yuvIplimage;
frame.keyFrame = true; 
        recorder.record(frame);  
    }
}

This doesn't have any effect on the videos frames. Is there a sure way to get key frames on demand or setting the keyframe occurrence frequency in the recorded video? Related to this will is be possible to use similar logic and convert existing video to a video with more keyframes(grabber and recorder combination maybe)?

Thanks in advance.

Samuel Audet

unread,
Apr 5, 2014, 3:01:34 AM4/5/14
to jav...@googlegroups.com
Hello,

On 04/03/2014 11:56 PM, Saurabh Agrawal wrote:
> This doesn't have any effect on the videos frames. Is there a sure way

Right, the encoder doesn't take into account the keyframe flag. That
could be a good idea to implement.

> to get key frames on demand or setting the keyframe occurrence frequency
> in the recorded video? Related to this will is be possible to use
> similar logic and convert existing video to a video with more
> keyframes(grabber and recorder combination maybe)?

We can set the maximum interval between keyframes with the GOP size,
where a value of 0 should create a video file with only keyframes.
Incidentally, I've added a setter/getter pair recently in this revision:
http://code.google.com/p/javacv/source/detail?r=9f9f82497b661170d29d43784f04fdcb3ad1f9c3
Would that be enough? if not, FFmpeg does support forcing key frames
with the "force_key_frames" option:
http://ffmpeg.org/ffmpeg.html
But we'd had to add support for that in FFmpegFrameRecorder. Please post
a request here:
http://code.google.com/p/javacv/issues/list
and post a patch as well if you end up implementing something yourself,
thanks! :)

Samuel

Reply all
Reply to author
Forward
0 new messages