Python,pydub 分割音频文件

Mar*_*k K 5 python pydub

您好,我正在使用 pydub 分割音频文件,给出从原始文件中提取片段的范围。

\n\n

我所拥有的是:

\n\n
from pydub import AudioSegment\n\nsound_file = AudioSegment.from_mp3("C:\\\\audio file.mp3")\n\n# milliseconds in the sound track\nranges = [(30000,40000),(50000,60000),(80000,90000),(100000,110000),(150000,180000)] \n\nfor x, y in ranges:\n    new_file = sound_file[x : y]\n    new_file.export("C:\\\\" + str(x) + "-" + str(y) +".mp3", format="mp3")\n
Run Code Online (Sandbox Code Playgroud)\n\n

它对于前 3 个新文件效果很好。但其余部分则不然 - 它不会相应地分割\xe2\x80\x99。

\n\n

问题出在我给出范围的方式上吗?

\n\n

谢谢。

\n\n

添加在:

\n\n

当它变得简单时 - 例如

\n\n
sound_file[150000:180000]\n
Run Code Online (Sandbox Code Playgroud)\n\n

并将其导出为 mp3 文件。它可以工作,但只能削减 50000:80000 部分。似乎没有读取正确的范围。

\n

小智 0

试试这个,它可能会起作用

import pydub
import numpy as np
sound_file = pydub.AudioSegment.from_mp3("a.mp3")
sound_file_Value = np.array(sound_file.get_array_of_samples())
# milliseconds in the sound track
ranges = [(30000,40000),(50000,60000),(80000,90000),(100000,110000),(150000,180000)] 

for x, y in ranges:
    new_file=sound_file_Value[x : y]
    song = pydub.AudioSegment(new_file.tobytes(), frame_rate=sound_file.frame_rate,sample_width=sound_file.sample_width,channels=1)
    song.export(str(x) + "-" + str(y) +".mp3", format="mp3")
Run Code Online (Sandbox Code Playgroud)