Python – Writing to video with OpenCV Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue 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: https://media.geeksforgeeks.org/wp-content/uploads/20210926125350/input.mp4 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: https://media.geeksforgeeks.org/wp-content/uploads/20210926153928/output_final.mp4 Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve. Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 03 Jan, 2023 Like Article Save Article Previous Python - Process images of a video using OpenCV Next Python OpenCv: Write text on video Share your thoughts in the comments Add Your Comment Please Login to comment...