Use Python+Moviepy 5 lines of code to realize the sequential splicing of two videos

☞ ░LaoYuanPython blog post directory: https://blog.csdn.net/LaoYuanPython

Insert picture description here

I. Introduction

Recently I saw several blog posts similar to "n lines of Python code...". They look pretty good. They are concise and practical. They spread knowledge and brought reading volume. They moved the heart of the old ape and decided to follow suit and push one. " N Lines of Python Code Series " article.

Today, I write a minimalist implementation of stitching two videos into one video in sequence. For more " n-line Python code series " articles, please refer to the free column " https://blog.csdn.net/laoyuanpython/category_10858655.html n-line Python code series ".

Two, five lines of code two video sequence stitching

from  moviepy.editor import *

clipVideo1 = VideoFileClip(r"F:\video\WinBasedWorkHard_src.mp4")
clipVideo2 = VideoFileClip(r"F:\video\seeWindAndCloudWithSmile.mp4")
video = concatenate_videoclips([clipVideo1,clipVideo2],'compose')
video.write_videofile(r'f:\video\concatenateVideo2.mp4')

The above five lines of code first load the moviepy related modules, then read in the two video files, stitch them, and finally output to the result file, so that the two video files are stitched sequentially. In fact, the above five lines of code can be combined into the following two lines:

from  moviepy.editor import *

concatenate_videoclips([VideoFileClip(r"F:\video\WinBasedWorkHard_src.mp4"),VideoFileClip(r"F:\video\seeWindAndCloudWithSmile.mp4")],'compose').write_videofile(r'f:\video\concatenateVideo2.mp4')

But this is not readable, so it is more appropriate to use three lines of code like this:

from  moviepy.editor import *
clips =[VideoFileClip(r"F:\video\WinBasedWorkHard_src.mp4"),VideoFileClip(r"F:\video\seeWindAndCloudWithSmile.mp4")]

concatenate_videoclips(clips,'compose').write_videofile(r'f:\video\concatenateVideo2.mp4')

3. Background knowledge

3.1, moviepy introduction

To convert video to animation, Old Ape uses the moviepy library.

MoviePy is a Python module for video editing, which can be used to perform basic video operations (such as cutting, joining, and title insertion), video synthesis (also called non-linear editing), video processing or creating advanced effects.

It can read and write the most common video formats, including GIF. The video that MoviePy can handle is in ffmpeg format. Laoyuan understands that the supported file types include at least: *.mp4 *.wmv *.rm *.avi *.flv *.webm *.wav *rmvb.

MoviePy installation is very simple. When using pip to install, please point the site to a domestic mirror site, otherwise the download will be slow or cannot be downloaded. The old monkey uses Tsinghua's mirror. The instruction is:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple moviepy

For more introduction to Moviepy, please refer to Laoyuan’s free column " https://blog.csdn.net/laoyuanpython/category_9991887.html PyQt+moviepy audio and video editing combat ".

3.2, related functions

The related functions involved in the above code include VideoFileClip, concatenate_videoclips and write_videofile.

  • VideoFileClip is actually a class used to read video from a video file, and the write_videofile method is used to output a video clip to a file. These two functions have been introduced in the previous blog post, so I won’t repeat them here.
  • The concatenate_videoclipsclips function supports merging multiple videos into one video that is played sequentially

Calling syntax :

