0

I am trying to convert mp3 to wav foramt in python. I have searched and found this code

from os import path
from pydub import AudioSegment

# files                                                                         
src = "transcript.mp3"
dst = "test.wav"

# convert wav to mp3                                                            
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="wav")

I have installed pydub package using this line in the anaconda3 pip install pydub. I also installed ffmpeg for windows and I made sure it works by typing ffmpeg -version in the command prompt. Although I got the following error

enter image description here

YasserKhalil
  • 9,138
  • 7
  • 36
  • 95
  • 2
    Try adding the path to `ffmpeg\bin\ ` to your environment's PATH. – duckboycool Oct 24 '20 at 00:39
  • I already did this step. Thanks a lot for the reply. – YasserKhalil Oct 24 '20 at 00:41
  • You could also try manually setting `pydub.AudioSegment.converter` to the path to `ffmpeg.exe`. – duckboycool Oct 24 '20 at 00:43
  • Can you explain more as I am a newbie to python? – YasserKhalil Oct 24 '20 at 00:43
  • If you do `pydub.AudioSegment.converter = r'C:\...\ffmpeg\bin\ffmpeg.exe'` before you open a non-wav file, pydub should pick up ffmpeg. – duckboycool Oct 24 '20 at 00:46
  • I tried this line after dst line `AudioSegment.converter = r'C:\ffmpeg\bin\ffmpeg.exe'` .. And this is the real path of ffmpeg.exe but I got the same error. I have restarted the kernel of anaconda by the way before testing. – YasserKhalil Oct 24 '20 at 00:50
  • At some point, I have used this line `pip install ffmpeg` in anaconda. I think this may be confusing. But when trying to use `pip uninstall ffmpeg`, it takes too long time and nothing appears to be removed. Can you guide me how to remove this package? – YasserKhalil Oct 24 '20 at 00:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223539/discussion-between-duckboycool-and-yasserkhalil). – duckboycool Oct 24 '20 at 00:56
  • @duckboycool Can I specify the WAV encoding ..? – YasserKhalil Oct 24 '20 at 01:54
  • I could do that after digging `import subprocess` then `subprocess.call(['ffmpeg', '-i', 'SampleMP3.mp3', '-c', 'pcm_u8', 'ggg.wav'])`. This worked fine for me. – YasserKhalil Oct 24 '20 at 02:37
  • last question: how to leave silence for the output (say to leave 2 seconds at the start of the output audio)? – YasserKhalil Oct 24 '20 at 02:38
  • Also as for the code, we can use such line `sound.export(dst, format="wav", parameters=["-c:a", "pcm_u8"])` – YasserKhalil Oct 24 '20 at 02:55
  • This solved the issue completely https://stackoverflow.com/questions/46757852/adding-silent-frame-to-wav-file-using-python – YasserKhalil Oct 24 '20 at 04:16

0 Answers0