栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

我发现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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/652306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号