关于数据:我们有两个视频文件是相同的,这些文件的音频也是一样的,但它们的质量不同。即分别为128 that和320 that。
我们使用ffmpeg从视频中提取音频,并使用以下代码为两个音频文件生成散列值:ffmpeg -loglevel error -i 320kbps.wav -map 0 -f -输出为:,类似地,我们为另一个必须进行比较的音频文件,C:\FFMPEG>ffmpeg -loglevel error -i 128kbps.wav -map 0 -f - SHA256=f8ca7622da40473d375765e1d4337bdf035441bbd01187b69e4d059514b2d69a进行了比较。
现在我们知道这些音频文件和散列值是不同的,但是我们想知道它们实际上有多少不同/相似之处,例如:就像a-b中的某个距离是3。
有人能帮忙吗?
发布于 2021-07-11 08:00:49
像SHA-256这样的密码散列不能用来比较两个音频文件之间的距离。加密散列被故意设计为不可预测的,并且理想情况下不显示关于被散列的输入的信息。
然而,有许多合适的声指纹算法可以接受一段音频并返回指纹矢量。然后,你可以通过观察两个音频片段的指纹向量有多接近来度量它们之间的相似性。
选择一种声学指纹算法
色素是一种流行的基于绑定和重新实现的开源语音指纹算法。Chromaprint由AcoustID项目使用,该项目正在构建一个开源数据库,为流行音乐收集指纹和元数据。
研究人员Joren还编写并开源了声学指纹库帕纳科和奥拉夫。然而,它们目前都被授权为AGPLv3,并可能侵犯仍在使用的美国专利.
有几家公司--比如佩克斯--出售API来检查任意音频文件是否包含版权材料。如果你注册了Pex,他们会给你他们的近源SDK,根据他们的算法生成声音指纹。
指纹生成与比较
在这里,我假设您选择了Chromaprint。您必须安装libchromaprint和FFT库。
我假设您选择了Chromaprint,并且希望使用Python比较指纹,尽管一般原则适用于其他指纹库。
xor
函数和计算1
位数来比较两个音频文件。下面是一些快速和肮脏的Python代码,用于比较两个指纹之间的距离。虽然如果我正在构建一个生产服务,我会在C++或Rust中实现比较。
from operator import xor
from typing import List
# These imports should be in your Python module path
# after installing the `pyacoustid` package from PyPI.
import acoustid
import chromaprint
def get_fingerprint(filename: str) -> List[int]:
"""
Reads an audio file from the filesystem and returns a
fingerprint.
Args:
filename: The filename of an audio file on the local
filesystem to read.
Returns:
Returns a list of 32-bit integers. Two fingerprints can
be roughly compared by counting the number of
corresponding bits that are different from each other.
"""
_, encoded = acoustid.fingerprint_file(filename)
fingerprint, _ = chromaprint.decode_fingerprint(
encoded
)
return fingerprint
def fingerprint_distance(
f1: List[int],
f2: List[int],
fingerprint_len: int,
) -> float:
"""
Returns a normalized distance between two fingerprints.
Args:
f1: The first fingerprint.
f2: The second fingerprint.
fingerprint_len: Only compare the first `fingerprint_len`
integers in each fingerprint. This is useful
when comparing audio samples of a different length.
Returns:
Returns a number between 0.0 and 1.0 representing
the distance between two fingerprints. This value
represents distance as like a percentage.
"""
max_hamming_weight = 32 * fingerprint_len
hamming_weight = sum(
sum(
c == "1"
for c in bin(xor(f1[i], f2[i]))
)
for i in range(fingerprint_len)
)
return hamming_weight / max_hamming_weight
上面的函数可以让您按以下方式比较两个指纹:
>>> f1 = get_fingerprint("1.mp3")
>>> f2 = get_fingerprint("2.mp3")
>>> f_len = min(len(f1), len(f2))
>>> fingerprint_distance(f1, f2, f_len)
0.35 # for example
您可以更多地了解如何使用Chromaprint计算不同音频文件之间的距离。这个邮件列表线程描述了如何比较Chromaprint指纹的理论。这个GitHub吉斯特提供了另一个实现。
发布于 2020-07-17 12:01:46
不能为此使用SHA256哈希。这是故意的。如果可以的话,它会削弱哈希的安全性。你的建议类似于差分密码分析。SHA256是一种现代的密码散列,它的设计是为了防止这种攻击。
https://stackoverflow.com/questions/62953484
复制相似问题