Keyframe intervals in x264

General questions or discussion about HandBrake, Video and/or audio transcoding, trends etc.
Post Reply
DKqwerty
Posts: 11
Joined: Wed May 29, 2019 5:02 pm

Keyframe intervals in x264

Post by DKqwerty »

Description of problem or question:

Wondering how keyframes are handled with regard to x264.

I've noticed almost all scene cuts get a keyframe, makes sense as a default. However, it seems in the absence of a detectable scene cut, a keyframe is generated every 12 seconds (at least when 1080p @ 29.970fp). SD content seems to have a shorter but more arbitrary non-scene cut interval. Not sure why this default duration was selected or how to change it.

So first, how do I change to a constant keyframe rate (i.e. every 2 seconds, regardless of cuts or lack of cuts)?

Second, how do I change the non-scene cut keyframe duration while also generating them for scene cuts?


Steps to reproduce the problem (If Applicable):

Inspect the keyframes of any x264 render from HandBrake of content that has long intervals (30+ seconds) without scene cuts.


HandBrake version (e.g., 1.0.0):

1.3.0 and Nightly 0b089e9


Operating system and version (e.g., Ubuntu 16.04 LTS, macOS 10.13 High Sierra, Windows 10 Creators Update):

Windows 10 Pro x64


HandBrake Activity Log ***required*** (see How-to get an activity log)

Code: Select all

HandBrake Nightly 20191229213216-0b089e9-master (2019122901)
OS: Microsoft Windows NT 6.2.9200.0
CPU: Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz
Ram: 8169 MB, 
GPU Information:
  NVIDIA GeForce GTX 460M - 23.21.13.8857
Screen: 1920x1080
Temp Dir: C:\Users\David\AppData\Local\Temp\
Install Dir: X:\Installers\HandBrake-20191229-0b089e9d1_x86_64-Win_GUI
Data Dir: C:\Users\David\AppData\Roaming\HandBrake\Nightly

-------------------------------------------


# Starting Encode ...

