DEV Community

Cover image for Unleashing the power of python and FFMPEG: Extracting and stitching video frames with ease
Sebastian Marin for api.video

Posted on • Edited on • Originally published at api.video

Unleashing the power of python and FFMPEG: Extracting and stitching video frames with ease

TL;DR:

Learn how to harness the capabilities of Python and the FFMPEG library to effortlessly extract frames from a video at regular intervals. Explore the step-by-step process, from leveraging the ffmpeg-python module to extracting frames, and discover how to seamlessly stitch them back together into a dynamic video sequence. Unleash your creativity by automating video skimming and programmatically creating video trailers with just a few lines of code.

Unlocking New Possibilities

Have you ever wondered how you can extract frames from a video at regular intervals using Python and FFMPEG? Well, wonder no more! In this article, I will delve into the exciting world of video processing and show you how to effortlessly extract a list of frames from a video file. This powerful technique not only enables you to perform a quick skim of the video but also opens up a world of possibilities, such as creating a captivating trailer for your video programmatically.

By utilising the robust capabilities of Python and the versatile FFMPEG library, you can automate the extraction process and obtain frames evenly spaced throughout the video duration. Whether you're a video enthusiast looking to explore the content of a lengthy recording or a content creator aiming to generate engaging teasers, this technique will undoubtedly come in handy.

Once you've extracted the frames, you might be wondering, "What can I do with them?" Well, the possibilities are endless! You can reassemble the frames back into a condensed video, allowing you to create a visually stunning trailer that captures the essence of the original footage. Imagine the convenience of automating this process instead of manually sifting through hours of video content to find the most compelling moments. With Python and FFMPEG, you have the power to programmatically curate a captivating preview, saving you time and effort.

In this article, we'll guide you through the step-by-step process of extracting frames from a video using Python and FFMPEG. I'll cover the necessary installations and setup, dive into the code implementation, and explore additional customisation options to suit your specific needs. So, whether you're a beginner seeking to expand your Python skills or an experienced developer looking for an efficient video processing solution, this tutorial is tailored just for you.

Extract Images From Your Video

To extract images from your video, we will utilize the powerful ffmpeg-python module. This module provides a convenient Python interface to interact with the FFMPEG library, enabling us to perform various video processing tasks with ease.

To get started, you need to ensure that ffmpeg-python is installed in your Python environment. If it's not installed yet, you can easily install it by running the following command:

pip install ffmpeg-python
Enter fullscreen mode Exit fullscreen mode

Once ffmpeg-python is installed, let's explore the process of extracting images from a video at specific intervals, as illustrated in the code snippet below:

import ffmpeg

YOUR_FILE = 'sample-mov-file.mov'
probe = ffmpeg.probe(YOUR_FILE)
time = float(probe['streams'][0]['duration']) // 2
width = probe['streams'][0]['width']

# Set how many spots you want to extract a video from.
parts = 7

intervals = time // parts
intervals = int(intervals)
interval_list = [(i * intervals, (i + 1) * intervals) for i in range(parts)]
i = 0

for item in interval_list:
    (
        ffmpeg
        .input(YOUR_FILE, ss=item[1])
        .filter('scale', width, -1)
        .output('Image' + str(i) + '.jpg', vframes=1)
        .run()
    )
    i += 1
Enter fullscreen mode Exit fullscreen mode

Let's break down the code snippet step by step:

  1. Firstly, import the ffmpeg module, which provides the necessary video processing capabilities.
  2. In the YOUR_FILE variable, specify the name of the video file from which you want to extract images. Ensure that the video file is located in the same folder as the code sample.
  3. The ffmpeg.probe function is utilized to retrieve essential information about the video. By accessing properties like 'duration' and 'width' from the probe, we can determine the video's duration and width.
  4. Define the parts variable to represent the desired number of intervals from which you want to extract images.
  5. Calculate the intervals value by dividing the video's duration by the number of parts and converting it to an integer.
  6. Create the interval_list using list comprehension, generating tuples that specify the start and end time for each interval.
  7. Iterate over each interval in the interval_list using a loop.
  8. Within the loop, utilize the ffmpeg.input function to specify the input file, with the ss parameter set to the end time of the current interval (item[1]).
  9. Apply the .filter method to scale the output image, ensuring the width remains consistent while automatically adjusting the height to maintain the aspect ratio.
  10. Use the .output method to define the output file name for the extracted frame. In this example, the format 'Image' + str(i) + '.jpg' is employed, where i represents the index of the image.
  11. Finally, execute the extraction process by calling .run(). The loop will iterate to the next interval accordingly.

To assemble the extracted images back into a video sequence, consider the following code snippet:

import ffmpeg

(
    ffmpeg
    .input('Image%d.jpg', framerate=1)
    .output('output.mp4')
    .run()
)

Enter fullscreen mode Exit fullscreen mode

This code snippet compiles the sequence of extracted frames named 'Image%d.jpg', where %d represents the frame number. It then creates a new video file named 'output.mp4' with a frame rate of 1 frame per second, effectively assembling the extracted images into a video sequence.

Now that you have the code snippet at your disposal, you can start extracting frames from your videos programmatically. Experiment with different frame rates and explore the exciting possibilities of using these extracted frames to create engaging visual content, such as video trailers or quick skims of your videos.

Get ready to unlock the potential of video frame extraction with Python and FFMPEG. Discover the magic of transforming videos into dynamic visual experiences that leave a lasting impression on your audience.

Top comments (0)