Skip to content

Instantly share code, notes, and snippets.

@pakkinlau
Created November 23, 2023 20:32
Show Gist options
  • Save pakkinlau/3963b1081869a0e4ec33db192aa8c0dd to your computer and use it in GitHub Desktop.
Save pakkinlau/3963b1081869a0e4ec33db192aa8c0dd to your computer and use it in GitHub Desktop.
This script can be a handy tool for managing and manipulating large video files, especially when you need to stitch together and compress several clips into one video. Please note that this script assumes that the input video files are in .avi format and their names follow the "MOVI####.avi" pattern. Also, the output video is in .avi format with…
import os
import moviepy.editor as mpy
import glob
def compress_video(video_path, output_path, video_bitrate="1000k"):
clip = mpy.VideoFileClip(video_path)
# Compress video
clip.write_videofile(output_path, codec='mpeg4', bitrate=video_bitrate)
def concatenate_videos(videos_path, output_path):
# Read and sort the video file names
video_files = sorted(
glob.glob(os.path.join(videos_path, "MOVI*.avi")),
key=lambda x: int(os.path.splitext(os.path.basename(x))[0][4:]),
)
# Compress and load videos
clips = []
for idx, video_file in enumerate(video_files):
compressed_path = f"compressed_{idx}.avi"
compress_video(video_file, compressed_path)
clips.append(mpy.VideoFileClip(compressed_path))
# Concatenate videos and write to file
final_clip = mpy.concatenate_videoclips(clips)
final_clip.write_videofile(output_path, codec='mpeg4')
# Path where the videos are located
videos_path = r"C:\Users\kinla\Downloads\VDO"
# Path for the output video
output_path = "output.avi"
concatenate_videos(videos_path, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment