<div dir="ltr">







<p class="">Hello, </p><p class="">based on some chunks of code from internet I did following. This gives me the best acurancy:<br></p><p class=""><span class="" style="white-space:pre"> </span>// tsms- time in milliseconds</p><p class=""><br></p><p class=""><span class="" style="white-space:pre">       </span>int64_t frame = av_rescale(tsms, video_stream->time_base.den, video_stream->time_base.num);</p><p class=""><span class="" style="white-space:pre">   </span>frame /= 1000;</p><p class=""><span class="" style="white-space:pre">      </span>if (avformat_seek_file(format_context, video_stream_id, 0, frame, frame, AVSEEK_FLAG_FRAME)< 0) {</p><p class="">                return 0;</p><p class=""><span class="" style="white-space:pre"> </span>}</p><p class=""><span class="" style="white-space:pre">   </span>avcodec_flush_buffers(video_codec_ctx);<br></p><p class=""><span class="" style="white-space:pre">   </span>int64_t pts= 0;<br></p><p class=""><span class="" style="white-space:pre">   </span>do {</p><p class=""><span class="" style="white-space:pre">                </span>if (av_read_frame(format_context, &packet) >= 0) {</p><p class=""><span class="" style="white-space:pre">                   </span>if (packet.stream_index == video_stream_id) {</p><p class=""><span class="" style="white-space:pre">                               </span>avcodec_decode_video2(video_codec_ctx, decoded_video_frame, &frameFinished,</p><p class=""><span class="" style="white-space:pre">                                             </span>&packet);</p><p class=""><span class="" style="white-space:pre">                       </span>}</p><p class=""><span class="" style="white-space:pre">           </span>} else {</p><p class=""><span class="" style="white-space:pre">                    </span>LOGE("Seek failed");</p><p class=""><span class="" style="white-space:pre">                      </span>break;</p><p class=""><span class="" style="white-space:pre">              </span>}</p><p class=""><span class="" style="white-space:pre">           </span>// calculate PTS:</p><p class=""><span class="" style="white-space:pre">           </span>pts = av_frame_get_best_effort_timestamp (decoded_video_frame);</p><p class=""><span class="" style="white-space:pre">             </span>pts = av_rescale_q ( pts,  video_stream->time_base, AV_TIME_BASE_Q );</p><p class=""><span class="" style="white-space:pre">   </span>} while ( pts/1000 < tsms);</p><p class=""><span class="" style="white-space:pre">      </span>av_free_packet(&packet);</p><p class=""><br></p><p class="">I would appreciate If somebody could look at it and check if something can be improved. Thanks!</p><p class=""><br></p></div><div class="gmail_extra"><br><div class="gmail_quote">On 28 November 2014 at 22:25, adev dev <span dir="ltr"><<a href="mailto:androiddevmar11@gmail.com" target="_blank">androiddevmar11@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I saw many threads about seeking in video files with FFMPEG. But still it is not clear for me how to seek to specific location (in milliseconds) with good accuracy. I know that to seek in video file it is needed to seek than decode frames and check where we are, than seek again decode frames etc. till we reach the exact moment we need. The best result with seeking I have using this code:<div><br></div><div>







<p><span>AVRational</span> timeBase = video_stream-><span>time_base</span>;</p><p><span>int64_t</span> seek_pos = (<span>int64_t</span>)(((<span>float</span>)tsms)/1000 * (<span>float</span>)AV_TIME_BASE);</p><p><span>int64_t</span> seek_target = av_rescale_q(seek_pos, AV_TIME_BASE_Q, timeBase);</p><p>










</p><p>av_seek_frame(format_context, <span>video_stream_id</span>, seek_target, <span>AVFMT_SEEK_TO_PTS);</span></p><p>But still in some videos and some places in this videos seeking is not working perfectly. The question is if seeking to AVFMT_SEEK_TO_PTS makes this complex algorithm of seeking in video file to have hight accuracy? Maybe there is better implementation of seeking which considers all video formats?</p><p>For example seeking is not working with MPEG-4 Visual file. I am using FFMPEG 2.1. Thanks!</p></div></div>
</blockquote></div><br></div>