Seeking in an MPEG-2 Program stream

Andy Bennett andyjpb at
Fri Nov 19 03:21:05 PST 2004


Google seems unhelpful here so I was wondering if anyone here would be able
to help me.
I have some MPEG-2 Program Streams and I want to play them out using a
Visual Circuits 4ReelPro card. This in itself is not difficult.
Now, the API lets you jump to a given byte offset in the file provided that
it lands on a harddisk sector boundry. However, what I want to do is jump to
a particular frame in the stream. Specifically, we have 15 seconds of colour
bars and tone and then 15 seconds of countdown clock on our archives. i.e.
When we play these out we want to skip the first 30 seconds.
For each MPEG I have the bit rate and the number of seconds of "bars &

Does anyone know how to calculate the byte offset in the file from this

>From my googling, I fear that it is particularly difficult for the following
- Your best hope is to find the keyframe before the point you want and then
scan forward.
- The audio and video in the MPEG file are not locked together, so syncing
them up after a seek might prove difficult. (Although I'm hoping the
4ReelPro card will take care of this)
- The MPEG may require a TOC to be generated. - This is fine, we can
generate them. (Well, I'm assuming that ffmpeg or similar will be able to
when I investigate it further)
- Depending on the frequency of keframes & what other parameters you select
at encode time affects where in the file the particular frame falls. i.e.
MPEGs of the same bitrate are not equal, so I cannot just calculate it once
and forget. If it turns out that I need more meta data such as frequency of
keyframes, etc. then I can  either cause the database to become populated
with it or work it out from the MPEG headers, etc.

So, can anyone offer any insight into this or tell me where I can find out
more information? (I don't particularly want to read the MPEG specs cover to
cover. - Perhaps that's lazyness on my part.)


andyjpb at

More information about the lfs-chat mailing list