当前位置:   article > 正文

python3制作视频截取工具_tkinter 截取视频

tkinter 截取视频

首先导入相关的库文件:

import cv2
import time
import os
import threading
import win32api
import win32con

import tkinter as tk
from tkinter import messagebox, filedialog
from tkinter import Label, END, Entry, Button, Canvas
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

接着写入负责和粘贴的函数:

def get_copy():
    win32api.keybd_event(17, 0, 0, 0)  #ctrl键位码是17
    win32api.keybd_event(67, 0, 0, 0)   #c键位码是67
    win32api.keybd_event(67, 0, win32con.KEYEVENTF_KEYUP, 0)    #释放按键
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)

def get_paste():
    win32api.keybd_event(17, 0, 0, 0)  #ctrl键位码是17
    win32api.keybd_event(86, 0, 0, 0)   #v键位码是86
    win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)    #释放按键
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

然后写入选择目标视频路径和选择文件存放位置的窗口函数:

# 选择文件安放的位置
def get_wen():
    # 使用文件对话框选择文件夹
    data_2 = filedialog.askdirectory(title="选择文件路径")
    data = data_2.replace('/' , r'\\')
    name = os.path.basename(entry_1.get()).split(".")
    all_name = data + r"\\" + name[0] + "_" + time.strftime("%Y_%m_%d_%H_%M_%S",time.localtime()) + "." + name[-1]
    entry_2.delete(0, END)
    entry_2.insert(0, all_name)

# 选择截取的目标视频
def get_kai():
    # 使用文件对话框选择文件
    data_1 = filedialog.askopenfilename(title="选择视频文件")
    data = data_1.replace('/', r'\\')
    entry_1.delete(0, END)
    entry_1.insert(0, data)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

最后写入截取程序的主函数:

# 主函数
def get_zhu():
    cap = cv2.VideoCapture(entry_1.get())  # 打开视频文件
    frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)  # 获得视频文件的帧数
    fps = cap.get(cv2.CAP_PROP_FPS)  # 获得视频文件的帧率
    width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)  # 获得视频文件的帧宽
    height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # 获得视频文件的帧高

    # 创建保存视频文件类对象
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(entry_2.get(), fourcc, fps, (int(width), int(height)))

    # 计算视频长度/s
    video_length = frames / fps
    start = int(entry_3.get())
    stop = int(entry_4.get())
    if stop > video_length:
        stop = video_length

    # 设置帧读取的开始位置
    cap.set(cv2.CAP_PROP_POS_FRAMES, start * fps)
    pos = cap.get(cv2.CAP_PROP_POS_FRAMES)  # 获得帧位置

    # 添加进度条参数
    num = 0
    zhong_wen = (stop - start)*fps + 1

    while (pos <= stop * fps):
        ret, frame = cap.read()  # 捕获一帧图像
        out.write(frame)  # 保存帧
        pos = cap.get(cv2.CAP_PROP_POS_FRAMES)

        num += 1
        now_pross = (num / zhong_wen) * 420
        # print(now_pross)
        # 用进度条来显示下载进度
        fill_line = canvas.create_rectangle(1, 1.5, 0, 23, width=0, fill="green")
        canvas.coords(fill_line, (0, 0, now_pross, 60))
        window.update()

    cap.release()
    out.release()
    lable1_4 = Label(window, text="100%", font=('微软雅黑', 15), fg='black', bg="green")
    lable1_4.place(x=300, y=205)
    time.sleep(1)
    messagebox.showinfo(title='操作结果', message='文件下载完成')


def thread_it(func):
    '''将函数打包进线程'''
    # 创建
    t = threading.Thread(target=func)
    # 守护 !!!
    t.setDaemon(True)
    # 启动
    t.start()
    # 阻塞--卡死界面!
    # t.join()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

下面放的是博主设计的交互界面,借用之前的下载器界面改的,读者可以自行设计界面:

# 布置界面
window = tk.Tk()
window.title("博主的视频截取软件")
window.geometry("600x300+490+250")
window.config(bg="#FAFAFA")
# 设置窗口是否可以变化长宽,默认可变
window.resizable(width=False, height=False)

lable1_1 = Label(window, text='目标视频: ', font=('微软雅黑', 20), fg='blue', bg="#FAFAFA")
lable1_1.place(x=20, y=20)

lable1_2 = Label(window, text="存放目录:", font=('微软雅黑', 20), fg='blue', bg="#FAFAFA")
lable1_2.place(x=20, y=72)

# 进度条的实现
lable1_3 = Label(text="裁剪进度:", font=('微软雅黑', 20), bg="#FAFAFA", fg="blue")
lable1_3.place(x=20, y=172)
canvas = Canvas(window, width=420, height=25, bg="#FFF0F5")
canvas.place(x=150, y=180)

lable1_4 = Label(window, text="开始(秒):", font=('微软雅黑', 20), fg='blue', bg="#FAFAFA")
lable1_4.place(x=20, y=122)

lable1_5 = Label(window, text="结束(秒):", font=('微软雅黑', 20), fg='blue', bg="#FAFAFA")
lable1_5.place(x=320, y=122)

entry_1 = Entry(window, font=('微软雅黑', 18), width=27, bg='white')
entry_1.place(x=150, y=30)

entry_2 = Entry(window, font=('微软雅黑', 18), width=27, bg='white')
entry_2.place(x=150, y=80)

entry_3 = Entry(window, font=('微软雅黑', 18), width=9, bg='white')
entry_3.place(x=150, y=130)

entry_4 = Entry(window, font=('微软雅黑', 18), width=9, bg='white')
entry_4.place(x=440, y=130)

button_1 = Button(window, text="裁剪", font=("隶书", 20), bg='Snow', activeforeground='gold', activebackground='green',
                  fg="black", command=lambda: thread_it(get_zhu))
button_1.place(x=20, y=240, width=120)

button_2 = Button(window, text="退出", font=("隶书", 20), bg='Snow', activeforeground='gold', activebackground='green',
                  fg="black", command=window.quit)
button_2.place(x=450, y=240, width=120)

button_3 = Button(window, text="...", font=("隶书", 15), bg='Snow', activeforeground='gold', activebackground='green',
                  fg="black", command=get_wen)
button_3.place(x=535, y=80, width=40)

button_6 = Button(window, text="...", font=("隶书", 15), bg='Snow', activeforeground='gold', activebackground='green',
                  fg="black", command=get_kai)
button_6.place(x=535, y=30, width=40)

button_4 = Button(window, text="复制", font=("隶书", 20), bg='Snow', activeforeground='gold', activebackground='green',
                  fg="black", command= get_copy)
button_4.place(x=165, y=240, width=120)

button_5 = Button(window, text="粘贴", font=("隶书", 20), bg='Snow', activeforeground='gold', activebackground='green',
                  fg="black", command= get_paste)
button_5.place(x=310, y=240, width=120)

window.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

代码到这里就结束了,本文设计思路来源博主——升级打怪,文章地址为:https://www.cnblogs.com/lxz123/p/13608098.html,有疑问的地方可以评论或私信博主。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/525087
推荐阅读
相关标签
  

闽ICP备14008679号