【发布时间】:2020-07-28 07:31:26
【问题描述】:
我正在用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()
【问题讨论】:
-
您可以暂时停止截屏,然后再继续截屏。只需要将以前的记录与新记录连接起来,您也可以使用全局变量。
-
@AmitRautray 你能告诉我如何使用答案部分中的代码来做到这一点。将不胜感激。
-
是的。 @jadu
标签: python opencv tkinter python-imaging-library