concatenate_videoclips(clips, method="chain", transition=None, bg_color=None, ismask=False, padding = 0)
Grammar definition:
  • clips: A list of multiple video clips, each element in the list is an object of type VideoClip
  • method: splicing method, there are 2 values
  1. "Chain": Generate a clip that simply outputs multiple consecutive frames of clips, regardless of whether the clips are the same size (resolution) without correction. If none of the clips in the parameter list have a mask, then the final stitched clip will also have no mask, otherwise the final clip will be the stitching of all parameter clip masks. If the corresponding parameter specifies the clip without a mask, the completely opaque clip will be used. As a mask. If multiple clips corresponding to the parameters have different sizes and you want to directly output the stitched clips to a file, you need to use the "compose" method
  2. "Compose": If the clip corresponding to the parameter does not have the same resolution, the final resolution will be the one that makes all clips need not be resized. Therefore, the final clip has the height of the highest clip and the width of the widest clip in the parameter list. All smaller clips will appear centered. If mask=True, the border is transparent, otherwise it is the color specified by "bg-color". The highest FPS of all input clips is the FPS of the final connected clip
  • transition: transition specifies a clip that will be played between every two clips in the list, that is, the resulting clip will not only splice the parameters corresponding to the clips, but also insert a transition clip specified by transition between the two clips.
  • bg_color: only used when method="compose", set the background color, if you want a transparent clip, set it to None, otherwise it is a triple representing RGB color, such as (0,0,0) for black, also I.e. transparent color
  • padding: only used when method="compose", the interval time between two consecutive clips. Note that if this parameter is assigned, the playback of the following clip and the previous clip will overlap for a while. This negative padding parameter will Create the effect of gradually dimming and exiting from the next clip. A non-zero padding value will automatically set the method to "compose"
  • Return value: the final stitched clip

For more information about Moviepy functions and functions, please refer to the guide of " https://blog.csdn.net/LaoYuanPython/article/details/108184832 Python audio and video editing library MoviePy1.0.3 Chinese tutorial guide and executable tool download " Guided introduction.

Four, summary

This article introduces the method of using Python+Moviepy five lines of code to merge two MP4 video files into a video that is played sequentially, and introduces the key functions and syntax of related processing. Moviepy can not only process video files in MP4 format, but actually all FFmpeg Moviepy can handle all formats of video files.

Blogging is not easy, please support:

If you have gained something from reading this article, please like, comment, and bookmark. Thank you for your support!

If you have any questions about the content of the article, you can leave a message in the blog comment area, or follow: Lao Yuan Python WeChat public account to send a message for consultation.

For more information about moviepy, please refer to the guided introduction of " Python Audio and Video Editing Library MoviePy1.0.3 Chinese Tutorial Guide and Executable Tool Download ".

Blogging is not easy, please support:

If you have gained something from reading this article, please like, comment, and bookmark. Thank you for your support!

If you have any questions about the content of the article, you can leave a message in the blog comment area, or follow: Lao Yuan Python WeChat public account to send a message for consultation.

Paid column about the old ape

  1. The paid column " Using PyQt to develop graphical interface Python applications" specifically introduces the basic tutorial of PyQt graphical interface development based on Python, and the corresponding article directory is " Use PyQt to develop graphical interface Python application column directory ";
  2. The paid column " moviepy audio and video development column ) introduces in detail the related methods of moviepy audio and video editing and synthesis and the use of related methods to process related editing and synthesis scenes. The corresponding article directory is " moviepy audio and video development column article directory ";
  3. The paid column " OpenCV-Python Difficult Questions for Beginners " is a companion column of " OpenCV-Python Graphics and Image Processing ". It is the integration of the author's personal perceptions of some problems encountered in the learning of OpenCV-Python graphics and image processing. The relevant information is basically These are the results of repeated research by the old ape, which helps OpenCV-Python beginners to understand OpenCV more deeply. The corresponding article directory is " OpenCV-Python Beginners Difficult Questions Column Directory "
  4. The paid column " Introduction to Python Crawler" introduces the content of crawler development from the perspective of an Internet front-end developer, including the basic knowledge of crawler introduction, as well as crawling CSDN article information, blogger information, likes and comments on articles And other actual content.

The first two columns are suitable for novice readers who have a certain Python foundation but no relevant knowledge to learn. The third column is for everyone to study and use with " OpenCV-Python Graphics and Image Processing ".

For those who lack Python foundation, you can learn Python from scratch through Lao Yuan’s free column " Column: Python Basic Tutorial Directory ".

If you are interested and willing to support the readers of Old Ape, welcome to buy paid columns.

If you have any questions about the content of the article, you can leave a message in the blog comment area, or follow: Lao Yuan Python WeChat public account to send a message for consultation.

Learn Python from the old ape!

☞ ░ to the old ape Python Bowen directory

Guess you like

Origin blog.csdn.net/LaoYuanPython/article/details/114710111