[Libav-user] Once again about seeking in video file

adev dev androiddevmar11 at gmail.com
Tue Dec 2 08:56:49 CET 2014


Hello,

based on some chunks of code from internet I did following. This gives me
the best acurancy:

// tsms- time in milliseconds


int64_t frame = av_rescale(tsms, video_stream->time_base.den,
video_stream->time_base.num);

frame /= 1000;

if (avformat_seek_file(format_context, video_stream_id, 0, frame, frame,
AVSEEK_FLAG_FRAME)< 0) {

                return 0;

}

avcodec_flush_buffers(video_codec_ctx);

int64_t pts= 0;

do {

if (av_read_frame(format_context, &packet) >= 0) {

if (packet.stream_index == video_stream_id) {

avcodec_decode_video2(video_codec_ctx, decoded_video_frame, &frameFinished,

&packet);

}

} else {

LOGE("Seek failed");

break;

}

// calculate PTS:

pts = av_frame_get_best_effort_timestamp (decoded_video_frame);

pts = av_rescale_q ( pts,  video_stream->time_base, AV_TIME_BASE_Q );

} while ( pts/1000 < tsms);

av_free_packet(&packet);


I would appreciate If somebody could look at it and check if something can
be improved. Thanks!



On 28 November 2014 at 22:25, adev dev <androiddevmar11 at gmail.com> wrote:

> 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:
>
> AVRational timeBase = video_stream->time_base;
>
> int64_t seek_pos = (int64_t)(((float)tsms)/1000 * (float)AV_TIME_BASE);
>
> int64_t seek_target = av_rescale_q(seek_pos, AV_TIME_BASE_Q, timeBase);
>
> av_seek_frame(format_context, video_stream_id, seek_target,
> AVFMT_SEEK_TO_PTS);
>
> 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?
>
> For example seeking is not working with MPEG-4 Visual file. I am using
> FFMPEG 2.1. Thanks!
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20141202/5cd25944/attachment.html>


More information about the Libav-user mailing list