Amazing-Python-Scripts
65 строк · 1.8 Кб
1from cv2 import cv22
3# take the input video
4cap = cv2.VideoCapture('videos/input.mp4')5
6# We need to set resolutions.
7# so, convert them from float to integer.
8frame_width = int(cap.get(3))9frame_height = int(cap.get(4))10
11size = (frame_width, frame_height)12
13# get the frame rate of the input video
14fps = cap.get(cv2.CAP_PROP_FPS)15print("Frames per second using video.get(cv2.CAP_PROP_FPS) : {0}".format(fps))16
17# Below VideoWriter object will create
18# a frame of above defined The output
19# is stored in 'videos/output' file.
20result = cv2.VideoWriter('videos/output.avi', cv2.VideoWriter_fourcc(*'MJPG'),21fps, size)22
23print("processing started...")24
25# continue till the video is not over...
26while (cap.isOpened()):27
28# Capture frame-by-frame from the video29ret, frame = cap.read()30if ret == True:31
32# describe the type of font to be used.33font = cv2.FONT_HERSHEY_SIMPLEX34
35# Use putText() method for inserting text on video36# Parameters:-37# frame: current running frame of the video.38# Text: The text string to be inserted.39# org: bottom-left corner of the text string40# font: the type of font to be used.41# color: the colour of the font.42# thickness: the thickness of the font43
44cv2.putText(frame, 'HELLO WORLD', (50, 50), font, 1, (0, 255, 255), 2,45cv2.LINE_4)46# write in the output file47result.write(frame)48
49# Display the resulting frame50cv2.imshow('video', frame)51
52# creating 'q' as the quit button for the video53if cv2.waitKey(1) & 0xFF == ord('q'):54break55else:56break57
58# release the cap object
59cap.release()60result.release()61
62# close all windows
63cv2.destroyAllWindows()64
65print("Video successfully saved inside the videos folder")66