FFmpeg Command to Extract Audio From Video with Mono or Stereo in Python – Python Tutorial

By | August 3, 2022

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:

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

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.

Implement Python subprocess.Popen(): Execute an External Command and Get Output

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

FFmpeg Command to Extract Audio From Video with Mono or Stereo in Python – Python Tutorial

Leave a Reply

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