开发者社区> 问答> 正文

如何减慢用cv2捕获的视频?

我的代码的问题是,保存视频后,它会大量加速,我注意到如果我更改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()

问题来源:stackoverflow

展开
收起
is大龙 2020-03-24 20:31:33 588 0
1 条回答
写回答
取消 提交回答
  • 几件事...

    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.

    回答来源:stackoverflow

    2020-03-24 20:31:41
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载