Skip to content

Instantly share code, notes, and snippets.

@laygond
Last active June 30, 2020 21:19
Show Gist options
  • Save laygond/2380338ba3ad4a3ca20b461443c3d67c to your computer and use it in GitHub Desktop.
Save laygond/2380338ba3ad4a3ca20b461443c3d67c to your computer and use it in GitHub Desktop.
Concatenate (glue) two videos given the two inputs and output paths from terminal
# Concatenate (glue) two videos given the two inputs and output paths
# (if destination path not specified it uses same as first input path)
# by Bryan Laygond
# USAGE:
# python gluevideos.py --v1 "Path/to/first/source/file" \
# --v2 "Path/to/second/source/file" \
# --output "Path/to/destination/file"
import os
import argparse
from moviepy.editor import VideoFileClip,VideoClip,concatenate_videoclips
# argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-1", "--v1", required = True,
help = "path to first input video")
ap.add_argument("-2", "--v2", required = True,
help = "path to second input video")
ap.add_argument("-o", "--output", type = str, default = "",
help = "path to output video")
args = vars(ap.parse_args())
# Create video objects
clip1 = VideoFileClip(args["v1"])
clip2 = VideoFileClip(args["v2"])
# Print video Info
print("[INFO] Video 1 fps: ", clip1.fps, " size: ",clip1.size)
print("[INFO] Video 2 fps: ", clip2.fps, " size: ",clip2.size)
# Set parameters if not specified:
# for dest_path
if args["output"] == "":
video_file_extension = args["v1"].split(".")[1] # example mp4
video_file_path = os.path.dirname(args["v1"])
dest_path = os.path.join(video_file_path, "glue_output.") + video_file_extension
else:
dest_path = args["output"]
# Concatenate Clips and Write
glue_clip = concatenate_videoclips([clip1, clip2], method='compose')
glue_clip.write_videofile( dest_path )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment