使用静音检测拆分音频文件

使用静音检测拆分音频文件,第1张

使用静音检测拆分音频文件

我发现pydub是用简单的方法和紧凑的代码来进行这种音频处理的最简单工具。

您可以安装pydub与

pip install pydub

如果需要,您可能需要安装ffmpeg / avlib。有关更多详细信息,请参见此链接。

这是您所要求的代码片段。某些参数(例如

silence_threshold
和)
target_dBFS
可能需要进行调整以符合您的要求。总体而言,
mp3
尽管我必须尝试为设置不同的值,但我仍然能够拆分文件
silence_threshold

片段

# import the AudioSegment class for processing audio and the # split_on_silence function for separating out silent chunks.from pydub import AudioSegmentfrom 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 parametersfor 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声道),则您的块也将是立体声。您可以像这样检查原始音频:

>>> song.channels2


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5654436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存