[FFmpeg-user] How '-ss' & '-to' should handle open GOPs
Mark Filipak
markfilipak.imdb at gmail.com
Sat Jun 1 20:32:33 EEST 2024
'-ss' and '-to' are flawed. They are not taking open GOPs into account. What they need to do is make
IDR cuts. They're not currently doing that.
I regret this report is so long. But It's well worth a read. I'll post a final report when all the
cuts and joins are complete.
Background.
I'm working on the 5-hour version of Bergman's FANNY AND ALEXANDER. It's four M2TSes. I'm working to
cut both ends of each M2TS (introductions and credits) -- symbolized by lower case letters -- and
then concatenating what's left -- symbolized by upper case letters -- into a giant MP4.
Schematically, the process is this:
aaaAAA..AAAaaa //00305.m2ts
bbbBBB..BBBbbb //00306.m2ts
cccCCC..CCCccc //00307.m2ts
dddDDD..DDDddd //00308.m2ts
AAA..AAA //00305 cut.m2ts
BBB..BBB //00306 cut.m2ts
CCC..CCC //00307 cut.m2ts
DDD..DDD //00308 cut.m2ts
AAA..AAABBB..BBBCCC..CCCDDD..DDD //FANNY AND ALEXANDER.mp4
Making the cuts via '-ss' & '-to' did not go well. The joins were not smooth and MP4 playback had
timing problems. The timing problems were about 5 seconds of stutter at each join.
This is a report of the first step: Cut-cut of 00305.m2ts to make '00305 cut.m2ts'.
What this all means:
If you use '-ss' and/or '-to' to make cuts _and_ the videos have open GOPs, then when you
concatenate them the result will not play properly.
What I've learned so far:
1 '-ss' and '-to' do not cut open GOPs properly.
2 Stick with M2TS. Changing to anything else changes the time_base,
which makes TS values that are different and can't be tracked or verified.
3 '-bsf noise' and '-c:v libx265' can't be used together. It's unknown why
that is. The problem is probably caused by any transcoding, or it may be
particular to x265. It merits further investigation.
What follows is my actual CLI command. It shows how meticulous the process has to be. It shows what
'-ss' and '-to' should be doing.
Regards --Mark.
: This cuts 00305.m2ts and makes '00305 cut.m2ts'.
:
: here is the procedure to make IDR cuts at both ends:
:
:=======================================================================
: The beginning is at 0:20.062, which is the "Svensk Filmindustri" logo.
: 0:20.062 is N = seconds*ticks-per-second/ticks-per-frame
: = 20.062*90000/3753.75 = 481.
: check '00305 showinfo.txt' to confirm that N = 481 is an I-frame.
: It is, and it has PTS = 2854113.
:
: FFmpeg's '-f framecrc' reports:
: ________00305.m2ts________ ______00305 cut.m2ts______
: DTS PTS
: 0, 2835345, 2846606, [P]
: 1, 2836080, 2836080, A (is audio)
: :
: 1, 2838960, 2838960, A
: 0, 2839098, 2839098, [B]
: 1, 2839920, 2839920, A
: :
: 1, 2842800, 2842800, A
: 0, 2842852, 2842852, [B]
: 1, 2843760, 2843760, A
: :
: 1, 2845680, 2845680, A
: 0, 2846606, 2854113, 0, 2850360, 2854113, [I] <<==
: 1, 2846640, 2846640, A
: :
: 1, 2849520, 2849520, A
: 0, 2850360, 2850360, [B]
:
: Note that '-f framecrc' shows the frames in DTS-order. That's not the
: physical order on the disc. The physical order is PTS-order.
:
: Here's a PTS- DTS-ordered diagram of the report [note]:
: open GOP ———————————————————————————————————>:
: 2839098 2842852 2846606 : 2854113
: / / / : /
: PTS A .. A B A .. A B A .. A P A .. A B I (continues)
: _¦____¦__¦__¦____¦__¦__¦____¦_/ _¦____¦__¦_________/
: / ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ / ¦ ¦ ¦
: DTS P A A B A A B A A I A A B
: \ \ \
: 2835345 2846606 2850360
:
: On the I-frame (2854113), cut (PTS<2854113)
: PTS I
: ___________________/
: /
: DTS I
:
: Change DTS=2846606 to 2850360
: PTS I
: ________/
: /
: DTS I ———————> I <<==
: \
: 2850360
:
: [note] The group of pictures (GOP) is shown as though it has PTS & DTS
: streams, but that's just to make time and physical frame order easier
: to visualize. It is actually one stream of course, and PTSes and DTSes
: are actually just numbers.
:
:=======================================================================
: The ending is at 1:33:10.835, which is a few black frames after
: fadeout & before the credits.
: 1:33:10.835 is N = (3600+33*60+10.835)*90000/3753.75 = 134046.
: Check '00305 showinfo.txt' to confirm that N = 134046 is an I-frame.
: It is, and it has PTS = 504223732.
:
: FFmpeg's '-f framecrc' reports:
: ________00305.m2ts________ ______00305 cut.m2ts______
: DTS PTS DTS PTS
: 0, 504204963, 504216225, 0, 504204963, 504216225, [P]
: 1, 504205680, 504205680, 1, 504205680, 504205680, A (is audio)
: : :
: 1, 504208560, 504208560, 1, 504208560, 504208560, A
: 0, 504208717, 504208717, 0, 504208717, 504208717, [B]
: 1, 504209520, 504209520, 1, 504209520, 504209520, A
: : :
: 1, 504212400, 504212400, 1, 504212400, 504212400, A
: 0, 504212471, 504212471, 0, 504212471, 504212471, [B]
: 1, 504213360, 504213360, 1, 504213360, 504213360, A
: : :
: 1, 504215280, 504215280, 1, 504215280, 504215280, A
: 0, 504216225, 504223732, 0, 504216225, 504219978, [I] <<==
: 1, 504216240, 504216240, A
: :
: 1, 504219120, 504219120, A
: 0, 504219978, 504219978, [B]
:
: Here's a PTS- DTS-ordered diagram of the report:
: open GOP ———————————————————————————————————>:
: 504208717 504212471 504216225 : 504223732
: / / / : /
: PTS A .. A B A .. A B A .. A P A .. A B A .. A I
: _¦____¦__¦__¦____¦__¦__¦____¦_/ _¦____¦__¦__¦____¦_/
: / ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ / ¦ ¦ ¦ ¦ ¦
: DTS P A A B A A B A A I A A B A A
: \ \ \
: 504204963 504216225 504219978
:
: On the final B-frame (504219978), cut (DTS>=504219978)
: PTS A .. A B A .. A B A .. A P A .. A I
: _¦____¦__¦__¦____¦__¦__¦____¦_/ _¦____¦____________/
: / ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ / ¦ ¦
: DTS P A A B A A B A A I A A
:
: Change PTS=504223732 to 504219978 504219978
: /
: PTS A .. A B A .. A B A .. A P A .. A I <——————— I <<==
: _¦____¦__¦__¦____¦__¦__¦____¦_/ _¦____¦_/
: / ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ / ¦ ¦
: DTS P A A B A A B A A I A A
ffmpeg -copyts -i "g:\FANNY AND ALEXANDER [1982(1983)]\00305.m2ts" -map 0 -bsf
noise=drop='lt(pts\,2854113)+gte(dts\,504219978)',setts=dts='if(eq(DTS\,2846606)\,2850360\,DTS)':pts='if(eq(PTS\,504223732)\,504219978\,PTS)'
-c copy -sn -dn -muxdelay 0 "g:\FANNY AND ALEXANDER [1982(1983)]\00305 cut.m2ts"
pause
More information about the ffmpeg-user
mailing list