Convert mp4 to mp3 format

This is the Python script which converts a folder of MP4 (video) files into a second folder of mp3 audio files.

You must first install ffmpeg on your computer to be able to run this code.

First, replace video_dir with the path to your videos

And then replace audio_dir with the path where you would like to save your audio mp3 files.

Once the script finishes running, you will see a list of mp3 audio files in the audio_dir folder. The script runs pretty quickly and will finish the conversion in a few seconds or within a minute.

from moviepy.editor import *
import os

video_dir = 'video/gpt_for_online_courses'
audio_dir = 'audio/gpt_for_online_courses'
for filename in os.listdir(video_dir):
    f = os.path.join(video_dir, filename)
    if os.path.isfile(f):
        video = VideoFileClip(f)
        print(f)
        mp3_filename = filename.replace('.mp4', '.mp3')
        audio = os.path.join(audio_dir, mp3_filename)
        video.audio.write_audiofile(audio, codec='libmp3lame')


About this website

BotFlo1 was created by Aravind Mohanoor as a website which provided training and tools for non-programmers who were2 building Dialogflow chatbots.

This website has now expanded into other topics in Natural Language Processing, including the recent Large Language Models (GPT etc.) with a special focus on helping non-programmers identify and use the right tool for their specific NLP task. 

For example, when not to use GPT

1 BotFlo was previously called MiningBusinessData. That is why you see that name in many videos

2 And still are building Dialogflow chatbots. Dialogflow ES first evolved into Dialogflow CX, and Dialogflow CX itself evolved to add Generative AI features in mid-2023