本文介绍了Python静音音频文件的特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个音频文件 audio.wav
,并且有一系列时间框架,如下所示:
I have an audio file audio.wav
and I have an array of time frames looking like this one:
X = [(12.31,14.),(15.4,18.9),...]
这些是我希望在拥有的.wav音频文件中完全保持静音的时间范围.我该如何实现?
These are the time frames that I would like to be completely silent in a .wav audio file that I have. How can I achieve this?
推荐答案
根据您的链接,我将其视为
Based on your link I see it as
from pydub import AudioSegment
a = AudioSegment.from_wav("audio.wav")
# X = [(12.31, 14.), (15.4, 18.9), ...]
duration = (14.0 - 12.31) * 1000
s1 = AudioSegment.silent(duration)
duration = (18.9 - 15.4) * 1000
s2 = AudioSegment.silent(duration)
b = a[:12310] + s1 + a[14000:15400] + s2 + a[18900:]
b.export('new_audio.wav', format='wav')
现在的问题是使用 for
循环使其自动化
now problem is to use for
-loop to automate it
我无法对其进行测试,但我将其视为
I can't test it but I see it as
from pydub import AudioSegment
a = AudioSegment.from_wav("audio.wav")
X = [(12.31, 14.), (15.4, 18.9)]
parts = []
# start for audio
begin = 0
for start, end in X:
# keep sound before silence
s = a[begin*1000:start*1000]
parts.append(s)
# create silence
duration = (end - start) * 1000
s = AudioSegment.silent(duration)
parts.append(s)
# value for next loop
begin = end
# keep part after last silence
parts.append(a[begin*1000:])
# join all parts using standard `sum()` but it need `parts[0]` as start value
b = sum(parts[1:], parts[0])
# save it
b.export('new_audio.wav', format='wav')
这篇关于Python静音音频文件的特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!