How to seek to the end of a video stream using ffmpeg


This was really simple, but took me about a day to find out. A google search didn’t help this time so I post it here and maybe it is going to help someone else.

It turns out that the format context has a handy member: duration. This may or may not be valid, so it needs to be checked before using it. The following two lines of code will do the trick:

av_seek_frame(pFormatCtx, -1, pFormatCtx->duration, 0);

When using -1 as a stream index, the time is measured in ffmpeg’s time base, which is the same unit the duration member is in.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s