What does ffmpeg's "setpts" filter do exactly?
Solution 1
PTS stands for Presentation TimeStamps. See What is video timescale, timebase, or timestamp in ffmpeg?
The setpts filter evaluates the expression and assigns the value as the timestamp for the current frame it is processing
e.g. setpts=2*N+5/TB
where N is frame index starting from 0, and TB is the timebase of the stream. Let's say it is 1/1000, so each PTS unit is 1 millisecond.
So, for each frame, it would go as follows,
N expression New PTS New PTS time
0 2*0+5/(1/1000) 5000 5.000 sec
1 2*1+5/(1/1000) 5002 5.002 sec
2 2*2+5/(1/1000) 5004 5.004 sec
...
Filters which work upon multiple inputs sync by timestamp i.e. in overlay filter, the filter will overlay overlay input with timestamp 5.0 upon main input with PTS time 5.0. If the streams have different starting PTS, this can lead to unexpected output, so timestamps are reset so each stream starts from 0. Of course, if you have a custom sync in mind, then you would modify the setpts expr accordingly.
Another reason is that when a stream has a non-zero starting timestamp, ffmpeg may duplicate frames in -vsync cfr
mode to plug the gap from timestamp 0 till that initial timestamp. This is only relevant in a few scenarios.
Solution 2
When trimming values you can often run into issues where the start isn’t from 0 anymore. So when using the -ss and -t flag you might want to be re setting that
ed22
Updated on June 14, 2022Comments
-
ed22 about 2 years
I was looking for a very long time and everywhere I look it's just like it was something obvious, a common knowledge. What does the "setpts" filter of ffmpeg do exactly? Why would you want to reset it to zero with
setpts=PTS-STARTPTS
? Thanks. -
ed22 over 4 yearsHow can streams have different starting PTS? Don't the streams start from PTS=0 ?
-
Gyan over 4 yearsBroadcast feeds sent as MPEG transport streams have non-zero starting PTS. A stream cut using trim filter can have non-zero starting PTS. There are many other ways.
-
ed22 over 4 years"trimming values"?
-
Michael Goodrum over 4 yearsTrimming time stamp values *
-
Maria about 3 yearsWould you please tell me : Does
setpts
change timing info that stored in the container? Or in the bitstream ?(VUI/SPS etc ?) -
Gyan about 3 yearssetpts will ultimately affect the packet timestamps, so container.
-
Maria about 3 yearsThank you very much, And please tell me bsf-setts=pts filter where exactly does affect? (again container ?)
-
Gyan about 3 yearsContainer as well. No code right now sets timestamps within the bitstream.