Running FFmpeg commands from a Python Script

FFmpeg is one of my favorite tools for encoding video files, but even as someone who has used it for a few years the syntax can still be pretty tricky. To help with this I decided to create a Python script to help automate the common task of taking a video file and encoding it to an MP4 encoded with x264.

I’ll go through the process of how I created the script and break down each section. The first thing I do is import the subprocess module which allows me to run shell commands from Python. More info about subprocess can be found here. Then I setup a variable to define where the ffmpeg binary is located.

import subprocessffmpeg = "/usr/local/bin/ffmpeg"

I created a function grabUserInput() which prompts a user for input, stores that input in a dictionary, and returns that dictionary to be used in another function. Within that function I defined a nested function called filterInput() which is responsible for formatting the input message the user will see and setting a default value so the user can simply hit enter if they don’t wish to change the default value.

def grabUserInput():    def filterInput(message, default):
user_input = input (message)
if user_input == "":
user_input = default
return user_input
print("Hit enter for default values\n") user_input_dict = {} user_input_dict["input_file"] = filterInput("Input File: ", "")
return user_input_dict

The buildFFmpegCommand() function is responsible to take the returned user input from grabUserInput() and formatting it properly for the subprocess module. To run a shell command with subprocess you must provide it with a list of strings. Ideally each flag and command should be separated into their own list element.

def buildFFmpegCommand():    final_user_input = grabUserInput()    commands_list = [
return commands_list

Finally I created a function called runFFmpeg that takes in a list of commands and runs those commands with subprocess. I added in an extra message that gets printed when FFmpeg runs successfully, or when it errors out. This is a great feature of subprocess in that it lets you check the exit status of your shell command. Then I simply call the runFFmpeg() function to run the script.

def runFFmpeg(commands):    if == 0:
print ("FFmpeg Script Ran Successfully")
print ("There was an error running your FFmpeg script")

Here is the full script for reference. This gives you an idea of how you can add or remove FFmpeg commands as necessary. To expand this functionality further you could do something like list other options available instead of just the default option, since right now the script requires you to know the names of the FFmpeg options.

Software Developer and Video/Post Production Professional. Recent graduate of Flatiron School.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store