用于计算和比较 MP3 中的 LAME MusicCRC 的 Mac 或 Unix 兼容实用程序?

用于计算和比较 MP3 中的 LAME MusicCRC 的 Mac 或 Unix 兼容实用程序?

LAME 编码器将音频流的 CRC16 校验和存储在其编码的每个 MP3 的标头中。然后可以计算“实际”音频校验和,并在以后将其与原始值进行比较,以验证音频是否已损坏(无需担心 ID3 标签等会更改计算值)。

在 Windows 上,有一个名为 LameTag 的命令行实用程序,它能够计算校验和并将其与原始校验和进行比较。不幸的是,它已被废弃,并且可能不易移植到 OS X,而我使用的当然是 OS X。我认为 EncSpot 能够做同样的事情,但它同样仅适用于 Windows。

我的问题是:是否有任何类似的实用程序与 Mac、Linux、BSD 或类似系统兼容?

我发现有一些可以显示原来的CRC(如eyeD3),但它们无法计算当前值。还有几个实用程序声称可以检查 MP3 中的损坏情况,但我没有发现任何实际使用 MusicCRC 框架的实用程序 — 它们中的大多数似乎使用更通用的检查方法,或者它们使用框架 CRC(默认情况下在 LAME 中禁用,不能依赖)。

编辑:
我想我已经回答了自己的问题。在研究这个问题的过程中,我偶然发现了Python 脚本为了诱变剂,QuodLibet 的音频元数据库。该脚本旨在读取 LAME 的信息标签,虽然它没有专门处理 CRC 字段,但我能够根据其示例创建一些可以处理的内容。经过几个小时的折腾(我是一个糟糕的程序员,对 Python 一无所知),我终于设法编写了一些东西,虽然没有特色而且很慢,但确实返回了原始 CRC计算新的:

# Known good track
kapche-lanka:test % ../mp3crc.py "10 - CLAW FINGER.mp3"
10 - CLAW FINGER.mp3:
    Original MusicCRC:     8171
    Computed MusicCRC:     8171
    Original Info Tag CRC: AEFD
    Computed Info Tag CRC: AEFD

# Known bad track
kapche-lanka:test % ../mp3crc.py "10 - Griffons Never Die.mp3"
10 - Griffons Never Die.mp3:
    Original MusicCRC:     2014
    Computed MusicCRC:     BCF1
    Original Info Tag CRC: DF02
    Computed Info Tag CRC: DF02

每当我以更严肃的方式使其工作时,我将再次更新此帖子以添加指向脚本的链接。

谢谢!

编辑2:
我在下面添加了一个指向我的脚本的链接(请参阅已接受的答案)。它被称为mp3crc,虽然设计得不是专业,但似乎在大多数情况下都有效:

https://github.com/ohkine/mp3crc

答案1

下面是一个 Bash shell 函数,lameCRC()用于计算 Xing/Info-LAME 头帧的 LAME musicCRC 和 CRC-16(由Mp3 信息标签修订版 1 规范 - 草稿 0)通过使用 Apple 的afinfo命令和crcHampa Hug 的命令行工具,http://www.hampa.ch/misc-utils/index.html

如果 Apple 的afinfo命令不可用,dd则会使用它(但这会导致速度减慢)。

(注意:我故意避免使用 Bash 的内部字符串函数,以使可移植性更容易)。

