为什么mime_content_type上的某些mp3返回应用程序/八位字节流 [英] why some mp3s on mime_content_type return application/octet-stream

查看:96
本文介绍了为什么mime_content_type上的某些mp3返回应用程序/八位字节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么当我调用mime_content_type($ mp3_file_path)时在某些mp3s文件上返回应用程序/八位字节流?

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

我有这个:

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");
    }
}

我在这里找到了一个不错的班级:

i found a nice class here:

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

推荐答案

MP3文件在识别它们时是一种奇怪的野兽.您可以将MP3与.wav容器一起存储.文件开头可以有一个ID3v2标头.您基本上可以将MP3嵌入任何文件中.

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.

唯一可靠地检测到它们的方法是缓慢地分析文件并尝试查找类似于MP3帧的内容.帧是可能的有效MP3数据的最小单位,表示(退出存储)0.028秒的音频.帧的大小根据比特率和采样率而变化,因此,您不能仅获取第一帧的比特率/采样率,并假设所有其他帧的大小相同-必须完整解析VBR mp3计算总播放时间.

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.

所有这些归结为使用PHP的fileinfo等来识别MP3的方法是不可靠的,因为实际的MP3数据可以从文件的任何位置开始. fileinfo仅查看数据的前一千字节,因此,如果说它不是MP3,则很可能是在撒谎,因为数据的起始位置稍远.

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.

这篇关于为什么mime_content_type上的某些mp3返回应用程序/八位字节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