How do I calculate the length of an MP3 file in a remote location without downloading the entire file?
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 size 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 the number of mp3 is very large, I don't want to download the entire file for calculating it's audio length. I was wondering if there was a smarter way to do this.
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.