Hello everyone , I'm Chen Chen ~
When making video clips , Still working repeatedly ?
today , Let me teach you how to use Python Help you with these repetitive editing work ~
Batch editing of videos , Three libraries are required , Namely Moviepy Kuhe Pathlib library , also Tkinter library .
First, we install these two libraries , The order is as follows :
pip install moviepy pip install pathlib
We have installed the required libraries above , Now let's start editing the video .
Video clip
Moviepy There's a VideoFileClip function , After passing the video into this function , Will return a VideoFileClip Instance object , The object can be subclip() Editing , The code is as follows :
from moviepy.editor import *
clip = VideoFileClip('1.mp4').subclip(2,4)
# Store the cut video
clip.write_videofile('2.mp4')Video merge
The first step is the same as video editing , You need to transfer the merged video into VideoFileClip function , Make it a VideoFileClip Instance object . And then call Concatenate_videoclips function , Will all VideoFileClip Merge the videos of the instance object , Finally, save it . The code is as follows :
from moviepy.editor import VideoFileClip,concatenate_videoclips
clip_1 = VideoFileClip('1.mp4')
clip_2 = VideoFileClip('2.mp4')
file = concatenate_videoclips([clip_1,clip_2])
file.write_videofile('3.mp4')The video turns into gif
Convert the video into gif It's simple , When the video passes VideoFileClip Function processing becomes VideoFileClip After the instance object , You can save it directly as gif Format . The code is as follows :
from moviepy.editor import *
file = VideoFileClip(k)
file.write_gif(f'{name}.gif')Batch
The above is just for a single video , Or operate the videos in individual folders , But what we want is batch editing 、 Merge and transform . here ,Pathlib The library provides us with two Path and PurePath function , It is very convenient to find in the computer mp4 file , The code is as follows :
files = []
p = Path(src_path)
for x in p.iterdir():
if PurePath(x).match('*.mp4'):
files.append(x)Then merge with the above code , You can achieve the function of batch acquisition .
GUI Interface
The functions of the program have been fully realized , Now we need to design the interface of the program , Here we use Tkinter Library for interface design , The code is as follows :
# create a window
root = tk.Tk()
# title
root.title(' Video clip ')
# Window size
root.geometry('450x200')
# Initial position of the window
root.geometry('+400+200')
# Tag control
label_1 = tk.Label(root, text=' Enter the file address ', font=(r' Su Xin's poems are written in regular script .ttf', 16), fg='black')
label_1.grid()
label_2 = tk.Label(root, text=' Enter the clip time ', font=(r' Su Xin's poems are written in regular script .ttf', 16), fg='black')
label_2.grid()
# Input box
enter_1 = tk.Entry(root, font=(r' Su Xin's poems are written in regular script .ttf', 16))
enter_2 = tk.Entry(root, font=(r' Su Xin's poems are written in regular script .ttf', 16))
# Set the position of the input box
enter_1.grid(row=0, column=1)
enter_2.grid(row=1, column=1)
# Button
button = tk.Button(root, text=' Start ', font=(r' Su Xin's poems are written in regular script .ttf', 16), command=jianjivideo)
# Set the position of the button
button.grid(row=1, column=2)
# Display window
root.mainloop()1. This article introduces in detail , How to use Moviepy Kuhe Pathlib library , also Tkinter library , Make a batch video processing software .
2. Interested students can reproduce the code for learning .