How to write a wrapper for ffmpeg with the help of Python, first part

The Python computer programming language is very handy when it comes to automating stuff, especially prototyping. Easy to read and write, the Python computer technology offers rapid development. It is a perfect tool for wrapping functionalities of existing software. As I have mentioned in many of the previous articles being shared in here, I am currently working on a project which wraps some of the functionalities present in the FFMpeg multimedia framework.

Through this first part, you are going to learn how to setup the skeleton of a Python wrapper for the FFMpeg multimedia framework. Let’s start.

The tools you need

First let’s define the requirements of our project. Since I have been coding my project in Python 2.7.12 I highly recommend that you download the same version if it is not present in your operating system.

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

Once you have managed to download and install Python 2.7.12 on your machine, I highly recommend that you compile ffmpeg version 3.2 on your own local machine.

Create the initial structure of the project

After you have managed to configure the tools needed for the development of the ffmpeg wrapper, create the following Python package.

ffmpeg
    __init__.py
    base.py

There are two Python modules inside the ffmpeg directory. The first one, called __init__.py, tells Python that the current directory should be treated as a package. The second module which is called base.py, will serve as the base for the wrapper. All the common tools needed by specific parts of the project will be stored in there.

Let’s write some code

Open the module base.py which is stored in ffmpeg and define the following Python classes.

class BasicParser(object):
    pass


class Base(object):
    pass

Let’s explain the role of each one of the Python classes defined in the above module called base.py.

Since the ffmpeg multimedia framework produces a lot of live output in the console, we need a base class which is going to parse the data. That’s why I defined the class BasicParser; it is going to serve as a base parser for each one of the specific parsers.

What about the class Base? This class is going to serve as the main engine of the wrapper.

For now we have only the skeleton of two main classes stored inside the module base.py, part of the package ffmpeg.

Let’s write some more code inside the module base.py. Since the BasicParser is going to parse output which comes directly from ffmpeg, we need to define a method called parse_output inside it.

So the code should look like the one being shared below

class BasicParser(object):
    def parse_output(self):
        pass


class Base(object):
    pass

Another method that should be inside the class BasicParser is the one which gets live output from the utilities that are part of the FFMpeg multimedia framework.

Let’s define it like shown below.

class BasicParser(object):
    def get_live_output(self):
        pass
    
    def parse_output(self):
        pass


class Base(object):
    pass

Now let’s code the constructor method of the class BasicParser. There are two parameters which we need to pass in the constructor: self and the opened child process.

The code for the constructor method part of the class BasicParser should look like the one which is being shared below.

class BasicParser(object):
    def __init__(self, child_p):
        self.child_p = child_p

    def get_live_output(self):
        pass
    
    def parse_output(self):
        pass


class Base(object):
    pass

As you can see from the above piece of Python code, I have declared the child_p as an instance attribute, because the method get_live_output needs it to get the real time output.

Let’s code the method get_live_output. This method should read data from the open child process of type subprocess.Popen object and return each one of the lines ffmpeg produces.

class BasicParser(object):
    def __init__(self, child_p):
        self.child_p = child_p

    def get_live_output(self):
        for line in self.child_p.stderr.read():
            yield line
    
    def parse_output(self):
        pass


class Base(object):
    pass

The method get_live_output is not finished yet. The code inside it does not work; its main purpose is to give the reader an idea on the role of the method.

The method parse_output will make use of the method get_live_output and parse the real time output which comes from it.

So this method should look similar to the one shown below.

class BasicParser(object):
    def __init__(self, child_p):
        self.child_p = child_p

    def get_live_output(self):
        for line in self.child_p.stderr.read():
            yield line
    
    def parse_output(self):
        for line in self.get_live_output():
            parse_line() # something like this


class Base(object):
    pass

Final thoughts

The Python code I have shared so far is not the final one; its main purpose is to give you guys a skeleton of the ffmpeg wrapper.

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 *

*