lameCRC() {     # Bash shell function

   # lameCRC() uses the crc command line tool from http://www.hampa.ch/misc-utils/index.html.
   # lameCRC() is partly inspired by the output of Apple's afinfo command and 
   # the C source code of Audio-Scan-0.93 and MP3-Cut-Gapless-0.03 by Andy Grundman:
   # https://metacpan.org/author/AGRUNDMA
   # Audio-Scan-0.93/src/mp3.c          (GNU General Public License Version 2, June 1991 or later)
   # Audio-Scan-0.93/include/mp3.h      ( ditto )
   # MP3-Cut-Gapless-0.03/src/mp3cut.c  ( ditto )

   # usage: lameCRC lame.mp3

   # Basic information: 
   # Mp3 Info Tag rev 1 specifications, http://gabriel.mp3-tech.org/mp3infotag.html
   # LAME info header zone A has a length of 120 bytes (or 240 chars in xxd hex output).
   # The "LAMEx." string is followed by 30 bytes (or 60 chars in xxd hex output) according to the 
   # "Suggested Info Tag extension fields + layout" in the Mp3 Info Tag rev 1 specifications.

   local n n1 n2 lines plus crcs hexchar lame_start_idx xinginfo_start_idx PATH

   PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

   [[ ! -x '/usr/local/bin/crc' ]] && 
      { printf '%s\n' 'No crc command line tool in /usr/local/bin!' 'See: http://www.hampa.ch/misc-utils/index.html' 1>&2; }

   # get Xing|Info|LAME strings and their offsets in binary file
   lines="$(strings -a -n 4 -t d "$1" | grep -E --line-buffered 'Xing|Info|LAME.\.' | head -n 2)" 

   [[ $(echo "$lines" | grep -E -c 'Xing|Info') -ne 1 ]] ||
   [[ $(echo "$lines" | grep -E -c 'LAME[^ ]{5}') -ne 1 ]] && {
      echo 'No Xing|Info string or correct LAME encoder version string (e.g. LAME3.98r) found!' 1>&2; 
      echo "$lines" 1>&2;
      return 1; 
   }

   # get offset index numbers of Xing|Info|LAME strings
   lame_start_idx="$(printf '%s' "$lines" | awk '/LAME/{print $1}' )"
   xinginfo_start_idx="$(printf '%s' "$lines" | awk '/Xing|Info/{print $1}' )"

   # get possible offset of LAME string in output of strings command
   # LAME version string should consist of 9 chars, but may have a prefix in output of strings command
   # example:  9LAME3.98r         instead of   LAME3.98r
   # example:  7LAME3.88 (beta)   instead of   LAME3.88 (beta)
   #plus="$(printf '%s' "$lines" | sed -n 's/^[^ ]*[ ][ ]*\([^ ]*\)LAME[^ ]\{5\}.*/\1/p' | tr -d '\n' | wc -c)"   # use [^ ]\{5\} ?
   plus="$(printf '%s' "$lines" | sed -n 's/^[^ ]*[ ][ ]*\([^ ]*\)LAME.*/\1/p' | tr -d '\n' | wc -c)"

   lame_start_idx=$(( $lame_start_idx + $plus ))

   [[ $(( $lame_start_idx - $xinginfo_start_idx )) -ne 120 ]] && 
      { echo 'No 120 bytes between Xing / Info and LAME string. Exiting ...' 1>&2; return 1; }

   # get entire LAME info tag
   #dd if="$1" bs=1 skip="$lame_start_idx" count=36 2>/dev/null |  LC_ALL=C od -A n -cv; return 0

   # get bytes $BC-$BD (MusicCRC) and bytes $BE-$BF (CRC-16 of Info Tag) (as described in http://gabriel.mp3-tech.org/mp3infotag.html)
   crcs="$(dd if="$1" bs=1 skip="$(( $lame_start_idx + 32 ))" count=4 2>/dev/null | xxd -p | tr -d '\n')"

   [[ -z "$crcs" ]] && { echo 'No LAME musicCRC and CRC-16 of Info Tag found!' 1>&2; return 1; }

   lameMusicLengthPlusCRCs="$(dd if="$1" bs=1 skip=$(( $lame_start_idx + 28 )) count=8 2>/dev/null | xxd -p | tr -d '\n')"
   lameMusicLength="$(echo "$lameMusicLengthPlusCRCs" | cut -b 1-8 )"
   lameMusicCRC1="$(echo "$lameMusicLengthPlusCRCs" | cut -b 9-10 )"   # cf. http://gabriel.mp3-tech.org/mp3infotag#musiccrc
   lameMusicCRC2="$(echo "$lameMusicLengthPlusCRCs" | cut -b 11-12 )"
   lameInfoTagCRC16="$(echo "$lameMusicLengthPlusCRCs" | cut -b 13-16 )"

   # LAME MusicLength consists of: 
   # [LAME Tag frame][complete mp3 music data]
   lameMusicLengthByteSize=$(printf '%d' "0x${lameMusicLength}")

   [[ $lameMusicLengthByteSize -le 0 ]] && { echo 'lameMusicLengthByteSize <= 0. Exiting ...' 1>&2; return 1; }


   if [[ -x '/usr/bin/sw_vers' ]] && [[ "$(/usr/bin/sw_vers -productName)" == "Mac OS X" ]] && [[ -x '/usr/bin/afinfo' ]]; then

      # get audio_bytes, i. e. [complete mp3 music data] - [LAME Tag frame]

      #id3v2 --delete-all "$1" 1>/dev/null  # for testing purposes; edits file in-place!
      # afinfo seems to be only available on Mac OS X 
      # afinfo alternative: Perl module Audio-Scan-0.93 by Andy Grundman 
      # perl -e 'use Audio::Scan; my $offset = Audio::Scan->find_frame($ARGV[1],0); print "$offset\n";' _ file.mp3
      audioinfo="$(afinfo "$1")"  
      audio_bytes="$(echo "$audioinfo" | awk -F" " '/audio bytes:/{print $NF}' )"
      audio_data_file_offset="$(echo "$audioinfo" | awk -F" " '/audio data file offset:/{print $NF}')"
      xingInfoLameTagFrameSize=$(( lameMusicLengthByteSize - audio_bytes ))

      [[ $audio_bytes -le 0 ]] && { echo 'audio_bytes <= 0. Exiting ...' 1>&2; return 1; }

      # 0..xingInfoLameTagFrameOffset (match first 0xff byte in mp3 file)
      n=0
      hexchar=""
      until [[ "$hexchar" == 'ff' ]]; do
         hexchar="$(dd if="$1" bs=1 skip=$n count=1 2>/dev/null | xxd -p)"
         n=$(( n + 1))
      done
      xingInfoLameTagFrameOffset=$(( n - 1 ))

   else   # dd speed bump

      # get xingInfoLameTagFrameSize
      # for mp3 magic numbers (\xFF\xFB) see: 
      # http://www.digitalpreservation.gov/formats/fdd/fdd000105.shtml

      # n1
      # count bytes from: 0xff...<--...$xinginfo_start_idx
      hexchar=""
      n=$xinginfo_start_idx
      until [[ "$hexchar" == 'ff' ]]; do
         n=$(( n - 1))
         hexchar="$(dd if="$1" bs=1 skip=$n count=1 2>/dev/null | xxd -p)"
      done
      xingInfoLameTagFrameOffset=$n
      n1=$(( xinginfo_start_idx - n ))

      # n2
      # count bytes from: $xinginfo_start_idx+120+36...-->...0xff
      hexchar=""
      n=$((xinginfo_start_idx + 120 + 36))
      until [[ "$hexchar" == 'ff' ]]; do
         hexchar="$(dd if="$1" bs=1 skip=$n count=1 2>/dev/null | xxd -p)"
         n=$(( n + 1))
      done
      n2=$(( n - xinginfo_start_idx - 120 - 36 - 1 ))   # - 1 because the trailing 0xff got counted by $n

      xingInfoLameTagFrameSize=$(( $n1 + $n2 + 120 + 36 ))
      audio_data_file_offset=$((xingInfoLameTagFrameOffset + xingInfoLameTagFrameSize))

      # get audio_bytes, i. e. [complete mp3 music data] - [LAME Tag frame]
      audio_bytes=$( printf "%s\n" "scale = 0; ${lameMusicLengthByteSize} - ${xingInfoLameTagFrameSize}" | bc )

   fi

   old_lameInfoTagCRC16="$lameInfoTagCRC16"
   new_lameInfoTagCRC16="$(head -c $(( xingInfoLameTagFrameOffset + xingInfoLameTagFrameSize )) "$1" | 
          tail -c ${xingInfoLameTagFrameSize} | head -c 190 | crc -R -r -g crc16)"

   old_lameMusicCRC16="${lameMusicCRC1}${lameMusicCRC2}"
   new_lameMusicCRC16="$(head -c $(( ${audio_data_file_offset} + ${audio_bytes} )) "$1" | 
          tail -c ${audio_bytes} | crc -R -r -g crc16)"

   echo
   printf '%s\n' "old_lameInfoTagCRC16: ${old_lameInfoTagCRC16}" "new_lameInfoTagCRC16: ${new_lameInfoTagCRC16}"
   echo
   printf '%s\n' "old_lameMusicCRC16: ${old_lameMusicCRC16}" "new_lameMusicCRC16: ${new_lameMusicCRC16}"
   echo

   return 0
}

