Converting numpy array to video
  • 31-May-2023
Lightrun Team
Author Lightrun Team
Converting numpy array to video

Converting numpy array to video

Lightrun Team
Lightrun Team

Explanation of the problem

The problem at hand involves processing a video using OpenCV and saving the processed video. The initial code snippet demonstrates the usage of OpenCV’s VideoCapture and VideoWriter objects to read frames from a video source, apply processing (flipping frames in this case), and save the processed frames as a new video file. However, when attempting to save each frame and provide it as input to the video writer, an error occurs during the output saving process, stating that there is no such file.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

    ret, frame =
    if ret == True:
        frame = cv2.flip(frame, 0)

        # Write the flipped frame

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):

# Release everything if the job is finished

The subsequent code snippet attempts to solve the problem by using ffmpeg-python library. It showcases a script that saves frames as JPEG files and then uses ffmpeg to create a video file from those frames. However, there is a need to ensure that the saved video file has the same size as the source video.

import ffmpeg

    .input('/path/to/jpegs/*.jpg', pattern_type='glob', framerate=25)

The challenge lies in modifying the code snippet that utilizes ffmpeg-python to ensure that the output video is saved with the same size as the source video, which is in Full HD resolution and has a duration of 2 minutes in AVI format, with a data rate of 7468kbps for the source file and 99532kbps for the saved file.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Converting numpy array to video

To save the video with the same size as the source using ffmpeg-python, adjustments need to be made to the code snippet. Specifically, the resolution and data rate parameters should be set to match the properties of the source video. The following modified code block illustrates how to achieve this:

import ffmpeg

input_file = '/path/to/source/video.avi'
output_file = '/path/to/saved/video.mp4'

    .output(output_file, pix_fmt='yuv420p', vcodec='libx264', vf='scale=1920:1080', video_bitrate='7468k')

In the modified code snippet, the input_file variable is set to the path of the source video file in AVI format. The output_file variable represents the desired path for the saved video file in MP4 format. Within the output() method, the pix_fmt parameter is set to 'yuv420p' to ensure compatibility, vcodec is set to 'libx264' for video encoding, vf (video filter) is used to scale the video to Full HD resolution (1920×1080), and video_bitrate is set to '7468k' to match the data rate of the source file. Adjust these parameters as needed to achieve the desired output video characteristics.

Other popular problems with ffmpeg-python

Problem: Incorrect Input File Format

One of the common issues encountered with ffmpeg-python is when providing an incorrect input file format. When attempting to process a video or perform operations on a file that is not supported by ffmpeg, an error occurs, indicating that the input file is not recognized or readable. This can lead to unexpected behavior or failure in executing the desired operations.


To solve this problem, ensure that the input file provided to ffmpeg-python is in a supported format, such as AVI, MP4, MKV, or other commonly used video formats. If the input file is in a different format, consider converting it to a supported format using ffmpeg or a suitable file conversion tool before passing it to ffmpeg-python. Verifying the input file format compatibility with ffmpeg documentation or relevant resources can help ensure smooth execution of the desired operations.

Problem: Incorrect Output File Configuration

Another frequent issue with ffmpeg-python arises when configuring the output file settings incorrectly. This can result in unexpected output file characteristics, such as incorrect resolution, frame rate, or codec selection. In some cases, the output file may not be created at all, or it may be generated with default settings that do not align with the desired requirements.


To address this problem, it is crucial to carefully configure the output file parameters according to the desired output characteristics. This includes specifying the desired resolution, frame rate, video codec, audio codec, and any other relevant settings. Consulting the ffmpeg documentation or relevant resources can provide guidance on the available options and their appropriate usage. Verifying the output file configuration ensures that the resulting video meets the desired specifications.

Problem: Lack of Error Handling and Logging

Another challenge when working with ffmpeg-python is the absence of comprehensive error handling and logging mechanisms. In complex video processing pipelines or batch operations, it can be difficult to identify the root cause of errors or failures that occur during the execution. Without proper error handling and logging, troubleshooting becomes cumbersome and time-consuming.


To mitigate this problem, implement robust error handling and logging strategies within the ffmpeg-python code. This includes capturing and handling exceptions that may occur during the execution, providing meaningful error messages, and logging relevant information, such as input and output file paths, operation parameters, and error details. Implementing a structured logging framework and incorporating appropriate error handling practices helps in identifying and resolving issues efficiently, improving the overall reliability of the ffmpeg-python workflows.

A brief introduction to ffmpeg-python

ffmpeg-python is a powerful and popular Python library that provides a convenient and intuitive interface for interacting with the FFmpeg multimedia framework. It allows developers to utilize the extensive capabilities of FFmpeg, such as video and audio processing, encoding, decoding, filtering, and more, within their Python applications. With ffmpeg-python, complex multimedia workflows can be implemented and automated using Python code, making it a valuable tool for handling video and audio files programmatically.

The library provides a high-level abstraction layer that simplifies the usage of FFmpeg commands and functionalities. Instead of manually constructing command-line arguments and executing external FFmpeg processes, developers can leverage the expressive API of ffmpeg-python. It enables them to perform tasks like video transcoding, frame extraction, video concatenation, audio extraction, and many other operations with ease. By encapsulating the complexities of FFmpeg’s command-line interface and providing a Pythonic approach, ffmpeg-python empowers developers to integrate multimedia processing seamlessly into their Python projects. The flexibility and versatility of ffmpeg-python make it a valuable tool for a wide range of multimedia-related applications, including video editing, media conversion, video analysis, and more.


Most popular use cases for ffmpeg-python

  1. Video Conversion and Transcoding: ffmpeg-python is commonly used for video conversion and transcoding tasks. It allows developers to change the format, codec, resolution, frame rate, and other parameters of video files. The library provides a simplified interface to define the input and output files, along with the desired video settings.
  2. Video Editing and Manipulation: With ffmpeg-python, developers can perform various video editing and manipulation operations. This includes cutting and trimming video segments, merging multiple videos, adding subtitles or watermarks, adjusting audio tracks, and applying video filters or effects. The library provides an intuitive API to define the desired editing operations and their parameters.
  3. Video Analysis and Processing: ffmpeg-python enables developers to perform advanced video analysis and processing tasks. This includes extracting frames from a video, calculating video statistics, detecting motion, performing object recognition, and implementing custom video processing algorithms. By leveraging the powerful features of FFmpeg, developers can access low-level video processing capabilities and manipulate video data at various stages. This flexibility allows for the implementation of complex video analysis pipelines and customized video processing workflows using Python code.

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications. It’s a registration form away.

Get Lightrun

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.