Created
April 7, 2023 04:02
-
-
Save espio999/9af09fa1a63bcaa8bc261b88261ce75c to your computer and use it in GitHub Desktop.
Convert MP4 to GIF with MoviePy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from moviepy.editor import * | |
def getCroppedFrames(movie, crop_size, resize_size): | |
cropped_movie = movie.crop(x1=0, y1=0, width=crop_size["w"], height=crop_size["h"]) | |
resized_movie = cropped_movie.resize(height=resize_size["h"]) | |
return resized_movie | |
def makeGIF(path, movie, fps, program): | |
movie.write_gif(path, fps=fps, program=program, logger='bar') | |
folder_path = '/content/drive/MyDrive/20230404/' | |
movie_name = 'screen-20230331-142030.mp4' | |
gif_name = '20230404.gif' | |
source_path = folder_path + movie_name | |
target_path_ffmpeg = folder_path + 'ffmpeg' + gif_name | |
target_path_imageio = folder_path + 'imageio' + gif_name | |
fps = 12 | |
my_movie = VideoFileClip(source_path) | |
# [height, width] | |
crop_size = {"h":1392, "w":1800} | |
resize_size = {"h":495, "w":640} | |
resized_movie = getCroppedFrames(my_movie, crop_size, resize_size) | |
#fps = my_movie.get(cv2.CAP_PROP_FPS) | |
makeGIF(target_path_ffmpeg, resized_movie, fps, 'ffmpeg') | |
makeGIF(target_path_imageio, resized_movie, fps, 'imageio') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment