Write a video using open-cv
Use open-cv VideoWriter function to write a video
Source Code
import cv2
video = cv2.VideoCapture("./data/video.mp4")
fourcc = cv2.VideoWriter.fourcc(*'FMP4')
writeVideo = cv2.VideoWriter('./data/writeVideo.mp4',fourcc,24,(1080,720))
while(video.isOpened()):
suc, frame = video.read()
if(suc):
frame = cv2.resize(frame,(1080,720))
cv2.imshow("write video",frame)
writeVideo.write(frame)
if(cv2.waitKey(24)&0xFF == ord('q')):
break
else:
break
writeVideo.release()
video.release()
cv2.destroyAllWindows()
Video

Pre-Required Knowledge
If you know OpenCV, you can use it to open a video. If you donβt know this, visit this open video blog.
Functions
Explain Code
Import open-cv Library import cv2
Open a Video Using videoCapture
Function
fourcc
The fourcc
function is used to specify a video codec.
Example: AVI format codec for XVID
.
VideoWriter
The videoWriter
function initializes the writeVideo
object. it specify video properties such as codec, FPS, and resolution.
There are four arguments:
- Video Path: Specifies the video write path and video name.
- fourcc: Specifies the video codec.
- FPS: Sets an FPS value.
- Resolution: Sets the video resolution.
The read()
function is used to read a frame.
After reading a frame, resize() it.
Note: If you set a resolution in writeVideo
, you must resize the frame
to the same resolution.
write
This function writes a video frame by frame into the writeVideo
object.
The waitKey
function is used to delay the program and check key events for program exit using an if condition.
Release objects
Once the writing process is complete, release the writeVideo
and video
objects to finalize the video writing process.