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

Is the video clip still working repeatedly? Python makes it easy for you

編輯:Python

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 ~

01 install

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

02 clip

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()

Summary

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 .


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