One of my friends in the group has a need to merge many video files into one. During this period, I also found various software, such as format factory, but I could only synthesize 50 files at a time. My friend had thousands of files to synthesize, which was too complicated. For example, the video clip is very powerful, but the software is also very large, the computer configuration requirements are also high. I only need the splicing function, how can I use the ox knife to cut the chicken?
Life is short and I use Python
💡 On second thought, Python is also good at graphics processing, so it is not a problem to process videos, so I searched on the Internet and found a simple solution
Start installation and use
The main use of moviepy library, which provides rich functions, we only need to use simple concatenation functions
Software & Dependencies
- Python
- moviepy
- imageio
- ffmpeg
- Numpy
- Decorator
- tqdm
- imageio
- moviepy
Install Python
This is not much said, directly go to the website to download the corresponding installation package: www.python.org/downloads/r…
Then double click to run, all the way to Next
Install Moviepy
Command line execution:
pip install moviepy
Copy the code
Install FFMPEG
If you have not installed FFMPEG before, you will get an error when importing Moviepy, so you can download FFMpeg from imageio
New text file:
import imageio
import ssl
The following statement is not required, but in some cases the SSL certificate is not trusted when accessing HTTPS
ssl._create_default_https_context = ssl._create_unverified_context
Download the FFMPEG component
imageio.plugins.ffmpeg.download()
Copy the code
Save the above code as xx.py and execute the command in the same directory:
python3 xx.py
Copy the code
4. Start writing the splicing script
# I need moviepy library
from moviepy.editor import *
import os
Define an array
L = []
# Access the Video folder (assuming the videos are in there)
for root, dirs, files in os.walk("./video") :Sort by filename
files.sort()
Pass through all files
for file in files:
# If the suffix is.mp4
if os.path.splitext(file)[1] = ='.mp4':
# concatenate the full path
filePath = os.path.join(root, file)
# Load video
video = VideoFileClip(filePath)
# add to array
L.append(video)
# Splicing video
final_clip = concatenate_videoclips(L)
Generate the target video file
final_clip.to_videofile("./target.mp4", fps=24, remove_temp=False)
Copy the code
Change the source and destination file names of the above code to your own, then save the above code as concatenate.py, and run the following command in the same directory:
python3 concatenate.py
Copy the code
Five, wait for the completion of the operation, the end of the flower 🎉
Wait until the output reaches 100% and the video is merged.
Moviepy also has a number of handy ways to capture video:
video = VideoFileClip("xxoo.mp4")
# Clip the video, 20 seconds before the video
video = video.subclip(0.20)
# Clip the video from 10 seconds to 12 seconds before the end
video = video.subclip(10, video.duration- 12)
Copy the code
Vi. Supplementary content
There is a bit of a problem with the above splicing code. Files.sort () sort the files, and the actual output is not consistent with our normal thinking, such as 1.mp4, 10.mp4, 2.mp4. Because it’s a character by character comparison from front to back, and the results we want are: 1.mp4, 2.mp4, 10.mp4. In addition to writing your own logical code to handle this problem, you can also directly use a third-party library: Natsort, which provides excellent natural sorting methods.
Install the natsort:
pip3 install natsort
Copy the code
Use:
- Import libraries:
from natsort import natsorted
- The code
files.sort()
Replace withfiles = natsorted(files)