Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.
In this tutorial, you will learn how to join two or more video files together using Python with the help of the MoviePy library.
This tutorial is similar to the joining audio files tutorial, but we'll be joining videos in this one.
To get started, let's install MoviePy first:
$ pip install moviepy
MoviePy uses FFmpeg software under the hood and will install it once you execute MoviePy code the first time. Open up a new Python file and write the following code:
def concatenate(video_clip_paths, output_path, method="compose"): """Concatenates several video files into one video file and save it to `output_path`. Note that extension (mp4, etc.) must be added to `output_path` `method` can be either 'compose' or 'reduce': `reduce`: Reduce the quality of the video to the lowest quality on the list of `video_clip_paths`. `compose`: type help(concatenate_videoclips) for the info""" # create VideoFileClip object for each video file clips = [VideoFileClip(c) for c in video_clip_paths] if method == "reduce": # calculate minimum width & height across all clips min_height = min([c.h for c in clips]) min_width = min([c.w for c in clips]) # resize the videos to the minimum clips = [c.resize(newsize=(min_width, min_height)) for c in clips] # concatenate the final video final_clip = concatenate_videoclips(clips) elif method == "compose": # concatenate the final video with the compose method provided by moviepy final_clip = concatenate_videoclips(clips, method="compose") # write the output video file final_clip.write_videofile(output_path)
Okay, there is a lot to cover here. The
concatenate() function we wrote accepts the list of video files (
video_clip_paths), the output video file path, and the method of joining.
First, we loop over the list of video files and load them using
VideoFileClip() object from MoviePy. The method parameter accepts two possible values:
reduce: This method reduces the quality of the video to the lowest on the list. For instance, if one video is
1280x720and the other is
320x240, the resulting file will be
320x240. That's why we use the
resize()method to the lowest height and width.
compose: MoviePy advises us to use this method when the concatenation is done on videos with different qualities. The final clip has the height of the highest clip and the width of the widest clip of the list. All the clips with smaller dimensions will appear centered.
Feel free to use both and see which one fits your case best.
Now let's use the argparse module to parse command-line arguments:
if __name__ == "__main__": import argparse parser = argparse.ArgumentParser( description="Simple Video Concatenation script in Python with MoviePy Library") parser.add_argument("-c", "--clips", nargs="+", help="List of audio or video clip paths") parser.add_argument("-r", "--reduce", action="store_true", help="Whether to use the `reduce` method to reduce to the lowest quality on the resulting clip") parser.add_argument("-o", "--output", help="Output file name") args = parser.parse_args() clips = args.clips output_path = args.output reduce = args.reduce method = "reduce" if reduce else "compose" concatenate(clips, output_path, method)
Since we're expecting a list of video files to be joined together, we need to pass
nargs for the parser to accept one or more video files.
$ python concatenate_video.py --help
usage: concatenate_video.py [-h] [-c CLIPS [CLIPS ...]] [-r REDUCE] [-o OUTPUT] Simple Video Concatenation script in Python with MoviePy Library optional arguments: -h, --help show this help message and exit -c CLIPS [CLIPS ...], --clips CLIPS [CLIPS ...] List of audio or video clip paths -r REDUCE, --reduce REDUCE Whether to use the `reduce` method to reduce to the lowest quality on the resulting clip -o OUTPUT, --output OUTPUT Output file name
Let's test it out:
$ python concatenate_video.py -c zoo.mp4 directed-by-robert.mp4 -o output.mp4
Here I'm joining
directed-by-robert.mp4 files to produce
output.mp4. Note that the order is important, so you need to pass them in the order you want. You can pass as many video files as you want. The
output.mp4 will appear in the current directory and you'll see similar output to this:
Moviepy - Building video output.mp4. MoviePy - Writing audio in outputTEMP_MPY_wvf_snd.mp3 MoviePy - Done. Moviepy - Writing video output.mp4 Moviepy - Done ! Moviepy - video ready output.mp4
And this is the output video:
You can also use the
reduce method with the following command:
$ python concatenate_video.py -c zoo.mp4 directed-by-robert.mp4 --reduce -o output-reduced.mp4
Alright, there you go. I hope this tutorial helped you out on your programming journey!
Finally, if you're a beginner and want to learn Python, I suggest you take the Python For Everybody Coursera course, in which you'll learn a lot about Python. You can also check our resources and courses page to see the Python resources I recommend on various topics!
Happy coding ♥View Full Code