如何从python中的拆分块中删除静默块?

时间:2019-04-12 02:03:23

标签: python audio split

我有一些语音音频文件,我想将其拆分为30秒的块,这是代码

# Split Audios to 30 sec
from pydub import AudioSegment
from pydub.utils import make_chunks

myaudio = AudioSegment.from_file('wav.audios/file_Name.wav' , "wav")
chunk_length_ms = 30000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec

#Export all of the individual chunks as wav files

for i, chunk in enumerate(chunks):
    chunk_name = 'parts/chunk{0}.wav'.format(i)
    print("exporting", chunk_name)
    chunk.export(chunk_name, format="wav")

音频中的一个块始终是一个静默块(最后一个),我希望python能够检测到并删除它,因此我最终得到了非静默的文件块。我该怎么办?

1 个答案:

答案 0 :(得分:2)

使用-del chunks[-1]删除最后一个块,如下所示。

from pydub import AudioSegment
from pydub.utils import make_chunks

myaudio = AudioSegment.from_file('wav.audios/file_Name.wav' , "wav")
chunk_length_ms = 30000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms)
del chunks[-1]

for i, chunk in enumerate(chunks):
    chunk_name = 'parts/chunk{0}.wav'.format(i)
    print("exporting", chunk_name)
    chunk.export(chunk_name, format="wav")