Amazing-Python-Scripts

Форк
0
65 строк · 1.8 Кб
1
from cv2 import cv2
2

3
# take the input video
4
cap = cv2.VideoCapture('videos/input.mp4')
5

6
# We need to set resolutions.
7
# so, convert them from float to integer.
8
frame_width = int(cap.get(3))
9
frame_height = int(cap.get(4))
10

11
size = (frame_width, frame_height)
12

13
# get the frame rate of the input video
14
fps = cap.get(cv2.CAP_PROP_FPS)
15
print("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.
20
result = cv2.VideoWriter('videos/output.avi', cv2.VideoWriter_fourcc(*'MJPG'),
21
                         fps, size)
22

23
print("processing started...")
24

25
# continue till the video is not over...
26
while (cap.isOpened()):
27

28
    # Capture frame-by-frame from the video
29
    ret, frame = cap.read()
30
    if ret == True:
31

32
        # describe the type of font to be used.
33
        font = cv2.FONT_HERSHEY_SIMPLEX
34

35
        # Use putText() method for inserting text on video
36
        # 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 string
40
        # font: the type of font to be used.
41
        # color: the colour of the font.
42
        # thickness: the thickness of the font
43

44
        cv2.putText(frame, 'HELLO WORLD', (50, 50), font, 1, (0, 255, 255), 2,
45
                    cv2.LINE_4)
46
        # write in the output file
47
        result.write(frame)
48

49
        # Display the resulting frame
50
        cv2.imshow('video', frame)
51

52
        # creating 'q' as the quit button for the video
53
        if cv2.waitKey(1) & 0xFF == ord('q'):
54
            break
55
    else:
56
        break
57

58
# release the cap object
59
cap.release()
60
result.release()
61

62
# close all windows
63
cv2.destroyAllWindows()
64

65
print("Video successfully saved inside the videos folder")
66

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.