How to script ffmpeg for Python hackers, second part

The Python computer programming language makes a perfect tool for scripting, especially when it comes to wrapping functionalities of already existing software. Being an FFMpeg user for quiet some time now, there are many functionalities part of it, which I truly need to automate through the Python computer programming language. It is very frustrating to memorize all different types of ffmpeg commands, with all the available options.

As we saw in the first part of this series, one can easily spawn child processes in their own operating system with the help of the Python’s subprocess module. Based on this fact, we can easily script functionalities of the FFMpeg multimedia framework with the help of the Python’s subprocess module.

Through the first part we went and practiced some practical examples which help one to convert video files from one format to another, and also copy the video stream from a video file.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

In this second part, we will write a basic Python script with the main purpose of wrapping some of the most common functionalities being found in the ffmpeg tool of the FFMpeg multimedia framework.

Let’s start to get our hands dirty.

Write the skeleton of the script

Although we did write the skeleton of the script for wrapping ffmpeg functionalities in the previous article of this series, we are going to write it again with the main purpose of practicing the code as much as possible.

Before going any further with this article, make sure to create a script called ffmpeg.py. This module will store our code. Once you have managed to create the new module, open it in editing mode as we have a lot of work to do inside it.

The first thing which we need in our Python script is the builtin subprocess module, which we can easily import like shown below.

import subprocess

Then we need to define the methods for wrapping specific functionalities of the ffmpeg tool, part of the FFMpeg multimedia framework.

The first method which we need to define is the one that deals with converting videos from one format to another. The code for defining it is being shown below.

def convert_video(video_input, video_output):
    pass

Each one of the methods which we are going to define in the ffmpeg.py module, will store the specific commands for the operation they’re going to perform. So it is a good idea to store them inside a Python list object.

def convert_video(video_input, video_output):
    cmds = [] 

The next method which I am going to define is the one which deals with extracting the video stream from a video file. The code for it is being shown below.

def extract_video_stream(video_input, video_output):
    cmds = []

whicAnother function which we need to define as part of the simple ffmpeg wrapper is the one which extracts only the audio stream from a video file.

The code for such function is being shown below.

def extract_audio_stream(video_input, audio_output):
    cmds = []

So far, the skeleton of the ffmpeg.py wrapper should look like the code which is being shown below.

import subprocess 

def convert_video(video_input, video_output):
    cmds = []


def extract_video_stream(video_input, video_output):
    cmds = []


def extract_audio_stream(video_input, audio_output):
    cmds = []

The last method which we are going to define deals with cutting videos. The code for it is being defined below.

def cut_video(video_input, start_cut, end_cut, video_output):
    cmds[]

The above function takes as an input four arguments: the video to cut, the point in which the cutting should start, the point in which the cut should be ended and the video to be produced in the output.

Enough methods for our simple wrapper. We need a helper function which will help us to spawn child processes through the Python’s subprocess module.

Define the helper function at the top level of the ffmpeg.py module like shown below.

def spawn_child(cmds=[]):
    subprocess.Popen(cmds)

Now you should have the following script.

import subprocess 

# top level function to spawn a child process
def spawn_child(cmds=[]):
    subprocess.Popen(cmds)

def convert_video(video_input, video_output):
    cmds = []


def extract_video_stream(video_input, video_output):
    cmds = []


def extract_audio_stream(video_input, audio_output):
    cmds = []


def cut_video(video_input, start_cut, end_cut, video_output):
    cmds[]

It is time to code the rest of the script. All we have to do now is to define the specific ffmpeg commands for each one of the methods we have already defined; and also make use of the spawn_child function to spawn the processes.

Let’s do it step by step. For the first method, the Python list object cmds should look like the piece of code shown below.

cmds = ['ffmpeg', '-i', video_input, video_output]

The ffmpeg.py script should look like the one which is shown below.

import subprocess 

# top level function to spawn a child process
def spawn_child(cmds=[]):
    subprocess.Popen(cmds)

def convert_video(video_input, video_output):
    cmds = ['ffmpeg', '-i', video_input, video_output]


def extract_video_stream(video_input, video_output):
    cmds = []


def extract_audio_stream(video_input, audio_output):
    cmds = []


def cut_video(video_input, start_cut, end_cut, video_output):
    cmds[]

Now let’s define the commands for other methods. For the second method, the Python list object cmds should be like the one shown below.

cmds =  ['ffmpeg', '-i', video_input, '-an', '-vcodec', 'copy', video_output]

