How can I seek to frame No. X with ffmpeg?

前端 未结 1 1631
谎友^
谎友^ 2020-12-28 16:13

I\'m writing a video editor, and I need to seek to exact frame, knowing the frame number.

Other posts on stackoverflow told me that ffmpeg may give

相关标签:
1条回答
  • 2020-12-28 16:37

    av_seek_frame will only seek based on timestamp to the key-frame. Since it seeks to the keyframe, you may not get what you want. Hence it is recommended to seek to nearest keyframe and then read frame by frame util you reach the desired frame.

    However, if you are dealing with fixed FPS value, then you can easily map timestamp to frame index.

    Before seeking you will need to convert your time to AVStream.time_base units if you have specified stream. Read ffmpeg documentation of av_seek_frame in avformat.h.

    For example, if you want to seek to 1.23 seconds of clip:

     double m_out_start_time = 1.23;
     int flgs = AVSEEK_FLAG_ANY;
     int seek_ts = (m_out_start_time*(m_in_vid_strm->time_base.den))/(m_in_vid_strm->time_base.num);
     if(av_seek_frame(m_informat, m_in_vid_strm_idx,seek_ts, flgs) < 0)
     {
         PRINT_MSG("Failed to seek Video ")
     }
    
    0 讨论(0)
提交回复
热议问题