16

I am absolutely new to video processing, I am building a video streaming site and worked with ffmpeg to generate the formats I need for ShakaPackager.

Sadly I don't understand some of the options being provided, especially keyInt, min-keyint and no-scenecut.

Below you will find the code I use to generate the required formats for my videos.

ffmpeg -y -i ${1} -c:a aac -ac 2 -ab 256k -ar 48000 -c:v libx264 -x264opts "keyint=24:min-keyint=24:no-scenecut" -b:v 1500k -maxrate 1500k -bufsize 1000k -vf "scale=-2:720" "${2}/${3}720.mp4"

I did a bit of research and they talked about IDR-frames I don't even understand those, maybe my needs go way more than that?

3 Answers 3

32

The frames in your H.264 video are grouped into units called GOPs (Group Of Pictures). Inside these GOPs frames are classified into three types:

  • I-frame: frame that stores the whole picture
  • P-frame: frame that stores only the changes between the current picture and previous ones
  • B-frame: frame that stores differences with previous or future pictures

Additionally, I-frames can be classified as IDR frames and non-IDR frames. The difference is that frames following an IDR frame cannot reference any frame that comes before the IDR frame, while in the case of a non-IDR frame there are no limitations.

Every GOP starts with an I-frame, also called a keyframe, but may contain more than one. To create further confusion, a GOP can start with an IDR frame or with a non-IDR frame. This means that frames in the GOP can sometimes refer to previous GOPs (in this case the GOP is said to be "open"), and sometimes not (in this case it's closed).

It's common to see the structure of a GOP represented as in this example: IBBBPBBBPBBBI. Here the length of the the GOP is 12 frames, with 3 B-frames between each P-frame.

Now your questions:

  • 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. I'm not completely sure but I think that by default ffmpeg will require every I-frame to be an IDR frame, so in practice you can use the terms IDR frame and I-frame interchangeably
  • min-keyint specifies the minimum length of the GOP. This is because the encoder might decide that it makes sense to add a keyframe before the keyint value, so you can put a limit
  • no-scenecut. When the encoder determines that there's been a scene cut, it may decide to insert an additional I-frame. The issue is that I-frames are very expensive if compared to other frame types, so when encoding for streaming you want to disable it.

A couple of other related parameters:

  • bframes specifies the number of consecutive B-frames
  • ref specifies the number of references frames that are used when encoding. It means that when encoding a picture, ref previous frames are looked at for encoding the current frame
4
  • In the OP's command, these are x264 options, so keyint and min-keyint relate to IDR placement, not non-RAP I frames.
    – Gyan
    Commented Aug 20, 2018 at 19:25
  • Thanks for the amazing response... I have learnt so much.
    – George
    Commented Aug 20, 2018 at 20:46
  • 3
    My question now is how did you learn all of these, how do I learn too?
    – George
    Commented Aug 20, 2018 at 20:48
  • I wish i could give more votes to this one. Amazing answer and very informative. Thank you.
    – Somebody
    Commented Oct 14, 2021 at 12:57
7

Most video encoders apply temporal compression i.e. most frames only store the difference in image content relative to other frames, thus saving storage space, while a few frames store the whole image. The frames with whole pictures are called key frames since they are required in order to reconstruct the whole picture of frames which only store differences. IDR frame in H264 stream denotes a keyframe.

Your code tells ffmpeg to generate a H264 video stream using the x264 encoder, and the options below are used by the encoder to decide keyframe placement.

-x264opts "keyint=24:min-keyint=24:no-scenecut"

keyint=24 tells x264 to forcibly generate a keyframe 24 frames after the last keyframe.

min-keyint=24 sets the minimum keyframe distance. The encoder may decide to place a keyframe on its own even if keyint # of frames haven't elapsed if it comes across an image which is more efficient to store as a complete picture rather than as a set of differences. min-keyint tells the encoder to never do this if the distance isn't at least 24. Since in this case keyint is the same value as min-keyint, all KFs will be 24 frames apart.

no-scenecut - scenecut is the value used by the encoder to calculate if it should force a keyframe if min-keyint allows it to. It can range from 0-100. no-scenecut tells the encoder to skip this evaluation.

3
  • What is the point of having no-scenecut when keyint = min-keyint? Aren’t they equivalent?
    – Géry Ogam
    Commented Mar 2, 2020 at 14:33
  • 3
    From what I remember, it saves the frame comparison analysis effort.
    – Gyan
    Commented Mar 2, 2020 at 14:49
  • In my case the command above returns instantly. Any idea? Commented May 27, 2021 at 15:38
0

What is the point of having no-scenecut when keyint = min-keyint

There is the code in x264 (validate_parameters function):

if( h->param.i_keyint_min == X264_KEYINT_MIN_AUTO )
    h->param.i_keyint_min = X264_MIN( h->param.i_keyint_max / 10, (int)fps );

It will disallow equality of the keyint and keyint_min.

1
  • That's correct, but the piece of code you copied is not the relevant one. The line that performs the clipping is this one: h->param.i_keyint_min = x264_clip3( h->param.i_keyint_min, 1, h->param.i_keyint_max/2+1 );
    – mcont
    Commented Nov 18, 2022 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.