[theora-dev] How to get the duration of a file or track?

Shane Stephens shane.stephens at gmail.com
Mon Aug 27 16:39:00 PDT 2007

Hi Ken,

The total duration of the content is not explicitly recorded anywhere in an
Ogg file.  This is why, as Ralph mentioned (and in a little more detail
here), you need to:

(1) Split the content into chain segments
(2) extract the first and last data packet from each segment, for all tracks
(3) recover / calculate the granulepos values for the first and last data
packets (*)
(4) convert the granulepos values from the first and last data packets into
time values
(5) take the largest difference between start and end time as the total
chain segment time
(6) sum the chain segment times to create the total content length

(*) granulepos values are recorded per page, not per packet.  Hence, if the
first data packet is not the last packet on the page then you will need to
reconstruct a granulepos value for it.

In practise, many libraries do not support chained ogg files due to the
additional complexity that they present.  If you are happy not to support
chained oggs then you still need to do steps 2 through 5.  However, we would
recommend including support for chained oggs as they are part of the

Unfortunately, liboggplay does not provide a simple way to determine the
length of an ogg file.  Instead, you might want to look at liboggz, and in
particular a utility called oggzinfo:

shans at igor:~$ oggzinfo /var/local/share/media/dart/CCFilm.ogg
Content-Duration: 00:04:49.655

Theora: serialno 1433232920
        7245 packets in 1551 pages, 4.7 packets/page
        Video-Framerate: 25.000 fps
        Video-Width: 384
        Video-Height: 288

Vorbis: serialno 0642835574
        20824 packets in 636 pages, 32.7 packets/page
        Audio-Samplerate: 44100 Hz
        Audio-Channels: 2

Please note that oggzinfo actually passes right through the file twice in
order to gather these statistics - this is not necessary if you simply want
to determine the Content-Duration.  I'm also not 100% sure whether liboggz
correctly supports chained oggs.

    -Shane Stephens

On 8/28/07, Ken Larson <forum at larsontechnologies.com> wrote:
>  Ralph -
> Thanks, that will come in handy, because I do eventually need to try to
> determine duration of individual frames.
> Actually, I phrased my question badly though:  What I originally wanted to
> know was how to find the total duration of the movie.
> Thanks,
> Ken
> Ralph Giles wrote:
> On Thu, Aug 23, 2007 at 07:13:17AM -0400, Ken Larson wrote:
>  I'm starting with the theora player_example.c.  I want to be able to
> determine the duration of the audio/video ahead of time, to be able to
> display it somewhere.
> I can't see anything obvious in ogg.h, theora.h, or vorbis.h for this.
>  It's a little complicated. First you bisection-search based on the ogg
> page serial numbers to find all the chain segments. Then you read the
> bos pages for each chain segment to identify the codecs and get their
> granulepos mappings. Then you read the first and last data packets for
> each stream in each chain segment and use those to calculate the
> duration of each segment. The length of the ogg stream is the sum of the
> durations of each segment.
>  Can anyone help me with the right place to look to do this?
>  You might take a look at liboggplay; it's a convenience library that
> does this for general streams, as well as general playback marshalling.
>   http://svn.annodex.net/liboggplay/trunk/include/oggplay/oggplay.h
> HTH,
>  -r
> _______________________________________________
> theora-dev mailing list
> theora-dev at xiph.org
> http://lists.xiph.org/mailman/listinfo/theora-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.xiph.org/pipermail/theora-dev/attachments/20070828/59cc62ef/attachment.html

More information about the theora-dev mailing list