Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the length of a ogg track from s3 without downloading the whole file

Tags:

python

ogg

How do I get the play length of an ogg file without downloading the whole file? I know this is possible because both the HTML5 tag and VLC can show the entire play length immediately after loading the URL, without downloading the entire file.

Is there a header or something I can read. Maybe even the bitrate, which I can divide by the file size to get an approximate play length?

like image 957
priestc Avatar asked Feb 25 '13 04:02

priestc


2 Answers

Unfortunately there does not appear to be a way to achieve this.

Mozilla's Configuring servers for Ogg media is very instructive. Basically:

  1. Gecko uses the X-Content-Duration header - sent by the web server if it has it. This explains the HTML5 audio streaming example you raised. If missing, then
  2. Gecko estimates the length based on the sample-rate (in the header) and the size of the file from the Content-length HTTP header

The sample rate is stored in the Identification Header - the first header packet. See the specification go to section "4.2 Header decode and decode setup"

like image 99
Andrew Alcock Avatar answered Nov 14 '22 07:11

Andrew Alcock


This is possible. The way to do it is to use HTTP range requests to fetch the end of the file, find the last Ogg page, and extract the timestamp from it. This is assuming that the file consists of contiguous streams (i.e. no chaining) which all have the same length, and that the stream time starts at 0 (otherwise, you should decode the beginning of the stream and subtract that timestamp from the final timestamp). Decoding the timestamp from the Ogg Page granulepos field is codec-specific (e.g. for Vorbis it is expressed as a number of samples).

Alternatively, if your Ogg file has Ogg Skeleton metadata, you can read that directly to determine the duration of the file.

like image 44
daf Avatar answered Nov 14 '22 08:11

daf