栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

使用python开发一个剪切视频、音频的小工具(完整案例)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用python开发一个剪切视频、音频的小工具(完整案例)

前言: 本篇文章将会教你如何用python语言去开发、制作一个可以剪切视频、音频的可视化工具,内容跟以往一样,简单直接,都是硬核内容,具体流程和备注都写在代码注释里了,实在看不懂没关系,代码复制粘贴直接用。

环境:

python3.5+

第三方库:

pip install moviepy (这个模块是用来对视频进行剪切的)

效果展示:

 

功能代码:
from moviepy.editor import *
from tkinter import *
from tkinter.filedialog import askdirectory
import tkinter
import time
import threading
import uuid


class FindURL(object):
    def __init__(self):
        # 创建主窗口
        self.root = Tk()
        # 设置标题
        self.root.title("剪辑小工具")
        self.path_all = StringVar()
        self.path_one = StringVar()
        # 设置界面大小
        self.root['width'] = 500
        self.root['height'] = 400

        # 创建左侧提示文本,采用place坐标方式布局
        Label(self.root, text='文件路径', width=15).place(x=1, y=10)
        Label(self.root, text='开始时间(秒)', width=15).place(x=1, y=50)
        Label(self.root, text='结束时间(秒', width=15).place(x=1, y=70)

        # 输入框设置默认值
        begin_time = tkinter.StringVar(value=1)
        end_time = tkinter.StringVar(value=10)

        self.select_one = tkinter.Entry(textvariable=self.path_one, width=40)
        Button(self.root, text="文件选择", command=self.selectFile).place(x=410, y=5)

        self.url_input3 = tkinter.Entry(textvariable=begin_time, width=20)
        self.url_input4 = tkinter.Entry(textvariable=end_time, width=20)

        # 创建一个显示框
        self.display_info = tkinter.Listbox(self.root, width=60)

        # 创建一个开始按钮
        self.result_button = tkinter.Button(command=self.spider_thred, text="开始测试")
        self.select_one.focus()

    # 打开单个文件
    def selectFile(self, event=None):
        # tkinter提供的askopenfilename函数可以实现打开文件对话框的效果,其返回值为所选文件的绝对路径,并且可指定文件类型
        self.path_ = tkinter.filedialog.askopenfilename(title="选择媒体文件",
                                                        filetypes=(("mp4 files", "*.mp4"), ("mp3 files", "*.mp3")))
        self.path_one.set(self.path_)

    # 设置输入框和按钮的坐标位置
    def gui_arrange(self):
        self.select_one.place(x=100, y=10)
        self.url_input3.place(x=100, y=50)
        self.url_input4.place(x=100, y=70)

        self.display_info.place(x=30, y=160)
        self.result_button.place(x=200, y=120)

    # 功能函数部分
    def spider(self):
        # 获取输入值
        self.midea_path = self.select_one.get()
        self.begin_time = self.url_input3.get()
        self.end_time = self.url_input4.get()
        try:
            self.display_info.insert(tkinter.END,
                                     "即将剪辑%s视频%s秒到%s秒部分" % (self.midea_path, self.begin_time, self.end_time))
            # 剪辑
            video = CompositeVideoClip([VideoFileClip(self.midea_path).subclip(self.begin_time, self.end_time)])
            self.display_info.insert(tkinter.END, "剪辑中...")
            # 写入剪辑完成的音乐
            self.display_info.insert(tkinter.END, "保存中...")
            # 生成随机文件名
            str_ = str(uuid.uuid4())
            # 获取文件后缀
            hz = self.midea_path.split('.')[-1]
            video.write_videofile(str_ + "." + hz)
            self.display_info.insert(tkinter.END, "已全部完成")
        except BaseException as e:
            self.display_info.insert(tkinter.END, "剪辑失败:%s" % str(e))

    # 多线程解决gui阻塞问题
    def spider_thred(self):
        T = threading.Thread(target=self.spider)
        T.start()


def all_main():
    window = FindURL()
    window.gui_arrange()
    tkinter.mainloop()


if __name__ == "__main__":
    all_main()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/994887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号