|
| 1 | +from moviepy.editor import concatenate_videoclips, VideoFileClip |
| 2 | + |
| 3 | + |
| 4 | +def concatenate(video_clip_paths, output_path, method="compose"): |
| 5 | + """Concatenates several video files into one video file |
| 6 | + and save it to `output_path`. Note that extension (mp4, etc.) must be added to `output_path` |
| 7 | + `method` can be either 'compose' or 'reduce': |
| 8 | + `reduce`: Reduce the quality of the video to the lowest quality on the list of `video_clip_paths`. |
| 9 | + `compose`: type help(concatenate_videoclips) for the info""" |
| 10 | + # create VideoFileClip object for each video file |
| 11 | + clips = [VideoFileClip(c) for c in video_clip_paths] |
| 12 | + if method == "reduce": |
| 13 | + # calculate minimum width & height across all clips |
| 14 | + min_height = min([c.h for c in clips]) |
| 15 | + min_width = min([c.w for c in clips]) |
| 16 | + # resize the videos to the minimum |
| 17 | + clips = [c.resize(newsize=(min_width, min_height)) for c in clips] |
| 18 | + # concatenate the final video |
| 19 | + final_clip = concatenate_videoclips(clips) |
| 20 | + elif method == "compose": |
| 21 | + # concatenate the final video with the compose method provided by moviepy |
| 22 | + final_clip = concatenate_videoclips(clips, method="compose") |
| 23 | + # write the output video file |
| 24 | + final_clip.write_videofile(output_path) |
| 25 | + |
| 26 | + |
| 27 | +if __name__ == "__main__": |
| 28 | + import argparse |
| 29 | + parser = argparse.ArgumentParser( |
| 30 | + description="Simple Video Concatenation script in Python with MoviePy Library") |
| 31 | + parser.add_argument("-c", "--clips", nargs="+", |
| 32 | + help="List of audio or video clip paths") |
| 33 | + parser.add_argument("-r", "--reduce", action="store_true", |
| 34 | + help="Whether to use the `reduce` method to reduce to the lowest quality on the resulting clip") |
| 35 | + parser.add_argument("-o", "--output", help="Output file name") |
| 36 | + args = parser.parse_args() |
| 37 | + clips = args.clips |
| 38 | + output_path = args.output |
| 39 | + reduce = args.reduce |
| 40 | + method = "reduce" if reduce else "compose" |
| 41 | + concatenate(clips, output_path, method) |
0 commit comments