程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

使用python開發一個剪切視頻、音頻的小工具(完整案例)

編輯: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()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved