Skip to content

Instantly share code, notes, and snippets.

@tngaspar
Created August 1, 2022 20:28
Show Gist options
  • Save tngaspar/9ba8f14a42f3e40f885aff13630ca5fe to your computer and use it in GitHub Desktop.
Save tngaspar/9ba8f14a42f3e40f885aff13630ca5fe to your computer and use it in GitHub Desktop.
Merges multiple .mp4 video files into a single file
# Merges multiple .mp4 video files into a single file.
# Add your .mp4 files into a video/ subdirectory and run this script.
# Will generate a final_video.mp4 file in the same directory as this file
import os
from moviepy.editor import VideoFileClip, concatenate_videoclips
# ### SETTINGS - Modify this to your preferences ###
video_fade_secs = 1 # Video fade in and out duration in seconds.
audio_fade_secs = 1 # Audio fade in and out duration in seconds.
video_directory = "video" # Directory with video files. Drop your .mp4 files into this directory
# ### MAIN ###
# get .mp4 files from directory
video_files = [
video_directory + "/" + file
for file in os.listdir(video_directory)
if file.endswith(".mp4")
]
# create list of clips
videos = []
for video in video_files:
videos.append(VideoFileClip(video))
# apply fade effects
videos = [
video.fadein(video_fade_secs)
.fadeout(video_fade_secs)
.audio_fadein(audio_fade_secs)
.audio_fadeout(audio_fade_secs)
for video in videos
]
# merge clips
videoclips = concatenate_videoclips(videos, method="compose")
# write to final .mp4
videoclips.write_videofile("final_video.mp4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment