<div dir="ltr"><div>Hi!</div><div>I am trying to figure out the right way to convert PTS during demuxing to frame number.</div><div>I came up with the following code:</div><div><br></div><div>
<pre class="gmail-lang-c gmail-s-code-block"><code class="gmail-hljs gmail-language-c"><span class="gmail-hljs-type">int64_t</span> <span class="gmail-hljs-title gmail-function_">timestamp_to_frame</span><span class="gmail-hljs-params">(Demuxer* demuxer, <span class="gmail-hljs-type">int64_t</span> pts)</span>
 {
     AVStream* stream = demuxer->fmtc->streams[demuxer->iVideoStream];
     <span class="gmail-hljs-keyword">if</span> (!stream || pts < <span class="gmail-hljs-number">0</span>)
     {
         <span class="gmail-hljs-built_in">fprintf</span>(<span class="gmail-hljs-built_in">stderr</span>, <span class="gmail-hljs-string">"Invalid stream or PTS.\n"</span>);
         <span class="gmail-hljs-keyword">return</span> <span class="gmail-hljs-number">-1</span>;
     }

     <span class="gmail-hljs-comment">// Get the frame rate (FPS)</span>
     <span class="gmail-hljs-type">const</span> <span class="gmail-hljs-type">double</span> fps = get_frame_rate(stream);<span class="gmail-hljs-comment">//here returns 25.0</span>

     <span class="gmail-hljs-comment">// Stream time base (e.g., 1/25000 for my case)</span>
     <span class="gmail-hljs-type">const</span> AVRational timeBase = stream->time_base;

     <span class="gmail-hljs-comment">// Calculate frame duration in seconds</span>
     <span class="gmail-hljs-type">const</span> <span class="gmail-hljs-type">double</span> frameDurationInSeconds = <span class="gmail-hljs-number">1.0</span> / fps;

     <span class="gmail-hljs-type">const</span> <span class="gmail-hljs-type">int64_t</span> ptsIncrement = (<span class="gmail-hljs-type">int64_t</span>)((frameDurationInSeconds * timeBase.den) * frameDurationInSeconds + <span class="gmail-hljs-number">0.5</span>);
     <span class="gmail-hljs-comment">// Calculate the frame number by dividing PTS by the increment</span>
     <span class="gmail-hljs-type">const</span>  <span class="gmail-hljs-type">int64_t</span> frameNumber = pts / ptsIncrement;

<span class="gmail-hljs-keyword">return</span> frameNumber;

}</code></pre>

</div><div>It works correctly, but I am not sure this is the right way to do that and that it will work with any fps/time scale. Could someone review this code and provide feedback, or an alternative way? <br></div><div><br></div><div>Much appreciated,</div><div>Mike.<br></div></div>