Video Processing using python and openCV

The code of following operation on videos will cover in this blog

how to save the video?

import cv2

video = cv2.VideoCapture("path_of_inputPath")

if (video.isOpened() == False):
    print("Error reading video file")

frame_width = int(video.get(3))
frame_height = int(video.get(4))

size = (frame_width, frame_height)
result = cv2.VideoWriter("outputPath/nameOfVideo",cv2.VideoWriter_fourcc(*'MJPG'), 10, size)

while(True):
    ret, frame = video.read()
    if ret == True:
        result.write(frame)
        cv2.imshow('Frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('s'):
            break
    else:
        break
video.release()
result.release()
cv2.destroyAllWindows()

print("The video was successfully saved")

How to trim the video?

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
ffmpeg_extract_subclip("input_video_path", startTime, endTime, targetname="output_video_path")