答案2

似乎有一个 C 端口LameTag_Source_0.4.1/CRC16.pas称为mp3_check-1.98/crctest.c(这是一个命令行工具)。

这是一个破解版本mp3_check-1.98/crctest.c,可以计算给定 mp3 文件的 CRC16 校验和。

/*

modified version of source code taken from:
mp3_check-1.98/crctest.c,
http://sourceforge.net/projects/mp3check/

NOTE:
compare mp3_check-1.98/crctest.c with
LameTag_Source_0.4.1/CRC16.pas from
http://phwip.wordpress.com/home/audio/ 

See also: 
mp3check - check mp3 files for integrity,
http://jo.ath.cx/soft/mp3check/

gcc -Wall -Wextra -03 -o crctest crctest.c

./crctest *.mp3
printf '%d\n' $(./crctest *.mp3)

*/

#include <stdio.h>
#include <stdlib.h>

int
crcbuf(crc, len, buf)
    register int    crc;    /* running CRC value */
    register unsigned long  len;
    register char *buf;
{

    static short crc_table[] =  {
            0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
            0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
            0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
            0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
            0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
            0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
            0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
            0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
            0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
            0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
            0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
            0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
            0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
            0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
            0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
            0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
            0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
            0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
            0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
            0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
            0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
            0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
            0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
            0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
            0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
            0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
            0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
            0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
            0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
            0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
            0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
            0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
    };

    register unsigned long  i;

    for (i=0; i<len; i++)
            crc = ((crc >> 8) & 0xff) ^ crc_table[(crc ^ *buf++) & 0xff];

    return (crc);
}

