[FFmpeg-user] split and re-encode H.264 stream at IDR frame

wtfux wtfux.dev at googlemail.com
Wed Sep 12 18:34:56 CEST 2012


Hey,
thanks for your help with my other questions, you were really helpful :).
I got another question: I got a blu-ray disc with 2 episodes of a show in
one m2ts file. Both episodes share the same H.264 and PCM streams and the
second one is selectable from BD menu.
I want to re-encode the episodes to H.264 (with less bitrate) and flac. The
problem is to split the clip as the episodes don't have the same length.
At the point where the second episode starts there is a IDR frame in the
H.264 stream and I want ffmpeg to split exactly before that frame.

The blu-ray menu says that the second episode starts at 00:23:42.046. I
used Avidemux to look where the IDR/I frame is. Avidemux says that the IDR
frame is at 00:23:42.088 and the frame prior to that is at 00:23:42.047.
Well the times of Avidemux and the BD menu don't match, I guess Avidemux is
reading the file incorrectly.
I used FFprobe to dump frame information and this is what I got:

The frame ahead of the IDR frame:

[FRAME]
media_type=video
key_frame=0
pkt_pts=1422046
pkt_pts_time=0:23:42.046000
pkt_dts=1422046
pkt_dts_time=0:23:42.046000
pkt_duration=41
pkt_duration_time=0:00:00.041000
pkt_pos=5828419424
width=1920
height=1080
pix_fmt=yuv420p
sample_aspect_ratio=1:1
pict_type=P
coded_picture_number=34094
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
reference=0
[/FRAME]


And the IDR frame itself:

[FRAME]
media_type=video
key_frame=1
pkt_pts=1422087
pkt_pts_time=0:23:42.087000
pkt_dts=1422087
pkt_dts_time=0:23:42.087000
pkt_duration=41
pkt_duration_time=0:00:00.041000
pkt_pos=5828419597
width=1920
height=1080
pix_fmt=yuv420p
sample_aspect_ratio=1:1
pict_type=I
coded_picture_number=34095
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
reference=3
[/FRAME]


The times match with the BD menu however if pkt_dts_time is the time where
the frame *starts* then it would be off by 1 frame (the bd menu would refer
to the last frame of the first episode instead of the first frame of the
second episode).
However this is FFprobe's output for the FIRST frame of the whole file:

[FRAME]
media_type=video
key_frame=1
pkt_pts=42
pkt_pts_time=0:00:00.042000
pkt_dts=42
pkt_dts_time=0:00:00.042000
pkt_duration=N/A
pkt_duration_time=N/A
pkt_pos=799
width=1920
height=1080
pix_fmt=yuv420p
sample_aspect_ratio=1:1
pict_type=I
coded_picture_number=0
display_picture_number=0
interlaced_frame=0
top_field_first=0
repeat_pict=0
reference=3
[/FRAME]

pkt_pts_time is not 0 what I would expect for the first frame of a video to
be so I assume it's the time when the frame is *over* and not where it
*starts*? Please correct me if I'm wrong, I'm completely unsure here.

So all in all, if I want to create two videos from this file I need to use
these commands?

ffmpeg -i INPUT -c:v libx264 -c:a flac ... -t 0:23:42.087 OUTPUT #first
episode or maybe 0:23:42.046 which is the pkt_dts_time of the *second last*
frame which means the time where the *last* frame starts if I'm correct.

ffmpeg -ss 0:23:42.087 -i INPUT -c:v libx264 -c:a flac ... OUTPUT #second
episode, first frame of the second episode if I'm correct, also the same
time the BD menu entry suggests.


Or is there perhaps a much easier way to split at the IDR frame? I couldn't
find out if I can tell ffmpeg a frame count where it should end or start.
Don't know if that would work either because I get a "[h264 @
00000000015a2800] number of reference frames (0+3) exceeds max (2; probably
corrupt input), discarding one" while decoding and that could bring the
whole thing out of sync maybe.

Sorry for such a long post again.


More information about the ffmpeg-user mailing list