[Libav-user] use AVPacket.pts with av_seek_frame()

Fabio Tiriticco fabio.europe at gmail.com
Tue Jul 17 10:12:10 CEST 2012

Hi Alexandre,

thanks for your reply and apologies for the delayed reaction.
As an experiment, I am first reading through all the packets, so that I
know that I have an X number of packets. Each packet has a pts value, from
1 to X.

Later on I would like the user to be able to jump to any packet, and then I
need av_seek_frame() to move the header to the right timestamp.  My
reasoning to move it to packet Y has been:

const int64_t pti = pFormatContext.duration / (int64_t)X; // duration of a
const int64_t packetTimestamp = Y * pti;
av_seek_frame(pFormatCtx, videoStream, packetTimestamp * AV_TIME_BASE,

to test it, I do this in a loop and try to extract the first ten packets
and use the

avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);

after this I check which packet has been extracted from packet.pts.
At this point, when I do ten times in a row the av_seek_frame() call with X
going from 1 to 10, the avcodec_decode_video2() call, the packet.pts that I
extract is always the same number (250) and indeed if I print the frame on
screen it is always the same frame.

I will keep trying. At the moment I am trying to build this in a JNI
environment but I think I'll go to a simple C program to play around with.

Thank you very much for your attention and help,

On Mon, Jul 16, 2012 at 3:45 PM, Alexandre Millette <amillett at matrox.com>wrote:

> Hi Fabio.
> I can't say I have much experience with av_seek_frame(), but since no one
> else answered you, I might as well try to help.
> > Every time a read a packet I can see its position
> > in the timeframe with AVPacket.pts.
> > Now I am struggling with the seek functionality.
> So you are first reading the entire video and storing the pts value for
> later use?
> Because if you have the AVPacket, then you have the encoded frame. The
> only use of av_seek_frame with pts would be to go back to a packet that you
> previously read but have since lost.
> > av_seek_frame(pFormatCtx, videoStream, pts, backwardFlag)
> What exactly do you mean when you say that it doesn't work? Does an
> exception occur? Does the current frame change at all?
> av_seek_frame will only move the position of the file reader, meaning that
> you have to read the packet again and the decode it.
> Also be careful with pts... the doc specifies that it is a timestamp in
> AVStream->time_base units, but some tutorials (like
> http://dranger.com/ffmpeg/**tutorial07.html<http://dranger.com/ffmpeg/tutorial07.html>) suggest that this timestamp should be multiplied by AV_TIME_BASE.
> I guess a bit more code would be better if you wish for more help.
> Alex M
> ______________________________**_________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/**listinfo/libav-user<http://ffmpeg.org/mailman/listinfo/libav-user>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20120717/3c523595/attachment.html>

More information about the Libav-user mailing list