title: FFMPEG: embed current time in milliseconds into video
body: <p>I would like to embed the computer's local time <strong><em>in milliseconds</em></strong> into a stream using FFMPEG.</p>
  
  <p>I can embed the local time in seconds using drawtext like so:</p>
  
  <pre><code>ffmpeg -i &lt;input&gt; -vf "drawtext=text='%{localtime\:%T}'" -f flv &lt;output&gt;
  </code></pre>
  
  <p>Looking through the documentation for drawtext, doing some tests, and Googling around, it seems like <code>localtime</code> and <code>gmtime</code> don't provide milliseconds -- just seconds. However the <code>pts</code> options <strong><em>does</em></strong> have milliseconds:</p>
  
  <pre><code>ffmpeg -i &lt;input&gt; -vf "drawtext=text='%{pts\:hms}'" -f flv &lt;output&gt;
  </code></pre>
  
  <p>I also found out that <code>pts</code> supports an offset, so I was able to use something like this to display local time (kind of):</p>
  
  <pre><code>ffmpeg -i &lt;input&gt; -vf "drawtext=text='%{pts\:hms\:$(date +%s.%N)}'" -f flv &lt;output&gt;
  </code></pre>
  
  <p>This had two problems:</p>
  
  <ol>
  <li>It displayed something like <code>17499:17:29</code> for the time... I found a (rather complex) way to work around this</li>
  <li>It uses the server's current time <strong>when you first run the ffmpeg command</strong> as the offset -- not the server's current time when ffmpeg actually starts decoding video. I noticed ffmpeg had about a 2-second startup time, causing the clock to be off by roughly 2 seconds</li>
  </ol>
  
  <p>Is there a way to modify my solution, or an entirely separate solution, that can embed server local time in milliseconds into the stream?</p>