python3.5+
pip install moviepy ( This module is used to cut video )
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()