Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ffmpeg

encoder

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.

like image 612
SunnyShah Avatar asked Feb 18 '10 14:02

SunnyShah


People also ask

What is Keyint FFmpeg?

keyint specifies the maximum length of the GOP , so the maximum interval between each keyframe, which remember that can be either an IDR frame or a non-IDR frame.

What is FFmpeg video format?

ffmpeg is a command-line tool that converts audio or video formats. It can also capture and encode in real-time from various hardware and software sources such as a TV capture card. ffplay is a simple media player utilizing SDL and the FFmpeg libraries.


4 Answers

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.

like image 26
bcmpinc Avatar answered Oct 01 '22 16:10

bcmpinc


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.

like image 98
Ori Pessach Avatar answered Oct 01 '22 15:10

Ori Pessach


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.

like image 3
Ignacio Vazquez-Abrams Avatar answered Oct 01 '22 14:10

Ignacio Vazquez-Abrams


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.

like image 1
deddihp Avatar answered Oct 01 '22 15:10

deddihp