Template request | Bug report | Generate Data Product
Tags: #python #audio #wavtomp3 #pydub
Author: Mohit Singh
Description: This notebook uses the Pydub library to convert audio file from wav to mp3.
!wget "https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-amd64-static.tar.xz"
!tar xf ffmpeg-git-amd64-static.tar.xz
!mv ffmpeg-git-20230313-amd64-static ffmpeg
import os
new_path = os.path.join(os.getcwd(), "ffmpeg")
%env PATH=$new_path
try:
from pydub import AudioSegment
except:
!pip install pydub
from pydub import AudioSegment
import requests
audio_file_url
: Link of audio file in wav to be converted to mp3
# Inputs
## link of sample wav file
audio_file_url = 'https://file-examples.com/storage/fef1706276640fa2f99a5a4/2017/11/file_example_WAV_1MG.wav'
wav_path = 'audio.wav'
##Output
mp3_path = 'audio.mp3'
response = requests.get(audio_file_url)
open("audio.wav", "wb").write(response.content)
This code will convert the audio file from wav to mp3.
# read the WAV file using pydub
wav_audio = AudioSegment.from_file("audio.wav", format="wav")
# export the audio as MP3 using pydub
wav_audio.export(mp3_path, format="mp3")
This code will check if audio file is successfully converted to mp3.
if (os.path.isfile(mp3_path)):
print('Conversion of audio file from wav to mp3 is successful')