For now the script should look like the one shown below.

import subprocess 

# top level function to spawn a child process
def spawn_child(cmds=[]):
    subprocess.Popen(cmds)

def convert_video(video_input, video_output):
    cmds = ['ffmpeg', '-i', video_input, video_output]


def extract_video_stream(video_input, video_output):
    cmds =  ['ffmpeg', '-i', video_input, '-an', '-vcodec', 
             'copy', video_output]


def extract_audio_stream(video_input, audio_output):
    cmds = []


def cut_video(video_input, start_cut, end_cut, video_output):
    cmds[]

For the third method to properly work, we need the command shown below.

cmds = ['ffmpeg', '-i', video_input, '-vn', '-acodec', 'copy',
                 audio_output]

And for the last method, we need the command which is being shown below.

cmds = ['ffmoeg', '-i', video_input, '-ss', start_cut, '-to',
                 end_cut, '-c', 'copy', video_output]

In the end, we should have the following script.

import subprocess 

# top level function to spawn a child process
def spawn_child(cmds=[]):
    subprocess.Popen(cmds)

def convert_video(video_input, video_output):
    cmds = ['ffmpeg', '-i', video_input, video_output]


def extract_video_stream(video_input, video_output):
    cmds =  ['ffmpeg', '-i', video_input, '-an', '-vcodec', 
             'copy', video_output]


def extract_audio_stream(video_input, audio_output):
    cmds = ['ffmpeg', '-i', video_input, '-vn', '-acodec', 
            'copy', audio_output]


def cut_video(video_input, start_cut, end_cut, video_output):
    cmds = ['ffmoeg', '-i', video_input, '-ss', start_cut, '- 
             to', end_cut, '-c', 'copy', video_output]

Now that we have managed to write most of the code, it is time to give our script meaning by making use of the spawn_child function inside of every method.

Make sure that in the end, the ffmpeg.py script looks exactly like shown below.

import subprocess 

# top level function to spawn a child process
def spawn_child(cmds=[]):
    subprocess.Popen(cmds)

def convert_video(video_input, video_output):
    cmds = ['ffmpeg', '-i', video_input, video_output]
    spawn_child(cmds)


def extract_video_stream(video_input, video_output):
    cmds =  ['ffmpeg', '-i', video_input, '-an', '-vcodec', 
             'copy', video_output]
    spawn_child(cmds)


def extract_audio_stream(video_input, audio_output):
    cmds = ['ffmpeg', '-i', video_input, '-vn', '-acodec', 
            'copy', audio_output]
    spawn_child(cmds)


def cut_video(video_input, start_cut, end_cut, video_output):
    cmds = ['ffmoeg', '-i', video_input, '-ss', start_cut, '- 
             to', end_cut, '-c', 'copy', video_output]
    spawn_child(cmds)

How to make use of the ffmpeg.py script

Although the script which we have coded is not a complex one, it is advanced enough to automate a few ffmpeg functionalities. With the help of the Python code being shared above, the computer geek can easily convert their videos from one format to another, extract video or audio streams form his or her videos and also cut videos to specific points in time.

Before we can make use of the ffmpeg.py module we need to change the directory and go where the script is located, so we can call it. Once you have made sure you are inside the directory in which the script is located, launch a new Python interactive console in there.

Then start to import the ffmpeg module in your Python interactive shell with the help of the following command.

import ffmpeg

Then use the ffmpeg.convert_video function like shown below.

ffmpeg.convert_video(‘test.mp4’, ‘test.avi’)

The above Python command converts the video test.mp4 to test.avi.

Now, let’s try to extract the video stream from our video with the help of the ffmpeg.extract_video_stream function.

ffmpeg.extract_video_stream(‘test.mp4’, ‘video_stream.mp4’)

To extract the audio stream from your video, the following command should be used.

ffmpeg.extract_audio_stream(‘test.mp4’, ‘audio_stream.aac’)

To cut your video, the ffmpeg.cut_video function should be used. The syntax for its usage is being shown below.

ffmpeg.cut_video(‘test.mp4′, ’00:01:20′, ’00:02:10’, ‘cut_video_test.mp4’)

Final thoughts

The FFMpeg multimedia framework offers many useful utilities to process videos, audios and images, but since one finds it hard to memorize its commands it is always a good idea to automate stuff through a computer programming language. Through this second part of scripting ffmpeg with Python series, we wrote a simple script which helps one to perform some of the most common tasks supported by the ffmpeg tool.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

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

*