簡體   English   中英

如何計算沒有標簽信息的mp3文件的哈希值?

[英]How to calculate a hash of a mp3 file without tag info?

我們需要計算一個mp3文件的哈希來唯一地識別它。 問題是Traktor軟件修改了文件的標簽,沒有機會改變它。

我們使用id3lib庫,所以我想也許有一些方法來獲得各種版本的標簽的前置和附加大小,並且只讀取它們之間的媒體內容以計算它的散列。 我一直在id3lib文檔中搜索,我發現的唯一的東西是ID3_Tag::GetPrependedBytes()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。

如果這可以提供幫助,我們正在使用Qt和Qt一起開發,所以也許可以有一些東西來幫助解決問題。

另一種解決方案可能是使用音頻有效載荷的散列來識別mp3文件。 你可以使用庫來解析mpeg音頻文件而不是id3lib嗎?

我用下面的代碼解決了這個問題。 也許它會幫助別人。

/** 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()代碼返回的值來獲取數字表示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM