Skip to content

Latest commit

 

History

History
84 lines (56 loc) · 2.41 KB

Python_Convert_audiofile_from_wav_to_mp3.md

File metadata and controls

84 lines (56 loc) · 2.41 KB



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.

Input

Download ffmpeg

!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

Import libraries

try:
    from pydub import AudioSegment
except:
    !pip install pydub
    from pydub import AudioSegment
import requests

Setup Variables

  • 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'

Model

Get and save audio file from URL

response = requests.get(audio_file_url)
open("audio.wav", "wb").write(response.content)

Convert wav to mp3

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")

Output

Display result

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')