How to Use FFMpeg in Python (with Examples)

Integrating FFmpeg's powerful media manipulation capabilities into your Python code becomes easy with the “ffmpeg-python” library. With a few lines of code, you can convert media formats, edit videos, extract audio, and more within your Python applications.
by Josephine Loo ·

Contents

    Python is one of the most popular programming languages due to its simplicity and versatility. You can use it for various types of projects including web development, data analysis, web scraping, automation, and more. When it comes to editing or manipulating media files like videos, images, and audio files, you will need to use libraries that provide these functionalities as they don’t come with Python.

    In this article, we are going to learn how to use FFmpeg, a popular media manipulation tool in Python to work with media files, showing some practical examples.

    What is FFmpeg

    FFmpeg is a complete, cross-platform solution to record, convert, and stream audio and video. It can decode, encode, transcode, mux, demux, stream, filter, and play media files in any format. It is also highly portable—it compiles and runs in a wide variety of build environments, machine architectures, and configurations like Linux, Mac OS X, Microsoft Windows, etc.

    FFmpeg contains multiple tools for end-users to convert, play, and analyze media files and libraries for developers to use in different applications. Libraries like libavcodec, libavutil, libavformat, libavfilter, libavdevice, libswscale, and libswresample will be downloaded automatically when you download FFmpeg to your machine.

    Although FFmpeg is a command-line tool, you can use it in your Python project with the Python library ffmpeg-python.

    Pre-requisite

    You need to have the tools below installed and the versions used in this tutorial are included as a reference:

    The ffmpeg-python library is only a Python wrapper for the FFmpeg installed on your machine and does not work independently. Therefore, you must have FFmpeg installed before using ffmpeg-python in your Python project.

    Installing/Adding the FFmpeg Libary in Python

    Once you have the tools above installed, open up the terminal/command prompt and run the command below in your working directory to install ffmpeg-python in your Python project using pip:

    pip install ffmpeg-python
    

    Then, create a Python file (eg. index.py), import ffmpeg in the code, and use it to perform various operations on media files.

    import ffmpeg
    
    // use ffmpeg here to perform various operations here
    

    Let’s look at some examples…

    Using ffmpeg-python

    Basic Usage (Convert Video Format)

    Using the ffmpeg-python library typically includes specifying the input video file using the ffmpeg.input() function, output file using the output() function, and executing the command using run() function:

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4")
    	.output("output.wmv")
    	.run()
    )
    

    The basic command above will convert the input MP4 video into a WMV video. To do more with FFmpeg, we can apply various filters to the input file after the input() function and also pass different parameters like ss, to, acodec, etc. to the input() and output() functions.

    Convert MP4 to MP3/Extract Audio from Videos

    Converting an MP4 video to an MP3 audio or extracting audio from a video is simple. It works similarly to converting the format of a video as shown in the previous example. Instead of naming the output file in a video file extension, name it in an audio file extension like “.mp3”. It will convert the video to MP3 automatically using the default audio codec (libmp3lame):

    screenshot of the Terminal/Command Prompt output that shows the default codec

    Here’s the code:

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4")
    	.output('audio.mp3')
    	.run()
    )
    

    You can also use a custom audio codec by specifying it in the acodec parameter of the output() function:

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4")
    	.output('audio.mp3', acodec='libshine')
    	.run()
    )
    

    This will use the libshine encoder to encode the output file:

    screenshot of the Terminal/Command Prompt output that shows the custom codec

    🐻 Bear Tips: Check the FFMpeg doc for the complete list of supported codecs.

    Trim Video

    Trimming a video using ffmpeg-python only requires adding some parameters in the input() function. In the input() function, specify the start time in the ss parameter and the end time in the to parameter.

    This will trim the input video from 00:00:10 to 00:00:20 and save it as a new “output.mp4” video:

    import ffmpeg
    
    start_time = '00:00:10' # Start time for trimming (HH:MM:SS)
    end_time = '00:00:20' # End time for trimming (HH:MM:SS)
    
    (
    	ffmpeg.input("input.mp4", ss=start_time, to=end_time)
    	.output("trimmed_output.mp4")
    	.run()
    )
    

    Here’s a screenshot of the input video at the 00:00:10 timestamp:

    screenshot of the input video at the 00:10 timestamp

    And this is the result of the output video:

    screenshot of the trimmed output video

    Extract Frames from Videos

    Another thing you can do with FFmpeg by adding parameters to the functions is to extract frames from the input video. You can specify the number of frames that you want to extract using the parameter vframes:

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4")
    	.output('frame_%d.png', vframes=3)
    	.run()
    )
    

    It will extract the first three frames from the input video at 25fps, which is the default frame rate:

    screenshot of the first three frames extracted

    You can also use the fps filter to change the frame rate of the video. The code below will extract frames from the whole video (since vframes is not specified) at 1fps instead of 25fps:

    import ffmpeg 
    
    (
    	ffmpeg.input("input.mp4")
    	.output('frame%d.png', vf='fps=1')
    	.run()
    )
    

    Here’s a screenshot of the output frames:

    screenshot of all the frames

    For both methods, you use the ss parameter to specify the start time:

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4", ss="00:00:15")
    	.output('frame%d.png', vframes=3)
    	.run()
    )
    

    The code above will extract the first three frames starting from 00:00:15:

    screenshot of the first three frames extracted starting from the specified timestamp

    Create Video Thumbnails

    Creating a thumbnail image from the input video is technically the same as extracting frames from the video, but only a single frame:

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4", ss="00:00:15")
    	.output("thumbnail.png", vframes=1)
    	.run()
    )
    

    Here’s the thumbnail created from the executing the code above:

    the thumbnail output

    Besides using the ss parameter to select a particular frame from the video, you can also use a filter.  The “Thumbnail” filter selects the most representative frame in a given sequence of consecutive frames automatically from the video and saves it as the thumbnail image.

    You can use the “Thumbnail” filter by specifying the filter name in the filter() function following input():

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4")
    	.filter('thumbnail')
    	.output("thumbnail_filter.png")
    	.run()
    )
    

    By default, FFmpeg analyzes the frames in a batch of 100 and picks the most representative frame out of them. The process continues until the end of the video and we will get the best frame among all the batches.

    Here’s the thumbnail selected automatically using the “Thumbnail” filter in the default batch size:

    the thumbnail result using the "Thumbnail" filter

    You can also change the batch size by using the parameter n:

    import ffmpeg
    
    (
    	ffmpeg.input("input.mp4")
    	.filter('thumbnail', n=300)
    	.output("thumbnail_filter_2.png")
    	.run()
    )
    

    Using Bannerbear API

    If you’re using FFmpeg in Python to automate the video editing process like adding subtitles, logos, or other elements to them, you should try an alternative tool, which is Bannerbear. It provides a simple REST API that offers easy integration into any existing platform or app to generate designed visual content including images and videos automatically.

    It’s easy to add it to your Python project to automate the video editing process. Firstly, you need a design template in your Bannerbear account like the one in the screenshot below:

    screenshot of the video template in the Bannerbear editor

    🐻 Bear Tips: Duplicate one from the Template Library in seconds!

    Then, make an HTTP request in your Python code to call the Bannerbear API and trigger the video generation process. You will get the URL of the resulting video in the response and here’s a screenshot of the video:

    You can refer to this tutorial and the API Reference to learn how to do it in detail.

    Conclusion

    The ffmpeg-library enables you to use FFmpeg in Python to manipulate various media files for different purposes like building comprehensive multimedia applications, preprocessing media files for machine learning projects, etc. However, learning the FFmpeg commands has a learning curve. If you prefer a tool that is more visual and with a user-friendly UI, use Bannerbear. Signing up for an account is free and you can choose a design from the Template Library to start immediately!

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.

    FFmpeg Video Editing Essentials: Trimming, Merging, Subtitling, and More!

    As an open-source tool, FFmpeg provides a wide array of functions for handling media files. Let's learn how to use FFmpeg to edit videos programmatically, including trimming, merging, adding subtitles, etc.

    How to Automatically Transcribe Audio Like a Pro using AI Tools and APIs

    In this article, we will guide you on how to transcribe audio, and list several tools for doing it automatically, such as Amazon Transcribe, Google Cloud Speech-to-Text, Whisper by OpenAI, Azure AI Speech, and AssemblyAI.

    What To Know About Zapier’s New Chatbots Feature

    Chatbots play a significant role in modern business communication. Thanks to AI and other technological developments, they are easier and cheaper than ever to add into your workflow—even if you can't code. Here's what to know about Zapier's new AI-powered Chatbots product.

    Automate & Scale
    Your Marketing

    Bannerbear helps you auto-generate social media visuals, banners and more with our API and nocode integrations

    How to Use FFMpeg in Python (with Examples)
    How to Use FFMpeg in Python (with Examples)