Python Extract Audio (WAV) From Video (MP4) with Mono or Stereo – Python Tutorial

By | August 3, 2022

In this tutorial, we will introduce how to extract audio or voice from a video file in python with mono or stereo. We will use python moviepy library to extract.

We also can convert a video to mp3 file to get audio, here is the tutorial:

A Complete Guide to Python Convert MP4 to MP3 with MoviePy – Python Tutorial

Here we will convert a video to wav audio for more detailed information.

Convert a video to wav audio

It is easy to convert a video file to a wav audio file. Here is an example:

from moviepy.editor import *

mp4_file = r'Androm_Spin.mp4'
wav_file = r'Androm_Spin-2.wav'

videoclip = VideoFileClip(mp4_file)
audioclip = videoclip.audio
audioclip.write_audiofile(wav_file)

audioclip.close()
videoclip.close()

Run this code, Androm_Spin-2.wav audio file will be created.

However, we also can find Androm_Spin-2.wav is 44100 sample rate and 2 channels (stereo)

In order to see the sample rate or channel of an audio file, you can view this tutorial:

View Audio Sample Rate, Data Format PCM or ALAW Using ffprobe – Python Tutorial

Extract audio from a video with 8000 sample rate

We can set a sample rate for target audio. For example:

audioclip.write_audiofile(wav_file, fps=8000)

Here fps = 8000, which means the audio file created is 8000 sample rate.

Extract audio from a video with mono

We can set audio channel before extracting. Here is the example code:

audioclip.nchannels = 1
audioclip.write_audiofile(wav_file, fps=8000)
print(audioclip.nchannels)
audioclip.close()
videoclip.close()

Then, we will get a mono audio with 8000 sample rate.

Leave a Reply

Your email address will not be published. Required fields are marked *