使用静音检测拆分音频文件 [英] Split audio files using silence detection
问题描述
我有200多个MP3文件,我需要使用静音检测来拆分其中的每个文件.我尝试了Audacity和WavePad,但它们没有批处理过程,将它们一一制作很慢.
I've more than 200 MP3 files and I need to split each one of them by using silence detection. I tried Audacity and WavePad but they do not have batch processes and it's very slow to make them one by one.
情况如下:
- 分割曲目,但静音2秒或更长时间
- 然后在这些音轨的开头和结尾添加0.5 s并将其另存为.mp3
- BitRate 192立体声
- 标准化音量,以确保所有文件的音量和质量相同
我尝试了FFmpeg,但没有成功.
I tried FFmpeg but no success.
推荐答案
我发现 pydub 最简单该工具可以通过简单的方式和紧凑的代码来进行这种音频处理.
I found pydub to be easiest tool to do this kind of audio manipulation in simple ways and with compact code.
您可以通过
安装 pydub
pip install pydub
如果需要,您可能需要安装ffmpeg/avlib.有关更多详细信息,请参见此链接.
You may need to install ffmpeg/avlib if needed. See this link for more details.
以下是符合您要求的代码段.某些参数(例如silence_threshold
和target_dBFS
)可能需要进行一些调整以符合您的要求.
总的来说,尽管我不得不为silence_threshold
尝试不同的值,但我能够拆分mp3
文件.
Here is a snippet that does what you asked. Some of the parameters such as silence_threshold
and target_dBFS
may need some tuning to match your requirements.
Overall, I was able to split mp3
files, although I had to try different values for silence_threshold
.
代码段
# Import the AudioSegment class for processing audio and the
# split_on_silence function for separating out silent chunks.
from pydub import AudioSegment
from pydub.silence import split_on_silence
# Define a function to normalize a chunk to a target amplitude.
def match_target_amplitude(aChunk, target_dBFS):
''' Normalize given audio chunk '''
change_in_dBFS = target_dBFS - aChunk.dBFS
return aChunk.apply_gain(change_in_dBFS)
# Load your audio.
song = AudioSegment.from_mp3("your_audio.mp3")
# Split track where the silence is 2 seconds or more and get chunks using
# the imported function.
chunks = split_on_silence (
# Use the loaded audio.
song,
# Specify that a silent chunk must be at least 2 seconds or 2000 ms long.
min_silence_len = 2000,
# Consider a chunk silent if it's quieter than -16 dBFS.
# (You may want to adjust this parameter.)
silence_thresh = -16
)
# Process each chunk with your parameters
for i, chunk in enumerate(chunks):
# Create a silence chunk that's 0.5 seconds (or 500 ms) long for padding.
silence_chunk = AudioSegment.silent(duration=500)
# Add the padding chunk to beginning and end of the entire chunk.
audio_chunk = silence_chunk + chunk + silence_chunk
# Normalize the entire chunk.
normalized_chunk = match_target_amplitude(audio_chunk, -20.0)
# Export the audio chunk with new bitrate.
print("Exporting chunk{0}.mp3.".format(i))
normalized_chunk.export(
".//chunk{0}.mp3".format(i),
bitrate = "192k",
format = "mp3"
)
如果原始音频是立体声(2声道),则块也将是立体声. 您可以像这样检查原始音频:
If your original audio is stereo (2-channel), your chunks will also be stereo. You can check the original audio like this:
>>> song.channels
2
这篇关于使用静音检测拆分音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!