In this tutorial, we will tell you how to use ffmpeg command to etract audio from a video file with mono or stereo in python. We also can use moviepy library. For example:
However, if you can not install python moviepy library, how to extract audio?
Here we will use python to call ffmpeg command to extract.
In order to call external application in python, we can use subprocess package.
Here is an example code:
import subprocess mp4_file = r'Androm_Spin.mp4' wav_file = r'Androm_Spin-3.wav' def convert(video_file, wav_file, sample_rate = 8000, channel_num = 1): cmd = r"ffmpeg -i "+video_file+" -ar "+str(sample_rate)+" -ac "+str(channel_num)+" -f wav "+wav_file p1=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) convert(mp4_file, wav_file)
Here we should notice: channel_num = 1, wav file will be mono, channel_num = 2 is stereo.
Run this code, we will get a mono wav audio, its sample rate is 8000