Hello, I’m Yue Chuang.

Start installation

The main thing is to use the Moviepy library, which provides a lot of functionality, and we just use simple concatenation functions

Install Python

This is not much said, directly go to the website to download the corresponding installation package: www.python.org/downloads/r…

And then double click to run, remember Add to Path and then next

2. Install Moviepy

Command line execution:

pip install moviepy
Copy the code

Install FFMPEG

Let’s first try the installation using the following command:

pip install ffmpeg
pip install imageio-ffmpeg
Copy the code

Save the above code as xx.py and run the command in the same directory:

python3 xx.py
Copy the code

If the installation fails, try the following operations:

If you have not installed ffmpeg before, you will see an error when importing Moviepy, so you can use the imageio method to download ffmpeg

New text file:

import imageio
import ssl

The following statement is not required, but in some cases the SSL certificate is not trusted
ssl._create_default_https_context = ssl._create_unverified_context

Download the ffmPEG component
imageio.plugins.ffmpeg.download()
Copy the code

Start writing the concatenation script

# Moviepy library is needed
from moviepy.editor import *
import os

# Define an array
L = []

# Access the video folder (assuming all the videos are in there)
for root, dirs, files in os.walk("./video") :Sort by file name
    files.sort()
    # Traverse all files
    for file in files:
        # if the suffix is.mp4
        if os.path.splitext(file)[1] = ='.mp4':
            # Splice to complete path
            filePath = os.path.join(root, file)
            # Load video
            video = VideoFileClip(filePath)
            # Add to array
            L.append(video)

# Splice 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 folder and destination file name of the above code to whatever you want, then save the above code as concatenate. Py and run the following command in the same directory:

python3 concatenate.py
Copy the code

Five, waiting for the completion of the operation, the end of the flower 🎉

Wait for the output to reach 100% and the video will merge.

Moviepy - Building video ./target.mp4.
MoviePy - Writing audio in targetTEMP_MPY_wvf_snd.mp3
MoviePy - Done.
Moviepy - Writing video ./target.mp4

Moviepy - Done !
Moviepy - video ready ./target.mp4
Copy the code

But moviepy also has a lot of convenient ways to do it, like capture videos:

video = VideoFileClip("xxoo.mp4")

# Clip video, capture the first 20 seconds of the video
video = video.subclip(0.20)

Clip the video from 10 seconds to 12 seconds before the end of the video
video = video.subclip(10, video.duration-12)
Copy the code

Six, supplementary content

There is actually a little bit of a problem with the splicing code above. Files.sort () sorts the files, and the actual result is not what we normally think: 1.mp4, 10.mp4, 2.mp4. Because it compares characters from front to back, and what we want is generally: 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 an excellent natural sorting method.

Install the natsort:

pip3 install natsort
Copy the code

Use:

  • Import libraries:from natsort import natsorted
  • The codefiles.sort()Replace withfiles = natsorted(files)

The code is as follows:

""" project = 'Code', file_name = 'video_charm ', author = 'AI FFFF' time = '2020/4/25 12:19', product_name = PyCharm, Code is far away from bugs with the God animal protecting I love animals. They taste delicious. """
# Moviepy library is needed
from moviepy.editor import *
from natsort import natsorted
import os

# Define an array
L = []

# Access the video folder (assuming all the videos are in there)
for root, dirs, files in os.walk("./video") :Sort by file name
    # files.sort()
    files = natsorted(files)
    # Traverse all files
    for file in files:
        # if the suffix is.mp4
        if os.path.splitext(file)[1] = ='.mp4':
            # Splice to complete path
            filePath = os.path.join(root, file)
            # Load video
            video = VideoFileClip(filePath)
            # Add to array
            L.append(video)

# Splice 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

Such success I made to MP4 video splicing.