我的代码的问题是,保存视频后,它会大量加速,我注意到如果我更改cv2.waitkey()中的整数值,视频会改变速度,但是即使我将其设置为1仍会加速
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()
几件事...
您似乎开始一个新线程,然后并行执行任何操作,因此从表面上看不太明智。
您可能会发现pyautogui
每秒只能捕获大约2-3帧,因此您只能以该速率将它们传递给录像机,因此它们的显示速度会加快。您可以在创建输出文件之前捕获2-10帧,然后计算所达到的速率并将其传递。
如果有ffmpeg
,您将能够获得更好的帧率那就是你想要的。