How to script ffmpeg for python hackers, first part

The FFMpeg multimedia framework has many tools which come with a lot of functionalities and commands hard to remember, especially for the beginner. Been using it for quiet some time now, I find the framework really useful when processing multimedia files on my own machine, but sometimes it get really frustrating with the long commands which have many different options.

Having some basic knowledge in Python, I have decided to wrap some of the most used ffmpeg functionalities, so one can easily make use of them by calling classes and methods through the Python technology.

Since I like to share my journey as a coder with other geeks all over the world, through this article I will teach you guys how to start scripting the FFMpeg multimedia framework with the help of the Python computer technology, with the main purpose of automating stuff and also avoiding the long commands which I find really hard to remember.

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

There are many ways to spawn child processes with the help of the Python computer programming language, but for the purpose of this series, we will make use of the subprocess module. First, let me give you guys a short introduction to this module and its purpose.

Short introduction to the subprocess module

According to the Python’s official documentation, the subprocess module helps one to launch child processes and also interact with them through input/output/error pipes. Other modules such as os.system, os.popen and os.spawn can do the same thing, but the official documentation recommends subprocess as the best tool for the job.

Long story short, the subprocess module is a perfect fit for our case; it can help us to launch FFMpeg commands through the Python computer programming language.

Launch ffmpeg through Python’s subprocess module

It is time to get our hands dirty. Before going any further, make sure to open a fresh Python console on your own operating system so you can practice and run the code by yourself.

Once you have managed to launch a new console in your own operating system, use the command shown below to import the Python’s subprocess module, so you can make use of it.

import subprocess

Then launch a new process like shown below.

subprocess.Popen('ls')

The above command is a Unix based one, it lists files inside a directory. If executed successfully in your own operating system, the list of files will be displayed on your own interactive console.

Now try the command which is shown below.

subprocess.Popen('ffmpeg')

The above piece of Python code, spawns ffmpeg without any option. As you can see from the output which is shown below, we managed to run the ffmpeg tool with the help of the Python computer technology through the subprocess module.

ffmpeg version 3.2 Copyright (c) 2000-2016 the FFmpeg developers
built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
configuration: –prefix=/usr/local/Cellar/ffmpeg/3.2 –enable-shared –enable-pthreads –enable-gpl –enable-version3 –enable-hardcoded-tables –enable-avresample –cc=clang –host-cflags= –host-ldflags= –enable-libmp3lame –enable-libx264 –enable-libxvid –enable-opencl –disable-lzma –enable-vda
libavutil 55. 34.100 / 55. 34.100
libavcodec 57. 64.100 / 57. 64.100
libavformat 57. 56.100 / 57. 56.100
libavdevice 57. 1.100 / 57. 1.100
libavfilter 6. 65.100 / 6. 65.100
libavresample 3. 1. 0 / 3. 1. 0
libswscale 4. 2.100 / 4. 2.100
libswresample 2. 3.100 / 2. 3.100
libpostproc 54. 1.100 / 54. 1.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]… {[outfile options] outfile}…

Use -h to get full help or, even better, run ‘man ffmpeg’

The same as we managed to run the ffmpeg tool with the subprocess.Popen without any option, we can easily make use of options, by making use of the syntax shown below.

cmds = ['ffmpeg', '-i', 'test.mp4', 'test.avi']
subprocess.Popen(cmds)

As you can see from the above piece of Python code, we have declared a list object with the commands and options specific to the ffmpeg tool.

Once the above piece of Python code is being executed in the interactive console, live output will come out on the shell. It will look similar to the one which is being shown below.

Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> flv1 (flv))
Stream #0:1 -> #0:1 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
frame= 104 fps=0.0 q=23.9 size= 468kB time=00:00:04.39 bitrate= 873.3kbits/s speed=8.78x
frame= 218 fps=218 q=31.0 size= 911kB time=00:00:09.14 bitrate= 815.9kbits/s speed=9.14x
frame= 324 fps=216 q=31.0 size= 1372kB time=00:00:13.55 bitrate= 828.7kbits/s speed=9.02x
frame= 430 fps=215 q=31.0 size= 1947kB time=00:00:17.99 bitrate= 886.3kbits/s speed=8.98x
frame= 549 fps=219 q=31.0 size= 2471kB time=00:00:22.93 bitrate= 882.5kbits/s speed=9.16x

From my practical experience with the command shared in the above example, I can say that it works like a charm. I mostly use it to convert videos from one format to another.

So, first we define the commands inside a list object, and later we spawn a child process with the help of the subprocess.Popen module.

For example, to copy the video stream from a video file, we have to run the Python command which is being shown below.

cmds = ['-i', test.mp4, '-an', '-vcodec', 'copy', 'video_stream.mp4']
subprocess.Popen(cmds)

Once the above piece of Python code is being executed, the video stream of test.mp4 will be copied in a new file called video_stream.mp4.

Build a script to wrap some ffmpeg

For the purpose of this article, we are going to wrap some ffmpeg functionalities in a Python script, which we are going to extend in future articles.

First, create a new script with your favorite text editor on your operating system and name it like shown below.

ffmpeg.py

Then on the top of the ffmpeg module, import the subprocess module like shown below.

import subprocess

The build the following skeleton.

def convert_video(video_input, video_output):
    pass

def extract_video(video_input):
    pass

def cut_video(video_input, start_cut, end_cut, video_output):
    pass

Once finished, close the script. The functions which we defined, will serve as the base for the automation of some of the most common ffmpeg tools.

Final thoughts

As you saw in this article, the Python computer programming language is very useful when it comes to scripting and interacting with other software. In the second part, we will finish the script defined above, and get ready for more awesome Python stuff related to wrapping the FFMpeg multimedia framework.

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 *

*