将音频拆分为更小的 5 秒块,但元组索引错误超出范围

问题描述 投票:0回答:0

我想将音频拆分成更小的 5 秒块

import librosa
import librosa.display
from pydub import AudioSegment
import IPython.display as ipd
import soundfile as sf
from pydub.silence import split_on_silence

filename= "/content/drive/MyDrive/Colab Notebooks/22__19_07_13_adventure_maniobra.wav"
y,sr = librosa.load(filename, sr=None)
for i in range(0, len(filename), 5 * sr):
  u = filename[5 * sr * i: 5 * sr *(i+1)]
  sf.write("dest_audio"+str(i)+".wav", u, sr) 

我遵循的代码 https://groups.google.com/g/librosa/c/6UE47GMz0aM 但在

sf.write
函数中出现错误:

IndexError                                Traceback (most recent call last)
<ipython-input-23-ca78240adb01> in <cell line: 9>()
      9 for i in range(0, len(filename),5 * sr):
     10   u = filename[5 * sr * i: 5 * sr *(i+1)]
---> 11   sf.write("dest_audio"+str(i)+".wav", u, sr)

/usr/local/lib/python3.9/dist-packages/soundfile.py in write(file, data, samplerate, subtype, endian, format, closefd)
    340         channels = 1
    341     else:
--> 342         channels = data.shape[1]
    343     with SoundFile(file, 'w', samplerate, channels,
    344                    subtype, endian, format, closefd) as f:

IndexError: tuple index out of range
python arrays audio tuples chunks
© www.soinside.com 2019 - 2024. All rights reserved.