int main (int argc, char * argv []) 

{

    if (argc != 2) return(1);

    int crc = 0;
    int newcrc = 0;

    // cf. http://www.linuxquestions.org/questions/programming-9/c-howto-read-binary-file-into-buffer-172985/
    char *name = argv[1];
    FILE *file;
    char *buffer;
    unsigned long fileLen;

    //Open file
    file = fopen(name, "rb");
    if (!file)
    {
            fprintf(stderr, "Unable to open file %s", name);
            return(1);
    }

    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);

    //Allocate memory
    buffer=(char *)malloc(fileLen+1);
    if (!buffer)
    {
            fprintf(stderr, "Memory error!");
                            fclose(file);
            return(1);
    }

    //Read file contents into buffer
    fread(buffer, fileLen, 1, file);
    fclose(file);

    newcrc = crcbuf(crc, fileLen, buffer);

    printf("0x%x\n", newcrc);

    free(buffer);

    return(0);

}

答案3

我将在这里回答我自己的问题:

在尝试研究这个问题时,我偶然发现了Python 脚本为了mutagen,QuodLibet 的音频元数据库。该脚本旨在读取 LAME 的信息标签,尽管它不专门处理 CRC 字段,但我能够根据其示例创建一些可以处理的内容。经过几个小时的折腾(我是一个糟糕的程序员,对 Python 一无所知),我终于设法编写了一些东西,虽然没有特色而且很慢,但确实返回原始 CRC 并计算新的 CRC。它仍然有点问题,但在我自己的库中,它的准确率至少达到 90%,所以我猜我会“发布”它。它的名字是mp3crcGitHub,可在以下位置找到:

https://github.com/ohkine/mp3crc

该脚本应在 UNIX 和 Windows 上运行,尽管目前有一个仅适用于 Windows 的 Unicode 问题需要修复。它还需要安装crcmod和(我将它们包含在存储库中,但您可以自行安装它们)。mutagen

如上所述,我不是一个很好的程序员,所以我提前为代码可能有多尴尬道歉。但它基本上有效 :)

相关内容