The Wayback Machine - https://web.archive.org/web/20240912153947/https://www.geeksforgeeks.org/python-writing-to-video-with-opencv/
Open In App

Python – Writing to video with OpenCV

Last Updated : 03 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will discuss how to write to a video using OpenCV in Python.

Approach

  • Import the required libraries into the working space.
  • Read the video on which you have to write.

Syntax:

cap = cv2.VideoCapture("path")
  • Create a output file using cv2.VideoWriter_fourcc() method

Syntax:

output = cv2.VideoWriter(“path”,cv2.VideoWriter_fourcc(*’MPEG’),30,(1080,1920))

  • Then edit the frames of the video by adding shapes to it (for the example given here, the same can be applied to any other technique.).

Syntax:

cv2.rectangle(frame, (100,100), (500,500), (0,255,0), 3)

  • Then write the video.

Syntax:

output.write(frame)

Example: Program to write to video

Video Used: 

Python




import cv2
  
  
def main():
    
    # reading the input
    cap = cv2.VideoCapture("input.mp4")
  
    output = cv2.VideoWriter(
        "output.avi", cv2.VideoWriter_fourcc(*'MPEG'), 30, (1080, 1920))
  
    while(True):
        ret, frame = cap.read()
        if(ret):
              
            # adding rectangle on each frame
            cv2.rectangle(frame, (100, 100), (500, 500), (0, 255, 0), 3)
              
            # writing the new frame in output
            output.write(frame)
            cv2.imshow("output", frame)
            if cv2.waitKey(1) & 0xFF == ord('s'):
                break
  
    cv2.destroyAllWindows()
    output.release()
    cap.release()
  
  
if __name__ == "__main__":
    main()


Output:



Previous Article
Next Article

Similar Reads

