4

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.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Munim
  • 6,310
  • 2
  • 35
  • 44
  • 2
    I think that was already answered here: http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file –  Jun 11 '12 at 06:58
  • 1
    @sputnik I am not talking about the file size in bytes. I am talking about the length of the audio in the MP3 file in minutes and seconds. – Munim Jun 11 '12 at 13:11

1 Answers1

5

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.

Esailija
  • 138,174
  • 23
  • 272
  • 326