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

Python – Writing to video with OpenCV

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
Content has been removed on Author's request.
1 min read
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 | 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 de
2 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: 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
2 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
Article Tags :
Practice Tags :