python - 我如何在python中暂停录制屏幕

标签 python opencv tkinter python-imaging-library

我正在用python创建一个屏幕录制软件。

它几乎完成了这一步,screen_capturing()函数将抓取屏幕截图并将其存储在数组中,然后opencv将这些图像转换为视频文件。

因此,我具有启动和停止功能,但现在我想在运行时暂停它们,然后能够再次恢复它。

我如何才能实现该暂停/恢复部分

import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk, ImageGrab
import cv2
import numpy as np
import threading

p = ImageGrab.grab()
a, b = p.size
filename=(f'C://Users/{os.getlogin()}/desktop/temp_vid.mp4')
fourcc = cv2.VideoWriter_fourcc(*'X264')
frame_rate = 10
out = cv2.VideoWriter() 

def screen_capturing():

    global capturing
    capturing = True

    while capturing:

        img = ImageGrab.grab()
        frame = np.array(img)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        out.write(frame)

def start_screen_capturing():

    if not out.isOpened():

        out.open(filename,fourcc, frame_rate,(a,b))
    print(' rec started')
    t1=threading.Thread(target=screen_capturing, daemon=True)
    t1.start()

def stop_screen_capturing():
    global capturing
    capturing = False
    out.release()
    print('complete')

start_cap = Button(root, text='Start Recording', width=30, command=start_screen_capturing)
start_cap.grid(row=0, column=0)
stop_cap = Button(root, text='Stop Recording', width=30, command=stop_screen_capturing)
stop_cap.grid(row=0, column=1)

root.mainloop()

最佳答案

使用这些功能可通过按钮暂停和继续。

def pause_screen_capturing():
    global capturing
    capturing = False
    print("Paused")

def resume_screen_capturing():
    global capturing
    capturing = True
    if not out.isOpened():
        out.open(filename,fourcc, frame_rate,(a,b))
    t1=threading.Thread(target=screen_capturing, daemon=True)
    t1.start()
    print("Resumed")

关于python - 我如何在python中暂停录制屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61226878/

相关文章:

Python:兼容性需要未使用的参数。如何避免Pylint提示它

image-processing - OpenCV中检测到的轮廓数量不匹配

c++ - 等效于 OpenCV C++ 接口(interface)中的 cvSetImageROI

python - cv2.error : OpenCV(4. 5.2) .error : (-215:Assertion failed) ! _src.empty() 函数 'cv::cvtColor'

python - 如何生成具有不同图像的按钮

python - 使用 AngularJS 在 Tornado 中进行 csrf 检查

python - 在本地 virtualenv 中启动 GAE 时没有名为警告的模块

python - 使用重新编码自动连接 FFMPEG

java - 从 Intent 返回的 Uri 创建 Mat 对象

python - 我安装 mtTkinter 时遇到问题?