How to slow down a video captured with cv2?

Viewed 354

The problem of my code is that after the video is saved, it is sped up by alot, i've noticed if i change the integer value in cv2.waitkey(), the video changes speed, however even if i set it at 1 it is still sped up

import pyautogui
import os, cv2, threading
import numpy 
import time

#paths
path_videos = os.chdir('C:/Users/mypcname/Desktop/screenrec/videos')

codec = cv2.VideoWriter_fourcc(*'XVID')
video_file = cv2.VideoWriter(os.getcwd()+ '\\' + 'VIDEO2' + '.avi', codec, 23.976, (1920, 1080))



def rec_loop():
    global rec, path_videos, path_frames, frame_number, codec, video_file
    while True:
        #takes BGR screenshot and makes it in a NumPy array
        capture = pyautogui.screenshot()
        frame = numpy.array(capture)

        #converts BGR screenshot into RGB
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        #shows the recording screen live
        cv2.imshow('REC', frame)
       video_file.write(frame)

        #cancel recording
        if cv2.waitKey(250) == ord('Q'):
            break
            cv2.destroyAllWindows()
            video_file.release()

rec_thread = threading.Thread(target=rec_loop)
rec_thread.start()
1 Answers

A couple of things...

  1. You seem to start a new thread and then do nothing in parallel, so that is ostensibly not very sensible.

  2. You will probably find that pyautogui can only capture around 2-3 frames per second, so you will only be passing them at that rate to videowriter, so they will appear speeded up. You could capture 2-10 frames before creating your output file and calculate the rate you achieve and pass that in.

  3. You will be able to achieve a better framerate with ffmpeg, if that's what you want.

Related