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

Use Python to develop a gadget for cutting video and audio (complete case)

編輯:Python

Preface :

This article will teach you how to use python Language to develop 、 Make a video that can be cut 、 Audio visualization tools , The content is the same as before , Simple and direct , All hard core content , The specific process and comments are written in the code comments , really It doesn't matter if you don't understand , Copy and paste the code directly .

Environmental Science :

python3.5+

Third party Library :

pip install moviepy ( This module is used to cut video )

Effect display :


 

Function code :

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):
# Create main window
self.root = Tk()
# Set title
self.root.title(" Clip gadget ")
self.path_all = StringVar()
self.path_one = StringVar()
# Set the interface size
self.root['width'] = 500
self.root['height'] = 400
# Create left prompt text , use place Coordinate layout
Label(self.root, text=' File path ', width=15).place(x=1, y=10)
Label(self.root, text=' Starting time ( second )', width=15).place(x=1, y=50)
Label(self.root, text=' End time ( second ', width=15).place(x=1, y=70)
# The input box sets the default value
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=" File selection ", 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)
# Create a display box
self.display_info = tkinter.Listbox(self.root, width=60)
# Create a start button
self.result_button = tkinter.Button(command=self.spider_thred, text=" Start testing ")
self.select_one.focus()
# Open a single file
def selectFile(self, event=None):
# tkinter Provided askopenfilename Function to open the file dialog box , The return value is the absolute path of the selected file , And you can specify the file type
self.path_ = tkinter.filedialog.askopenfilename(title=" Select media file ",
filetypes=(("mp4 files", "*.mp4"), ("mp3 files", "*.mp3")))
self.path_one.set(self.path_)
# Set the coordinate position of input box and button
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)
# Function part
def spider(self):
# Get the input value
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,
" About to edit %s video %s Seconds to %s Second part " % (self.midea_path, self.begin_time, self.end_time))
# clip
video = CompositeVideoClip([VideoFileClip(self.midea_path).subclip(self.begin_time, self.end_time)])
self.display_info.insert(tkinter.END, " Editing ...")
# Write the edited music
self.display_info.insert(tkinter.END, " In storage ...")
# Generate random file names
str_ = str(uuid.uuid4())
# Get file suffix
hz = self.midea_path.split('.')[-1]
video.write_videofile(str_ + "." + hz)
self.display_info.insert(tkinter.END, " It's all done ")
except BaseException as e:
self.display_info.insert(tkinter.END, " Editing failed :%s" % str(e))
# Multithreading solution gui Blocking problem
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