python - 我如何在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()
解决方案
使用这些功能暂停和恢复按钮。
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")
推荐阅读
- internet-explorer - Windows Embedded 7:出现网站错误时退出信息亭模式
- webpack - 使用 webpack-dev-server 时不编译资产
- javascript - 是否可以更新使用 React.createElement 创建的 React 组件?
- python - 使用 Selenium 时 Cookie 不起作用
- php - php回显选择选项
- html - 如何在 CSS 代码中应用 BDI 标签?
- machine-learning - 如何使用 Keras(tensorflow)限制神经网络回归中预测输出的总和
- javascript - Angular 6 应用程序:无法解析 \node_modules\ts-odatajs\lib\odata 中的“http”
- javascript - 如果调试器中没有断点,this.el.object3D.position.set(0,0,0) 将不起作用
- angular - 如何从angular2中的mat-form-field中获取所选项目