5

我们需要计算一个 mp3 文件的哈希值来唯一标识它。问题是 Traktor 软件修改了文件的标签,没有机会改变它。

我们使用 id3lib 库,所以我想也许有一些方法可以获取各种版本标签的前置和附加大小,并且只读取它们之间的媒体内容来计算它的哈希值。我一直在 id3lib 文档中搜索,我发现的唯一内容是ID3_Tag::GetPrependedBytes()and ID3_Tag::GetAppendedBytes(),就像这样:

const std::size_t prepend = tagOpener.GetPrependedBytes();
const std::size_t append = tagOpener.GetAppendedBytes();
const std::size_t overall = tagOpener.Size();

但他们只返回 0。

如果这有帮助,我们正在用 C++ 和 Qt 一起开发,所以也许有一些东西可以帮助解决这个问题。

4

2 回答 2

1

我已经用下面的代码解决了这个问题。也许它会帮助某人。

/** Return QString hash for the given path */
inline QString GetHash( const QString& filePath )
{
   /// Determine positions of ID3 tags
   ID3_Tag tagOpener( filePath.toLocal8Bit() );
   const std::size_t prepend = tagOpener.GetPrependedBytes();
   const std::size_t append = tagOpener.GetAppendedBytes();

   /// Calculate a hash
   QString hashValueString;
   QFile file( filePath );
   QCryptographicHash hash( QCryptographicHash::Md5 );
   if( file.open(QIODevice::ReadOnly) )
   {
      /// Read only useful media data and skip tags
      const bool seekRes = file.seek( prepend ); // skip prepend tags info
      const qint64 mediaDataSize = file.size() - append - prepend;
      hash.addData( file.read(mediaDataSize) );

      /// Set hash md5 for current file
      hashValueString =  hash.result().toHex().data();
      file.close();
   }

   tagOpener.Clear();
   return hashValueString;
}

这是一个使用 Qt 和 ID3Lib 的解决方案。您只能使用hash.result()代码返回的值来获取数字表示。

于 2012-09-18T15:59:52.957 回答
0

另一种解决方案可能是使用音频有效负载的哈希来识别 mp3 文件。您可以使用库来解析 mpeg 音频文件而不是 id3lib 吗?

于 2012-08-14T10:28:01.333 回答