我们需要计算一个 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();
Run Code Online (Sandbox Code Playgroud)
但他们只返回 0。
如果这有帮助,我们正在用 C++ 和 Qt 一起开发,所以也许有一些东西可以帮助解决这个问题。
我已经用下面的代码解决了这个问题。也许它会对某人有所帮助。
/** 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;
}
Run Code Online (Sandbox Code Playgroud)
这是一个使用 Qt 和 ID3Lib 的解决方案。您可以仅使用代码返回的值hash.result()来获取数字表示形式。
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |