首页 > 解决方案 > 我如何在python中暂停录制屏幕

问题描述

我正在用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()

标签: pythonopencvtkinterpython-imaging-library

解决方案


使用这些功能暂停和恢复按钮。

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")

推荐阅读