views:

106

answers:

2

why on some mp3s file when i call mime_content_type($mp3_file_path) it's return application/octet-stream?

i have this:

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $image = getimagesize($tempFile);
    $mp3_mimes = array('audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio'); 

    if (in_array(mime_content_type($tempFile), $mp3_mimes)) { 
        echo json_encode("mp3");
    } elseif ($image['mime']=='image/jpeg') {
        echo json_encode("jpg");
    } else{
        echo json_encode("error");
    }
}

EDIT: i found a nice class here:

http://www.zedwood.com/article/127/php-calculate-duration-of-mp3

+1  A: 

application/octet-stream is probably mime_content_type s fallback type when it fails to recognize a file.

The MP3 in that case is either not a real MP3 file, or - more likely - the file is a real MP3 file, but does not contain the "magic bytes" the PHP function uses to recognize the format - maybe because it's a different sub-format or has a variable bitrate or whatever.

You could try whether getid3 gives you better results. I've never worked with it but it looks like a pretty healthy library to get lots of information out of multimedia files.

If you have access to PHP's configuration, you may also be able to change the mime.magic file PHP uses, although I have no idea whether a better file exists that is able to detect your MP3s. (The mime.magic file is the file containing all the byte sequences that mime_content_type uses to recognize certain file types.)

Pekka
I had a chance to dig into getid3 and if I remember correctly it turned out that it figures out file formats by extensions only.
jayarjo
A: 

MP3 files are a strange beast when it comes to identifying them. You can have an MP3 stored with a .wav container. There can be an ID3v2 header at the start of the file. You can embed an MP3 essentially within any file.

The only way to detect them reliably is to parse slowly through the file and try to find something that looks like an MP3 frame. A frame is the smallest unit of valid MP3 data possible, and represents (going off memory) 0.028 seconds of audio. The size of the frame varies based on bitrate and sampling rate, so you can't just grab the bitrate/sample rate of the first frame and assume all the other frames will be the same size - a VBR mp3 must be parsed in its entirety to calculate the total playing time.

All this boils down to that identifying an MP3 by using PHP's fileinfo and the like isn't reliable, as the actual MP3 data can start ANYWHERE in a file. fileinfo only looks at the first kilobyte or two of data, so if it says it's not an MP3, it might very well be lying because the data started slightly farther in.

Marc B