python实现延时录屏效果

原理:通过不同时间间隔,进行截图 然后n多截图转化成视频即可。
引用地址:
https://github.com/kohunglee/rec_screen_py/blob/main/main.py

from PIL import ImageGrab
import os
import time
import tkinter as tk
from tkinter import messagebox
import cv2

global_pic_name_i = 100000000  # 截屏图像名称序列
global_pic_time = 5000  # 截屏的时间差
global_afterid = None  # after id
global_unixtime = None  # unix 时间戳

# 截屏并保存的函数
def takeScrPic(folder, picName):
    screenshot_folder = folder  # 文件夹
    screenshot_name = picName  # 截屏图像名称
    new_width = 1080  # 新的宽度
    pic_quality = 45 # 截图的图片质量(1~95)
    if not os.path.exists(screenshot_folder):  # 文件夹不存在就建立一个
        os.makedirs(screenshot_folder)
    screenshot = ImageGrab.grab()  # 截屏命令
    width, height = screenshot.size  # 宽高计算
    new_height = int(height * (new_width / width))
    resized_screenshot = screenshot.resize((new_width, new_height))  # 根据新宽高来转换图片
    resized_screenshot = resized_screenshot.convert('RGB')  # rgba 变成 rgb
    screenshot_path = os.path.join(screenshot_folder, screenshot_name)
    resized_screenshot.save(screenshot_path, 'JPEG', quality=pic_quality)  # 保存到目录
    print(f'The resized screenshot is {new_width} wide x {new_height} tall and saved as {screenshot_path}')  # 打印结果

# 程序的界面
def makeUI():
    root = tk.Tk()
    root.title('screenRec')
    root.attributes('-topmost', True) # 将窗口置顶
    width = 200
    height = 170
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    size_geo = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2 - 70)
    root.geometry(size_geo)
    # root.resizable(False, False)
    label = tk.Label(root, text='输入项目名称,点击开始录制')
    label.pack()
    itemNameInput = tk.Entry(root)
    itemNameInput.pack(pady=10)
    startButton = tk.Button(root, text='开始录制', command=clickDef)
    startButton.pack()
    endButton = tk.Button(root, text='停止录制', command=stopAfter, state='disabled')
    endButton.pack()
    convertButton = tk.Button(root, text='转换成视频', command=item2video, state='disabled')
    convertButton.pack()
    return root, label, itemNameInput, startButton, endButton, convertButton

# 开始录制的点击事件
def clickDef():
    global global_pic_name_i, global_afterid, global_unixtime
    itemProjName = itemNameInput.get()
    if not itemProjName:
        messagebox.showerror('error', '项目名称不能为空!')
        return 0
    if not global_unixtime:
        global_unixtime = int(time.time())
    takeScrPic(itemProjName, itemProjName + str(global_pic_name_i) + '.jpg')
    global_pic_name_i = global_pic_name_i + 1
    label.config(text='已截 ' + str(global_pic_name_i - 100000000) + ' 张,' + '已录制' + toGoodTime( int(time.time()) - global_unixtime ))
    global_afterid = root.after(global_pic_time, clickDef)
    endButton.config(state='normal')
    startButton.config(state='disabled')

# 将秒转化为分秒格式
def toGoodTime(sec):
    minutes = str(sec // 60)
    remaining_seconds = str(sec % 60)
    return str(' ' + minutes+' 分 '+ remaining_seconds +' 秒')

# 停止录制
def stopAfter():
    root.after_cancel(global_afterid)
    endButton.config(state='disabled')
    convertButton.config(state='normal')

# 点击 转化成视频 按钮
def item2video():
    label.config(text = '转化中,请稍后...')
    itemProjName = itemNameInput.get()
    convertButton.config(state='disabled')
    convert2Video(itemProjName, itemProjName + '.mp4')

# 将序列转化为视频
def convert2Video(picdir, outputname, myfps=25):
    im_dir = picdir
    save_video_dir = './video_output/'
    if not os.path.exists(save_video_dir):
        os.makedirs(save_video_dir)
    fps = myfps
    frames = sorted(os.listdir(im_dir))  # 提取序列
    img = cv2.imread(os.path.join(im_dir, frames[0]))
    img_size = (img.shape[1], img.shape[0])
    seq_name = outputname
    video_dir = os.path.join(save_video_dir, seq_name)
    # fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
    fourcc = cv2.VideoWriter_fourcc(*'avc1')
    videowriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)

    for frame in frames:
        f_path = os.path.join(im_dir, frame)
        image = cv2.imread(f_path)
        videowriter.write(image)
        print(frame + ' has been written!')
    videowriter.release()
    label.config(text = '转化完成')
    # ↑↑↑↑↑ convert2Video('myscreen', 'out.mp4') 本函数的示例用法 ↑↑↑↑↑

root, label, itemNameInput, startButton, endButton, convertButton = makeUI()
root.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值