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