我有一些语音音频文件,我想分割成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能够检测到并删除它,因此最终我得到的是非静默文件。我怎样才能做到这一点?

最佳答案

只需使用-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")

10-06 03:13