[07:22:06] base preset: Fixer (Modified)
[19:22:06] hb_init: starting libhb thread
[19:22:06] Starting work at: Sun Jan 05 19:22:06 2020
[19:22:06] 1 job(s) to process
[19:22:06] json job:
{
  "Audio": {
    "AudioList": [
      {
        "DRC": 0.0,
        "Encoder": "copy",
        "Gain": 0.0,
        "Mixdown": -1,
        "NormalizeMixLevel": false,
        "Samplerate": 0,
        "Track": 0,
        "DitherMethod": 0
      }
    ],
    "CopyMask": [
      "copy:aac",
      "copy:ac3",
      "copy:dtshd",
      "copy:dts",
      "copy:eac3",
      "copy:flac",
      "copy:mp3",
      "copy:truehd"
    ],
    "FallbackEncoder": "ac3"
  },
  "Destination": {
    "ChapterList": [
      {
        "Name": "Chapter 1"
      }
    ],
    "ChapterMarkers": true,
    "AlignAVStart": false,
    "File": "X:\\--------------------.mkv",
    "Mp4Options": {
      "IpodAtom": false,
      "Mp4Optimize": false
    },
    "Mux": "mkv"
  },
  "Filters": {
    "FilterList": [
      {
        "ID": 2,
        "Settings": {
          "skip-bottom": "0",
          "skip-left": "0",
          "skip-right": "0",
          "skip-top": "1"
        }
      },
      {
        "ID": 12,
        "Settings": {
          "crop-bottom": "0",
          "crop-left": "0",
          "crop-right": "0",
          "crop-top": "0",
          "height": "1080",
          "width": "1920"
        }
      },
      {
        "ID": 6,
        "Settings": {
          "mode": "0"
        }
      }
    ]
  },
  "PAR": {
    "Num": 1,
    "Den": 1
  },
  "Metadata": {},
  "SequenceID": 0,
  "Source": {
    "Angle": 1,
    "Range": {
      "Type": "frame",
      "Start": 0,
      "End": 153657
    },
    "Title": 1,
    "Path": "N:\\Music\\-----.vid\\--------------------.mpg"
  },
  "Subtitle": {
    "Search": {
      "Burn": false,
      "Default": false,
      "Enable": false,
      "Forced": false
    },
    "SubtitleList": []
  },
  "Video": {
    "Encoder": "x264",
    "Level": "auto",
    "TwoPass": false,
    "Turbo": false,
    "ColorMatrixCode": 0,
    "Options": "rc-lookahead=60:bframes=8:ref=4:merange=64:analyse=all:trellis=2",
    "Preset": "veryfast",
    "Profile": "auto",
    "Quality": 19.0,
    "QSV": {
      "Decode": false,
      "AsyncDepth": 0
    }
  }
}
[19:22:06] CPU: Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz
[19:22:06]  - Intel microarchitecture Sandy Bridge
[19:22:06]  - logical processor count: 8
[19:22:06] Intel Quick Sync Video support: no
[19:22:06] hb_scan: path=N:\Music\-----.vid\--------------------.mpg, title_index=1
udfread ERROR: ECMA 167 Volume Recognition failed
src/libbluray/disc/disc.c:323: failed opening UDF image N:\Music\-----.vid\--------------------.mpg
src/libbluray/disc/disc.c:424: error opening file BDMV\index.bdmv
src/libbluray/disc/disc.c:424: error opening file BDMV\BACKUP\index.bdmv
src/libbluray/bluray.c:2585: nav_get_title_list(N:\Music\-----.vid\--------------------.mpg\) failed
[19:22:07] bd: not a bd - trying as a stream/file instead
libdvdnav: Using dvdnav version 6.0.1
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.IFO failed
libdvdread:DVDOpenFileUDF:UDFFindFile /VIDEO_TS/VIDEO_TS.BUP failed
libdvdread: Can't open file VIDEO_TS.IFO.
libdvdnav: vm: failed to read VIDEO_TS.IFO
[19:22:07] dvd: not a dvd - trying as a stream/file instead
[19:22:07] file is MPEG Program Stream
[19:22:07] Probing 1 unknown stream
[19:22:07]     Probe: Found stream mpegvideo. stream id 0xe0-0x0
[19:22:07] Found the following streams
[19:22:07]     Video Streams :
[19:22:07]       0xe0-0x0 type MPEG2 (0x2)
[19:22:07]     Audio Streams :
[19:22:07]       0xbd-0x80 type AC3 (0x81)
[19:22:07]     Subtitle Streams :
[19:22:07]     Other Streams :
[19:22:07] stream id 0xbd (type 0x81 substream 0x80) audio 0x8000bd
[19:22:11] scan: decoding previews for title 1
[19:22:11] file is MPEG Program Stream
[19:22:11] Probing 1 unknown stream
[19:22:11]     Probe: Found stream mpegvideo. stream id 0xe0-0x0
[19:22:11] scan: audio 0x8000bd: ac3, rate=48000Hz, bitrate=256000 Unknown (AC3) (2.0 ch) (256 kbps)
[19:22:12] stream: 324 good frames, 0 errors (0%)
[19:22:12] scan: 10 previews, 1920x1080, 29.970 fps, autocrop = 0/0/0/0, aspect 16:9, PAR 1:1
[19:22:12] libhb: scan thread found 1 valid title(s)
[19:22:12] Starting Task: Encoding Pass
[19:22:12] Skipping crop/scale filter
[19:22:12] Auto Passthru: allowed codecs are AAC, AC3, E-AC3, TrueHD, DTS, DTS-HD, MP3, FLAC
[19:22:12] Auto Passthru: fallback is AC3
[19:22:12] Auto Passthru: using AC3 Passthru for track 1
[19:22:12] work: only 1 chapter, disabling chapter markers
[19:22:12] job configuration:
[19:22:12]  * source
[19:22:12]    + N:\Music\-----.vid\--------------------.mpg
[19:22:12]    + title 1, frames 0 to 153656
[19:22:12]  * destination
[19:22:12]    + X:\--------------------.mkv
[19:22:12]    + container: Matroska (libavformat)
[19:22:12]  * video track
[19:22:12]    + decoder: mpeg2video
[19:22:12]      + bitrate 200 kbps
[19:22:12]    + filters
[19:22:12]      + Detelecine (pullup) (skip-left=0:skip-right=0:skip-top=1:skip-bottom=0)
[19:22:12]      + Framerate Shaper (mode=0)
[19:22:12]        + frame rate: same as source (around 29.970 fps)
[19:22:12]    + Output geometry
[19:22:12]      + storage dimensions: 1920 x 1080
[19:22:12]      + pixel aspect ratio: 1 : 1
[19:22:12]      + display dimensions: 1920 x 1080
[19:22:12]    + encoder: H.264 (libx264)
[19:22:12]      + preset:  veryfast
[19:22:12]      + options: rc-lookahead=60:bframes=8:ref=4:merange=64:analyse=all:trellis=2
[19:22:12]      + profile: auto
[19:22:12]      + level:   auto
[19:22:12]      + quality: 19.00 (RF)
[19:22:12]      + color profile: 1-1-1
[19:22:12]  * audio track 1
[19:22:12]    + decoder: Unknown (AC3) (2.0 ch) (256 kbps) (track 1, id 0x8000bd)
[19:22:12]      + bitrate: 256 kbps, samplerate: 48000 Hz
[19:22:12]    + AC3 Passthru
[19:22:12] file is MPEG Program Stream
[19:22:12] Probing 1 unknown stream
[19:22:12]     Probe: Found stream mpegvideo. stream id 0xe0-0x0
[19:22:12] sync: expecting 153657 video frames
[19:22:12] encx264: min-keyint: 30, keyint: 300
[19:22:12] encx264: encoding at constant RF 19.000000
[19:22:12] encx264: unparsed options: rc-lookahead=60:bframes=8:ref=4:merange=64:analyse=all:trellis=2:weightp=1:subme=2:mixed-refs=0
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
x264 [info]: profile High, level 4.0, 4:2:0, 8-bit
[19:22:12] sync: first pts video is 0
[19:22:12] sync: first pts audio 0x8000bd is 0
[20:46:56] reader: done. 2 scr changes
[20:46:58] work: average encoding speed for job is 30.211010 fps
[20:46:58] vfr: lost time: 76090014 (0 frames)
[20:46:58] vfr: gained time: 76090014 (101344 frames) (0 not accounted for)
[20:46:58] stream: 5750716 good frames, 0 errors (0%)
[20:46:58] ac3-decoder done: 160204 frames, 0 decoder errors
[20:46:58] mpeg2video-decoder done: 153643 frames, 0 decoder errors
[20:46:58] sync: got 153643 frames, 153657 expected
[20:46:58] sync: framerate min 29.970 fps, max 29.970 fps, avg 29.970 fps
x264 [info]: frame I:832   Avg QP:12.05  size: 93883
x264 [info]: frame P:28829 Avg QP:14.22  size: 47969
x264 [info]: frame B:98644 Avg QP:17.35  size: 16260
x264 [info]: consecutive B-frames:  3.0%  4.0%  4.3%  8.2% 52.6% 12.3%  8.5%  3.3%  3.8%
x264 [info]: mb I  I16..4: 41.4% 45.0% 13.6%
x264 [info]: mb P  I16..4: 33.1% 26.1%  1.5%  P16..4: 21.8%  3.8%  1.4%  0.5%  0.2%    skip:11.6%
x264 [info]: mb B  I16..4:  2.5%  4.5%  0.1%  B16..8: 21.9%  4.0%  0.4%  direct:18.7%  skip:48.0%  L0:46.4% L1:45.5% BI: 8.0%
x264 [info]: 8x8 transform intra:48.9% inter:54.3%
x264 [info]: coded y,uvDC,uvAC intra: 30.3% 74.1% 32.0% inter: 7.7% 37.6% 4.2%
x264 [info]: i16 v,h,dc,p: 73% 16%  8%  3%
x264 [info]: i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 31% 27% 29%  2%  2%  2%  2%  2%  3%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 25% 13%  5%  7%  7%  7%  5%  7%
x264 [info]: i8c dc,h,v,p: 41% 27% 23%  9%
x264 [info]: Weighted P-Frames: Y:5.5% UV:4.6%
x264 [info]: ref P L0: 73.9% 17.5%  8.2%  0.4%
x264 [info]: ref B L0: 88.0% 10.1%  1.9%
x264 [info]: ref B L1: 97.2%  2.8%
x264 [info]: kb/s:4782.92
[20:46:59] mux: track 0, 128305 frames, 3064955909 bytes, 4782.84 kbps, fifo 2048
[20:46:59] mux: track 1, 160204 frames, 164048896 bytes, 256.00 kbps, fifo 4096
[20:46:59] Finished work at: Sun Jan 05 20:46:59 2020
[20:46:59] libhb: work result = 0

# Encode Completed ...

mduell
Veteran User
Posts: 7034
Joined: Sat Apr 21, 2007 8:54 pm

Re: Keyframe intervals in x264

Post by mduell »

Code: Select all

[19:22:12] encx264: min-keyint: 30, keyint: 300
Default min keyint is 1x framerate, default max keyint is 10x framerate. Balances efficiency with reasonable seeking performance.

Adjust either to your liking, or make them equal if you really want an IDR frame every 60 frames. Also you could specify no-scenecut or scenecut=0 if you really want to avoid an I frame even when a scenecut is detected below the min-scenecut interval, but I wouldn't recommend that unless you have a very specific and well reasoned requirement.

I've noticed the non-scenecut I frame selection does vary for the same video when downscaling. Doesn't vary by much, but I guess the metrics come out enough differently to push the encoder there.

Lots of info, and I think most of it is still correct, in this thread.

DKqwerty
Posts: 11
Joined: Wed May 29, 2019 5:02 pm

Re: Keyframe intervals in x264

Post by DKqwerty »

Thanks for the prompt response! I'll dive into the thread.

Post Reply