How do I calculate the duration of an MP3 file in a remote location without downloading the entire file?

duration, mp3, php

Solution

Almost all mp3 files have Xing/VBRi frame at the beginning of the file that can be used to calculate the duration of the file with very little download.

In PHP you would probably:

- read the first 10 bytes to get the size of the id3 part

- read n amount of bytes where n is id3 part length

- read the amount of bytes required for Xing/VBRi frame

- parse and close connection

Read http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header#XINGHeader for how to parse Xing and VBRi frame.

Problem

I run an independent music website. We have a huge repository of MP3 files stored on our Amazon S3 CDN. We never cared to store the duration of the audio in the database while uploading. Now, I need the length of each of these files in minutes and seconds. I am not sure if the TLEN ID3 info is set in all the files, but I know for a fact that all files are 128kbps bit rate. Since I know the number of mp3 is very large, I don't want to download the entire file for calculating its audio length. I was wondering if there was a smarter way to do this.

Original source

Related problems