I am trying to use pydub to split audio into evenly spaced segments. Right now I can't seem to even get it to read the audio file. I'm using Jupyter notebook through Anaconda. The solutions from the other topic on this did not help (How to fix FileNotFoundError: [WinError 2] The system cannot find the file specified with AudioSegment.from_mp3())
I have:
- added libav to PATH
- specified where the ffmpeg exe file is, and also installed through conda
- tried running it from command prompt
- tried with and without the AudioSegment.converter line, as well as with and without AudioSegment.ffmpeg
- tried with AudioSegment.from_file and .from_mp3
Would appreciate some help.
import pydub
import math
from pydub import AudioSegment
pydub.AudioSegment.converter = r'C:\Users\M\Desktop\Pydub\ffmpeg.exe'
AudioName = 'Abc' #Name of audio (string)
AudioImported = AudioSegment.from_mp3(r'C:\Users\M\Desktop\Pydub\Abc.mp3')
AudioLength = AudioImported.duration_seconds
Interval = input('Clip length (seconds):') #Length of each clip in seconds
NumClips = math.floor(AudioLength / Interval)
t1 = 0
for i in range(1, NumClips):
t2 = t1 + i*1000
Clip = AudioImported[t1:t2]
ClipName = AudioName + 'i' + '.mp3'
Clip.export(ClipName, format='mp3')
t1 = t2
Edit: Issue resolved itself somehow. Last thing I did was install ffmpeg thorugh conda, but I guess it took a while to kick in.
Also apparently this function is right in the documentation that I missed:
from pydub import AudioSegment
sound = AudioSegment.from_file("/path/to/sound.wav", format="wav")
# split sound in 5-second slices and export
for i, chunk in enumerate(sound[::5000]):
with open("sound-%s.mp3" % i, "wb") as f:
chunk.export(f, format="mp3")