简体   繁体   中英

How to seek by msec with ffmpeg?

I am trying to seek in video by milliseconds with ffmpeg. I have been trying to use code from this question , which uses avformat_seek_file (i use it with -1 for stream number and AVSEEK_FLAG_ANY flag).

After that is called, i try to read next frames, that is:

if (av_read_frame(fmt_ctx, &pkt) >= 0)
{
    int ret = 0;

    if (pkt.stream_index == video_stream_idx) {
        /* decode video frame */
        ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
        if (ret < 0) {
            fprintf(stderr, "Error decoding video frame\n");
            return ret;
        }
//do something with frame
}

However, the frame->pts of retrieved frame always holds the time of the frame that was immediatly after last frame that was read before seeking.

Edit: In spite of frame->pts forming unbroken sequence, seeking does occur. For some bizarre reason next frame i read is the first one. In fact, after i run:

   int got_frame = 0;
   do
   if (av_read_frame(fmt_ctx, &pkt) >= 0) {
       decode_packet_ro(&got_frame, 0);
       av_free_packet(&pkt);
   }
   else
   {
       read_cache = true;
       pkt.data = NULL;
       pkt.size = 0;
       break;
   }
   while(!got_frame || this->frame->pts*av_q2d(video_dec_ctx->time_base) * 1000 < tsms);

next frame i read is always the first one.

In the end, i was able to seek with the following code:

/*!
 * \brief ffmpeg_reader::seekMs seek to millisecond
 * \param tsms timestamp
 * \return success of seeking
 */
bool ffmpeg_reader::seekFrame(int s_frame)
{
    if (!isOk())
        return false;

   printf("\t avformat_seek_file to %d\n",s_frame);
   int flags = AVSEEK_FLAG_FRAME;
   if (s_frame < this->frame->pkt_dts)
   {
       flags |= AVSEEK_FLAG_BACKWARD;
   }

   if(av_seek_frame(fmt_ctx,video_stream_idx,s_frame,flags))
   {
       printf("\nFailed to seek for time %d",s_frame);
      return false;
   }

   avcodec_flush_buffers(video_dec_ctx);
   /*read frame without converting it*/
   int got_frame = 0;
   do
   if (av_read_frame(fmt_ctx, &pkt) == 0) {
       decode_packet(&got_frame, 0, false);
       av_free_packet(&pkt);
   }
   else
   {
       read_cache = true;
       pkt.data = NULL;
       pkt.size = 0;
       break;
   }
   while(!(got_frame && this->frame->pkt_dts >= s_frame));
   return true;
}

I did not came up with it myself, but i (sadly) can't remember where the credit is due.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM