本帖最后由 william0712 于 2024-9-23 10:53 编辑
录屏工具保存了一大堆了,弯弯绕绕的功能太多了,眼花撩乱,我只想要一个“开录-保存”就完了,不想看到太多乱七八糟的功能,于是它诞生了“屏幕录制小工具”,python写的,没有过多的功能,只有一个,就是录屏,完了保存MP4,没了。由于真的太小了,请允许我屏取运行截图截大些吧,以下是运行后,在桌面上显示的大小。有改进建议的欢迎提出,如果觉得它对您有那么一丁点用,请给个免费评分,谢了。
(还是那句,火绒查毒无问题,python打包的,360安全卫士可能会报毒,就算代码里只有一条print "Hello world!",它也报毒,无语了,自行选择,360真的是越来越垃圾了),
由于涉及了FFMPEG和好几个库,打包出来就60多M了,代码只有95行,下载链接放123盘了,支持浏览器直下。
下载链接:https://www.123pan.com/s/EG5A-qw0AH
PY代码如下:
[Python] 纯文本查看 复制代码 import mss
import mss.tools
import cv2
import numpy as np
import tkinter as tk
from tkinter import messagebox, filedialog
import os
# 获取屏幕大小
with mss.mss() as sct:
screen_size = (sct.monitors[1]['width'], sct.monitors[1]['height'])
# 定义编码器
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
recording = False
frames = [] # 用于存储录制的帧
output_writer = None
def start_recording():
global recording, start_button_text
if not recording:
recording = True
start_button_text.set("录制中")
record_screen()
def stop_recording():
global recording, frames, output_writer, start_button_text
if recording:
recording = False
start_button_text.set("开始录制")
try:
# 让用户选择保存位置和文件名
output_file = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
if not output_file:
return
if os.path.exists(output_file):
choice = messagebox.askyesnocancel(
"文件已存在",
f"文件 {output_file} 已存在,是否覆盖?"
)
if choice is None:
return
elif choice == False:
return
output_writer = cv2.VideoWriter(output_file, fourcc, 20.0, screen_size)
# 将所有帧写入视频文件
for frame in frames:
output_writer.write(frame)
output_writer.release()
frames.clear()
messagebox.showinfo("停止", "已停止录制并保存文件。")
except Exception as e:
messagebox.showerror("错误", str(e))
finally:
output_writer = None
def record_screen():
if recording:
with mss.mss() as sct:
monitor = {'top': 0, 'left': 0, 'width': screen_size[0], 'height': screen_size[1]}
img = sct.grab(monitor)
frame = np.array(img)
# 确保颜色空间正确
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
frames.append(frame) # 存储每一帧
root.after(15, record_screen) # 根据需要调整刷新率
# 创建GUI
root = tk.Tk()
root.title("屏幕录制小工具 by.KOG丛林")
frame = tk.Frame(root)
frame.pack()
# 使用 StringVar 来动态更新按钮上的文本
start_button_text = tk.StringVar(value="开始录制 ")
# 开始录制按钮
start_button = tk.Button(frame, textvariable=start_button_text, command=start_recording)
start_button.pack(side=tk.LEFT)
# 停止录制按钮
stop_button = tk.Button(frame, text=" 停止录制", command=stop_recording)
stop_button.pack(side=tk.LEFT)
root.mainloop() |