How to write a video encoder with ffmpeg that explicitly controls the position of keyframes?

15,720

Solution 1

You'll need to look at the libavcodec documentation - specifically, at avcodec_encode_video(). I found that the best available documentation is in the ffmpeg header files and the API sample source code that's provided with the ffmpeg source. Specifically, look at libavcodec/api-example.c or even ffmpeg.c.

To force an I frame, you'll need to set the pict_type member of the picture you're encoding to 1: 1 is an I frame, 2 is a P frame, and I don't remember what's the code for a B frame off the top of my head... Also, the key_frame member needs to be set to 1.

Some introductory material is available here and here, but I don't really know how good it is.

You'll need to be careful how you allocate the frame objects that the API calls require. api-example.c is your best bet as far as that goes, in my opinion. Look for the function video_encode_example() - it's concise and illustrates all the important things you need to worry about - pay special attention to the second call to avcodec_encode_video() that passes a NULL picture argument - it's required to get the last frames of video since MPEG video is encoded out of sequence and you may end up with a delay of a few frames.

Solution 2

An up-to-date version of api-example.c can be found at http://ffmpeg.org/doxygen/trunk/doc_2examples_2decoding_encoding_8c-example.html

It does the entire video encoding in a single and relatively short function. So this is probably a good place to start. Compile and run it. And then start modifying it until it does what you want.

It also has audio encoding and audio & video decoding examples.

Solution 3

GStreamer has decent documentation, has bindings for a number of languages (although the native API is C), and supports any video format you can find plugins for, including H.263 via gstreamer-ffmpeg.

Solution 4

If you're Java programmer then use Xuggler.

Solution 5

you will need libavcodec library, For the first step I think you can learn about its use in ffplay.c file inside ffmpeg source code. It would tell you a lot. You can check my project also about video at rtstegvideo.sourceforge.net.

Hope this help.

Share:
15,720
SunnyShah
Author by

SunnyShah

I solve machine learning problems with Java, R, JS, C++.

Updated on June 09, 2022

Comments

  • SunnyShah
    SunnyShah about 2 years

    I want to write an encoder with ffmpeg which can put iFrames (keyframes) at positions I want. Where can I found tutorials or reference material for it?

    P.S
    Is it possible to do this with mencoder or any opensource encoder. I want to encode H263 file. I am writing under & for linux.

  • MyKey_
    MyKey_ almost 13 years
    Btw, the values for the pict_type member of the picture are AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, and so on...
  • Nicolas Dusart
    Nicolas Dusart over 10 years
    It is better to look for the official documentation for up-to-date version: ffmpeg.org/doxygen/trunk/…