Skip to content

Instantly share code, notes, and snippets.

@bpradana
Created October 5, 2022 03:48
Show Gist options
  • Save bpradana/ebf1454ceb4214a3bd68f8c182f30784 to your computer and use it in GitHub Desktop.
Save bpradana/ebf1454ceb4214a3bd68f8c182f30784 to your computer and use it in GitHub Desktop.
Save video using OpenCV
import cv2
# do something with the frame
def process_frame(frame):
return frame
if __name__ == '__main__':
# input and output file name
INPUT_FILE = 'input_video.mp4'
OUTPUT_FILE = 'output_video.mp4'
# output video configuration
FPS = 30
WIDTH = 1280
HEIGHT = 720
# define video reader
reader = cv2.VideoCapture(INPUT_FILE)
# define video writer
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
writer = cv2.VideoWriter(OUTPUT_FILE, fourcc, FPS, (WIDTH, HEIGHT))
# read video
while reader.isOpened():
res, frame = reader.read()
# do something with the frame
processed_frame = process_frame(frame)
# write the frame
writer.write(processed_frame)
reader.release()
writer.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment