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

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

问题描述

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



对于WAV文件,可以简单查找ASCII值RIFF和WAVE文件的开始。这同样适用于FLAC,我们可以简单地读取前4个字节,这将是fLaC。



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

解决方案

检测文件是否是MP3比在文件中搜索固定模式更复杂。



一些概念



=http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header =nofollow noreferrer> http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame详细信息请参阅-Header )


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



确定文件是否为MP3的算法




  • 搜索文件中的同步字(0xFFF或0xFFE)。

  • 解析标题参数。

  • 使用头部参数确定帧长度。
  • 使用帧长搜索到下一帧。

  • 如果您在查找后发现另一个同步字,则该文件主要是一个MP3文件。

  • 可以肯定,重复该过程以查找N个连续MP3帧。 N可以提高以获得更好的命中率。


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.

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".

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).

解决方案

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

Some concepts

(See http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header for details)

  • MP3 file consists of a series of frames and each frame has a header at the beginning.
  • Header starts at a byte boundary with a 11-bit sync word, which is all 1s. Hence the sync word is either 0xFFE or 0XFFF.
  • Length of each frame is calculated based on the header parameters.

Algorithm to determine if a file is MP3 or not

  • Search for the syncword in the file (0xFFF or 0xFFE).
  • Parse the header parameters.
  • Determine the frame length using the header parameters.
  • Seek to the next frame using the frame length.
  • If you find another syncword after seeking, then the file is mostly an MP3 file.
  • To be sure, repeat the process to find N consecutive MP3 frames. N can be increased for a better hit-rate.

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

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