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:
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:
Extract audio from a video with 8000 sample rate
We can set a sample rate for target audio. For example:
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.