|
| 1 | +frommoviepy.editorimportconcatenate_videoclips, VideoFileClip |
| 2 | + |
| 3 | + |
| 4 | +defconcatenate(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) forcinvideo_clip_paths] |
| 12 | +ifmethod=="reduce": |
| 13 | +# calculate minimum width & height across all clips |
| 14 | +min_height=min([c.hforcinclips]) |
| 15 | +min_width=min([c.wforcinclips]) |
| 16 | +# resize the videos to the minimum |
| 17 | +clips= [c.resize(newsize=(min_width, min_height)) forcinclips] |
| 18 | +# concatenate the final video |
| 19 | +final_clip=concatenate_videoclips(clips) |
| 20 | +elifmethod=="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 | +importargparse |
| 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"ifreduceelse"compose" |
| 41 | +concatenate(clips, output_path, method) |
0 commit comments