20

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.

2 Answers 2

31

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.

7
  • How can streams have different starting PTS? Don't the streams start from PTS=0 ?
    – ed22
    Commented Oct 7, 2019 at 12:26
  • 4
    Broadcast 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.
    – Gyan
    Commented Oct 7, 2019 at 12:30
  • Would you please tell me : Does setpts change timing info that stored in the container? Or in the bitstream ?(VUI/SPS etc ?)
    – Maria
    Commented Jun 1, 2021 at 20:15
  • 3
    setpts will ultimately affect the packet timestamps, so container.
    – Gyan
    Commented Jun 2, 2021 at 4:18
  • Thank you very much, And please tell me bsf-setts=pts filter where exactly does affect? (again container ?)
    – Maria
    Commented Jun 2, 2021 at 5:12
4

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

3
  • "trimming values"?
    – ed22
    Commented Oct 8, 2019 at 8:40
  • Trimming time stamp values * Commented Oct 9, 2019 at 14:03
  • Oooh, so if I use setpts on video that I cut 8 minutes out of, I won't have my stream duration saying 2 hours anymore? Thanks!!!
    – Cliff
    Commented Jul 11, 2023 at 20:43

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