在音频文件开始处测量静音长度(wav) [英] measure length of silence at beginning of audio file (wav)
问题描述
我遍历了有关测量沉默的所有帖子,很遗憾,我无法找到解决问题的方法.
I went through all post about measuring silence, unfortunately I have not been able to find a solution for my problem.
我大约有3000个音频文件.每个文件为10秒长,是某人大声说一个单词的记录.
I have about 3000 audio files. Each file is 10 second long and is a recording of someone saying a word at loud.
我需要知道他们说这个单词花了多长时间(=在说这个单词之前保持沉默). 我读到可以使用公式audioop.rms(fragment,width)来做到这一点,但找不到有关如何使用它的说明.
I need to know how long it took for them to say the word (= silence before the word is said). I read I can do it with the formula audioop.rms(fragment, width), but I can not find instruction on how to use it.
新错误:
void@control:~/Documents$ python audio.py
Traceback (most recent call last):
File "audio.py", line 36, in <module>
leading_silences = {a: get_silence(a, threshold) for a in audio_files}
File "audio.py", line 36, in <dictcomp>
leading_silences = {a: get_silence(a, threshold) for a in audio_files}
File "audio.py", line 7, in get_silence
song = AudioSegment.from_wav(audio)
File "/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.py", line 471, in from_wav
return cls.from_file(file, 'wav')
File "/usr/local/lib/python2.7/dist-packages/pydub/audio_segment.py", line 387, in from_file
file = _fd_or_path_or_tempfile(file, 'rb', tempfile=False)
File "/usr/local/lib/python2.7/dist-packages/pydub/utils.py", line 59, in _fd_or_path_or_tempfile
fd = open(fd, mode=mode)
IOError: [Errno 2] No such file or directory: 'silbato.wav'
推荐答案
您可以使用可爱的 pydub @jiarro编写的库:
You could use the lovely pydub library by @jiarro:
from pydub import AudioSegment
from os import listdir
from os.path import isfile, join
def get_silence(audio, threshold, interval):
"get length of silence in seconds from a wav file"
# swap out pydub import for other types of audio
song = AudioSegment.from_wav(audio)
# break into chunks
chunks = [song[i:i+interval] for i in range(0, len(song), interval)]
# find number of chunks with dBFS below threshold
silent_blocks = 0
for c in chunks:
if c.dBFS == float('-inf') or c.dBFS < threshold:
silent_blocks += 1
else:
break
# convert blocks into seconds
return round(silent_blocks * (interval/1000), 3)
# get files in a directory
audio_path = 'path/to/directory'
audio_files = [i for i in listdir(audio_path) if isfile(join(audio_path, i))]
threshold = -80 # tweak based on signal-to-noise ratio
interval = 1 # ms, increase to speed up
leading_silences = {a: get_silence(join(audio_path, a),
threshold, interval) for a in audio_files}
# to get tab-separated values:
for name, leading_silence in leading_silences.items():
print(''.join([name, '\t', str(leading_silence)]))
这篇关于在音频文件开始处测量静音长度(wav)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!