Transition from OpenCV 2 to OpenCV 3.x
OpenCV is one of the most popular and most used Computer vision libraries. It contains tools to carry out image and video processing. When OpenCV 3..4.1 is an improved version of OpenCV 2.4 as it introduced new algorithms and features. Although some of the existing modules were rewritten and moved to sub-modules. In this articles, I will focus on t
2 min read
Top Books for Learning OpenCV: Computer Vision with OpenCV Library
OpenCV or Open Source Computer Vision Library, is an open-source computer vision and machine learning software library. It's extensively used for real-time computer vision tasks such as object detection, face recognition, image processing, etc. Whether you're a beginner or an experienced developer looking to deepen your understanding of OpenCV, her
5 min read
Automatic Document Scanner using OpenCV (OpenCV Document Scanner)
An automatic document scanner using OpenCV is a computer vision application that automatically detects and extracts documents from images. This type of scanner can be useful in various scenarios, such as digitizing paper documents, processing scanned documents, or automating document recognition tasks. In this article, we will see how we can build
6 min read
Python | Play a video in reverse mode using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV's application areas include : 1) Facial recognition system 2) motion tracking 3) Artificial neural network 4) Deep neural network 5) video streaming etc. In Python, one can use an OpenCV library named C
3 min read
Converting Color video to grayscale using OpenCV in Python
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will see how to convert a colored video to a gray-scale format. Approach: Import the cv2 module.Read the video file to be converted usi
1 min read
Python | Create video using multiple images using OpenCV
As we know OpenCV is a widely used library for image processing. It provides a wide sense of image processing. Let's see how to create video using multiple images using OpenCV. Install the following libraries: PIL cv2 Also, check the path before running the code otherwise you will be full with errors. How it works ? Using PIL library we are opening
3 min read
Python OpenCv: Write text on video
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. cv2.putText() method inserts a text on the
2 min read
Python - Displaying real time FPS at which webcam/video file is processed using OpenCV
We will be displaying the real-time processing FPS of the video file or webcam depending upon our choice.FPS or frame per second or frame rate can be defined as number of frames displayed per second. A video can be assumed as a collection of images or we can say frames which are displayed at some rate to produce motion. if you wish to identify the
4 min read
Get video duration using Python - OpenCV
OpenCV is one of the most popular cross-platform libraries and it is widely used in Deep Learning, image processing, video capturing, and many more. In this article, we will learn how to get the duration of a given video using python and computer vision. Prerequisites: Opencv moduledatetime moduleInstallation Opencv can be downloaded by running the
1 min read
How to draw Filled rectangle to every frame of video by using Python-OpenCV?
In this article, we will discuss how to draw a filled rectangle on every frame of video through OpenCV in Python. Stepwise Implementation:Import the required libraries into the working space.Read the video on which you have to write. Syntax: cap = cv2.VideoCapture("path") Create an output file using cv2.VideoWriter_fourcc() method. Here you will ha
2 min read
Save frames of live video with timestamps - Python OpenCV
Images are very important data nowadays from which we can obtain a lot of information which will helpful for analysis. Sometimes data to be processed can be in the form of video. To process this kind of data we have to read video extract frames and save them as images. Security camera videos we have seen that there is a timestamp and date at top of
3 min read
Click response on video output using Events in OpenCV – Python
OpenCV is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos. In this article, we will create a program with a click response on video output using events in OpenCV Python library. We will be using “cv2.EVENT_LBUTTONDOWN” in case wh
3 min read
Creating a Slow Motion Video Using OpenCV - Python
In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library. Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second. If this frame rate is decreased then we get a slo
4 min read
Python - Process images of a video using OpenCV
Processing a video means, performing operations on the video frame by frame. Frames are nothing but just the particular instance of the video in a single point of time. We may have multiple frames even in a single second. Frames can be treated as similar to an image.So, whatever operations we can perform on images can be performed on frames as well
4 min read
Python | Play a video using OpenCV
OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to perform operations on Images or videos. OpenCV library can be used to perform multiple operations on videos.  Let’s see how to play a video using the OpenCV Python. To capture a video, we need to create a VideoCapture object. VideoCapture have the d
2 min read
Python OpenCV: Capture Video from Camera
Python provides various libraries for image and video processing. One of them is OpenCV. OpenCV is a vast library that helps in providing various functions for image and video operations. With OpenCV, we can capture a video from the camera. It lets you create a video capture object which is helpful to capture videos through webcam and then you may
4 min read
OpenCV C++ Program to play a video
The following is the explanation to the C++ code to play a video in C++ using the tool OpenCV. Things to know: (1) The code will only compile in Linux environment. (2) To run in windows, please use the file: 'play_video.o' and run it in cmd. However if it does not run(problem in system architecture) then compile it in windows by making suitable and
3 min read
OpenCV | Loading Video
This article aims to learn how to display a video in OpenCV. To do so is a simple task just like displayed as a single image. But here, this display of frames have to be read in a loop as a part of the sequence. Lets's understand the complete process line by line in detail. Code : CvCapture* capture = cvCreateFileCapture("...input\\video_file.
3 min read
Saving a Video using OpenCV
OpenCV is an open-source and most popular computer vision library that contains several computer vision algorithms. You can read, display, write and doing lots of other operations on images and videos using OpenCV. The OpenCV module generally used in popular programming languages like C++, Python, Java. To save a video in OpenCV cv.VideoWriter() me
2 min read
Faster video file FPS with cv2.VideoCapture and OpenCV
Sometimes we need to speed up the FPS(Frame Per Second) of a video file to perform certain operations on the video such as surveillance footage analysis, video editing, and video game recording. In this article, we will learn to increase the FPS of our input video and download the resultant output video with the help of cv2.VideoCapture in Python.
4 min read
Saving key event video clips with OpenCV
OpenCV is an open-source and popular computer vision library that contains several computer vision algorithms. You can read, display, write and do lots of other operations on images and videos using OpenCV. The OpenCV module is generally used in popular programming languages like C++, Python, and Java. To save a video in OpenCV cv.VideoWriter() met
5 min read
OpenCV C++ Program to blur a Video
The following is the explanation to the C++ code to blur a video in C++ using the tool OpenCV. Things to know: (1) The code will only compile in Linux environment. (2) To run in windows, please use the file: 'blur_video.o' and run it in cmd. However if it does not run(problem in system architecture) then compile it in windows by making suitable and
3 min read
Saving Operated Video from a webcam using OpenCV
OpenCV is a vast library that helps in providing various functions for image and video operations. With OpenCV, we can perform operations on the input video. OpenCV also allows us to save that operated video for further usage. For saving images, we use cv2.imwrite() which saves the image to a specified file location. But, for saving a recorded vide
4 min read
Pafy - Getting Video ID of the given Video
In this article we will see how we can get video ID of the given youtube video in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. A Youtube Video ID is an unique ID to identify a video which is uploaded to Youtube. The Youtube video
2 min read
Pafy - Getting Best Video Stream of the given Video
In this article we will see how we can get best video stream of the given youtube video in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. Best video stream is basically a video stream which is available and in highest resolution. T
2 min read
Setting up ROS with Python 3 and Python OpenCV
Setting up a Robot Operating System (ROS) with Python 3 and OpenCV can be a powerful combination for robotics development, enabling you to leverage ROS's robotics middleware with the flexibility and ease of Python programming language along with the computer vision capabilities provided by OpenCV. Here's a step-by-step guide to help you set up ROS
3 min read
Python | Writing to an excel file using openpyxl module
Prerequisite : Reading an excel file using openpyxl Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, user might have to go through thousands of rows and pick out few handful information to make small changes b
3 min read
Writing to an excel sheet using Python
Using xlwt module, one can perform multiple operations on spreadsheet. For example, writing or modifying the data can be done in Python. Also, the user might have to go through various sheets and retrieve data based on some criteria or modify some rows and columns and do a lot of work. Let's see how to create and write to an excel-sheet using Pytho
2 min read
Reading and Writing JSON to a File in Python
The full form of JSON is Javascript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-
3 min read
Writing data from a Python List to CSV row-wise
Comma Separated Values (CSV) files a type of a plain text document in which tabular information is structured using a particular format. A CSV file is a bounded text format which uses a comma to separate values. The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating
2 min read
Article Tags :
Practice Tags :