What does ffmpeg's "setpts" filter do exactly?

11,306

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

Share:
11,306
ed22
Author by

ed22

Updated on June 14, 2022

Comments

  • ed22
    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
    ed22 over 4 years
    How can streams have different starting PTS? Don't the streams start from PTS=0 ?
  • Gyan
    Gyan over 4 years
    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.
  • ed22
    ed22 over 4 years
    "trimming values"?
  • Michael Goodrum
    Michael Goodrum over 4 years
    Trimming time stamp values *
  • Maria
    Maria about 3 years
    Would you please tell me : Does setpts change timing info that stored in the container? Or in the bitstream ?(VUI/SPS etc ?)
  • Gyan
    Gyan about 3 years
    setpts will ultimately affect the packet timestamps, so container.
  • Maria
    Maria about 3 years
    Thank you very much, And please tell me bsf-setts=pts filter where exactly does affect? (again container ?)
  • Gyan
    Gyan about 3 years
    Container as well. No code right now sets timestamps within the bitstream.