Reputation: 3618
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.
Upvotes: 6
Views: 3588
Reputation: 3618
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.
Upvotes: 4