检测文件是否为MP3文件? [英] Detect if a file is an MP3 file?

查看:206
本文介绍了检测文件是否为MP3文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个C ++库,用于在不同格式/编解码器之间对音频进行解码和编码.我有一个例程,可以在加载所需的编解码器库之前快速检测格式.

I'm writing a C++ library for decoding and encoding audio between different formats/codecs. I have a routine for quickly detecting the format before loading the required codec library.

对于WAV文件,可以简单地在文件开头查找ASCII值"RIFF"和"WAVE".同样适用于FLAC,我们可以简单地读取前4个字节,即"fLaC".

For WAV files one can simple look for the ASCII values "RIFF" and "WAVE" at the start of the file. The same applies to FLAC, we can simply read in the first 4 bytes, which will be "fLaC".

但是如何快速检测文件是否为MP3?我不能依靠文件扩展名.我也无法尝试解码第一个MP3帧,因为文件的开头可能会有其他数据(例如:ID3,封面图片等).

But how can I quickly detect if a file is MP3? I can't rely on the file extension. I also can't try to decode the first MP3 frame, since there might be additional data at the start of the file (eg: ID3, cover image, etc).

推荐答案

确定文件是否为MP3,比在文件中搜索固定模式要复杂得多.

Detecting if a file is an MP3 is more complicated than searching for a fixed pattern in the file.

Some concepts

(请参见 http://www.codeproject.com/Articles /8295/MPEG-Audio-Frame-Header 了解详情)

  • MP3文件由一系列帧组成,每个帧的开头都有一个标头.
  • 报头以11位同步字(均为1)在字节边界处开始.因此,同步字为0xFFE或0XFFF.
  • 每个帧的长度是根据头参数计算的.

Algorithm to determine if a file is MP3 or not

  • 在文件中搜索同步字(0xFFF或0xFFE).
  • 解析标头参数.
  • 使用标头参数确定帧长.
  • 使用帧长搜索下一帧.
  • 如果在搜索后找到另一个同步词,则该文件主要是MP3文件.
  • 可以肯定的是,重复此过程以找到N个连续的MP3帧.可以增加N以获得更好的命中率.

这篇关于检测文件是否为MP3文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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