<p>The FFMPEG does not support <em>strtime %f</em> option; therefore, the fractional second works with AVTB only. The parameter is</p>
<pre><code>-filter_complex &quot;drawtext=fontsize=90:x=20:y=20:text='pts=%{pts\:gmtime\:0\:%S %f} AVTB=%{eif\:trunc(t)\:d\:2}.%{eif\:(1M*t-1M*trunc(t))/10000\:d\:2}':box=1&quot;
</code></pre>
<p>and the result is</p>
<p><a href="https://i.sstatic.net/iMMti.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/iMMti.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2020-10-29T01:33:53.890Z
2020-10-29T01:33:53.890Z
xer-rex
<p>Since a lot of this was unfamiliar to me, I went ahead and wrote up how I worked through it in the hope that it would be useful to someone else down the road...</p>
<p>Here's our starting expression, let's break this down and go through it:</p>
<p><code>%{eif:trunc(t):d:2}.%{eif:(1M*t-1M*trunc(t))/10000:d:2}</code></p>
<p><code>eif</code> is 'evaluate_int_format', it evaluates the expression’s value and outputs as formatted integer.  From the docs:</p>
<p>The first argument is the expression to be evaluated, just as for the expr function.
The second argument specifies the output format. Allowed values are <code>x</code>, <code>X</code>, <code>d</code> and <code>u</code>. They are treated exactly as in the printf function.
The third parameter is optional and sets the number of positions taken by the output. It can be used to add padding with zeros from the left.... so <code>:d:2</code> means a decimal integer padded to two digits.</p>
<p><code>t</code> is the timestamp in seconds.  ffmpeg appears to keep this down to millisecond accuracy, so by manipulating this we can get the output we want.</p>
<p>So:</p>
<p><code>%{eif:trunc(t):d:2}</code>
trunc(t) is just the integer part of the timestamp.
the <code>eif</code> call here just formats that to two zero-padded digits.</p>
<p><code>(1M*t-1M*trunc(t))/10000</code>
this is slick: <code>1M*t</code> basically shifts the whole timestamp by 10e6, which gives us the total number of microseconds as an integer - or said another way we just cast the timestamp to an integer...
similarly: <code>1M*trunc(t)</code> rounds down that value to the nearest second.</p>
<p><code>1.746826 seconds</code> becomes <code>1,746,826 microseconds</code> which then trunc's down to <code>1,000,000 microseconds</code>.</p>
<p>subtracting them leaves us with just the remainder (in microseconds).</p>
<p>Since we want the output in hundredths of seconds, we divide by 10000 (really another shift) and this gives us the fractional leftovers, as another integer, which we then format to two digits with zero padding.</p>
<p><a href="https://ffmpeg.org/ffmpeg-utils.html" rel="nofollow noreferrer">ffmpeg-utils docs</a></p>
<p><a href="http://ffmpeg.org/ffmpeg-all.html#Text-expansion" rel="nofollow noreferrer">ffmpeg text expansion docs</a></p>

CC BY-SA 4.0
2020-10-31T20:01:33.803Z
2020-10-31T20:01:33.803Z
ljwobker
<p>I've been able to get most of the manipulations I want on various PTS/timestamp things.. except this one.  I have a case where we have a running PTS that gets stamped via drawtext.  I currently use the <code>%{pts:flt}</code> which outputs millisecond accurate seconds.  This is great, but I ultimately need to format this in the format <code>SS.ff</code>, by which I mean we always keep <em>only</em> the last two digits of the seconds, and we ONLY print two significant digits of the fractional (micro/milli) second portion.</p>
<p>I've been through a truckload of various gmtime/strftime() attempts, and I'm stymied in part because it appears that only <code>flt</code> provides the fractional seconds part that I want, and it doesn't look like strftime() has support for width/padding/truncating?</p>

<ffmpeg><timecode>
is there a way to manipulate (pad, slice) a computed PTS timestamp within ffmpeg / drawtext?
ljwobker
# is there a way to manipulate (pad, slice) a computed PTS timestamp within ffmpeg / drawtext?

(Asked by: username_0 on 2020-10-28)

I've been able to get most of the manipulations I want on various PTS/timestamp things.. except this one. I have a case where we have a running PTS that gets stamped via drawtext. I currently use the `%{pts:flt}` which outputs millisecond accurate seconds. This is great, but I ultimately need to format this in the format `SS.ff`, by which I mean we always keep *only* the last two digits of the seconds, and we ONLY print two significant digits of the fractional (micro/milli) second portion.

I've been through a truckload of various gmtime/strftime() attempts, and I'm stymied in part because it appears that only `flt` provides the fractional seconds part that I want, and it doesn't look like strftime() has support for width/padding/truncating?

**Tags:** ffmpeg, timecode


---

## 2 answers

### Answer by username_1 on 2020-10-29. Score: 2 | Accepted answer

The FFMPEG does not support strtime %f option; therefore, the fractional second works with AVTB only. The parameter is
-filter_complex "drawtext=fontsize=90:x=20:y=20:text='pts=%{pts\:gmtime\:0\:%S %f} AVTB=%{eif\:trunc(t)\:d\:2}.%{eif\:(1M*t-1M*trunc(t))/10000\:d\:2}':box=1"

and the result is


---

### Answer by username_0 on 2020-10-31. Score: 0

Since a lot of this was unfamiliar to me, I went ahead and wrote up how I worked through it in the hope that it would be useful to someone else down the road...

Here's our starting expression, let's break this down and go through it:

`%{eif:trunc(t):d:2}.%{eif:(1M*t-1M*trunc(t))/10000:d:2}`

`eif` is 'evaluate\_int\_format', it evaluates the expression’s value and outputs as formatted integer. From the docs:

The first argument is the expression to be evaluated, just as for the expr function.
The second argument specifies the output format. Allowed values are `x`, `X`, `d` and `u`. They are treated exactly as in the printf function.
The third parameter is optional and sets the number of positions taken by the output. It can be used to add padding with zeros from the left.... so `:d:2` means a decimal integer padded to two digits.

`t` is the timestamp in seconds. ffmpeg appears to keep this down to millisecond accuracy, so by manipulating this we can get the output we want.

So:

`%{eif:trunc(t):d:2}`
trunc(t) is just the integer part of the timestamp.
the `eif` call here just formats that to two zero-padded digits.

`(1M*t-1M*trunc(t))/10000`
this is slick: `1M*t` basically shifts the whole timestamp by 10e6, which gives us the total number of microseconds as an integer - or said another way we just cast the timestamp to an integer...
similarly: `1M*trunc(t)` rounds down that value to the nearest second.

`1.746826 seconds` becomes `1,746,826 microseconds` which then trunc's down to `1,000,000 microseconds`.

subtracting them leaves us with just the remainder (in microseconds).

Since we want the output in hundredths of seconds, we divide by 10000 (really another shift) and this gives us the fractional leftovers, as another integer, which we then format to two digits with zero padding.

[ffmpeg-utils docs](https://ffmpeg.org/ffmpeg-utils.html)

[ffmpeg text expansion docs](http://ffmpeg.org/ffmpeg-all.html#Text-expansion)


---

