Quantcast
Channel: MediaSPIP
Viewing all articles
Browse latest Browse all 117715

Get frame time in ffmpeg

$
0
0

I am trying to make a little video player that has seek bar (with ffmpeg, of course). For that i need function that will, using data from frame and/or packet, get me current time in the video that should be set in seek slider.

It should work like this:

my_time = get_cur_time()
seek(my_time + 10)
assert(my_time+10 == get_cur_time())
seek(my_time - 10)
assert(my_time-10 == get_cur_time())

I do understand thatffmpeg does not support precise seeking, so equality here means "something reasonably cloae).

What code have i used for this thus far:

frame_time = frame->pts*av_q2d(video_dec_ctx->time_base) * 1000;

where frame is AVFrame and video_dec_ctx is AVCodecContext.

And for seeking:

 int fn = ffmpeg::av_rescale(tsms,fmt_ctx->streams[video_stream->index]->time_base.den, fmt_ctx->streams[video_stream->index]->time_base.num); int frame = fn/1000; printf("\t avformat_seek_file to %d\n",frame); int flags = AVSEEK_FLAG_FRAME; if (frame < this->frame->pts) flags |= AVSEEK_FLAG_BACKWARD; if(ffmpeg::av_seek_frame(fmt_ctx,video_stream->index,frame,flags)) { printf("\nFailed to seek for time %d",frame); return false; } avcodec_flush_buffers(video_dec_ctx); 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 >= frame));

The code does forward seeking passably, but after any attempt of backward seeking my second assertion fails. After seeking to previous position, my method of getting time does not return position less that one before seeking. That causes my seek slider to work grossly incorrectly.


Viewing all articles
Browse latest Browse all 117715

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>