Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to solve the problem of repetitive work in video clips

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

I would like to share with you how to use Python to solve the repetitive work of video editing. I hope you will get something after reading this article. Let's discuss it together.

01 installation

Batch editing of videos requires three libraries, namely the Moviepy library and the Pathlib library, as well as the Tkinter library.

First, we install these two libraries with the following command:

Pip install moviepypip install pathlib02 clip

Now that we have installed the required libraries, let's start editing the video.

Video clips:

The Moviepy library has a VideoFileClip function. When the video is passed into this function, it returns a VideoFileClip instance object that can be edited by subclip ().

The code is as follows:

From moviepy.editor import * clip = VideoFileClip ('1.mp4'). Subclip (2p4) # store the clipped video on clip.write_videofile (' 2.mp4')

Video merge:

The first step is like a video clip, you need to pass the merged video into the VideoFileClip function to make it a VideoFileClip instance object. Then call the Concatenate_videoclips function to merge the videos of all VideoFileClip instance objects, and finally save them.

The code is as follows:

From moviepy.editor import VideoFileClip,concatenate_videoclipsclip_1 = VideoFileClip ('1.mp4') clip_2 = VideoFileClip (' 2.mp4') file = concatenate_videoclips ([clip_1,clip_2]) file.write_videofile ('3.mp4')

Convert video to gif:

It is very easy to convert a video to gif. When the video is processed into a VideoFileClip instance object by the VideoFileClip function, it can be directly saved to gif format.

The code is as follows:

From moviepy.editor import * file = VideoFileClip (k) file.write_gif (f'{name} .gif')

Batch:

The above is only for individual videos, or individual folder videos, but what we want is batch editing, merging and conversion. Here, the Pathlib library provides us with two Path and PurePath functions, which can easily find the mp4 file in the computer.

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 to achieve the function of batch acquisition.

GUI interface:

All the functions of the program have been realized, and now we need to design the interface of the program. Here we use the Tkinter library to design the interface.

The code is as follows:

# create window root = tk.Tk () # title root.title ('video clip') # window size root.geometry ('450x200') # initial position of window root.geometry (' + 400x200') # controls labeled label_1 = tk.Label (root, text=' enter file address', font= (r'Su Xin Shiliukai .ttf', 16), fg='black') label_1.grid () label_2 = tk.Label (root Text=' input editing time', font= (r'Su Xin Shi Liu Kai Jian .ttf', 16), fg='black') label_2.grid () # input box enter_1 = tk.Entry (root, font= (r'Su Xin Shi Liu Kai Jian .ttf', 16)) enter_2 = tk.Entry (root, font= (r'Su Xin Shi Liu Kai Jian .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 Shi Liu Kai Jian .ttf', 16), command=jianjivideo) # set the position of the button button.grid (row=1, column=2) # display window root.mainloop () finished reading this article I believe you have a certain understanding of "how to use Python to solve the repetitive work of video editing". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 298

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report