Skip to content

Instantly share code, notes, and snippets.

@artturijalli
Created April 27, 2021 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artturijalli/44b0e5b2d2372f41223b59d377eea929 to your computer and use it in GitHub Desktop.
Save artturijalli/44b0e5b2d2372f41223b59d377eea929 to your computer and use it in GitHub Desktop.
Convert video to gif with 3 lines of code. Install Tkinter with brew install python-tk. Install moviepy with pip install moviepy
from moviepy.editor import VideoFileClip
from tkinter.filedialog import *
import os
# Accept only .mov and .mp4 files. Feel free to change.
accepted_files = [("Mov files", "*.mov"), ("MP4 files", "*.mp4")]
# Select a video which is one of the accepted file types from your machine
video_file = askopenfilename(filetypes=accepted_files)
# Create a video clip instance
clip = VideoFileClip(video_file)
# Get the video file name. E.g. 'path/to/video.mov' --> 'video.mov'
video_file_name = os.path.basename(video_file)
# Split video name to parts. E.g. 'video.mov' --> ('video', 'mov')
parts = os.path.splitext(video_file_name)
# Get the name of the video from the parts and add a .gif to the end.
gif_name = parts[0] + ".gif"
# Create a gif with the same name as the original video.
clip.write_gif(gif_name, fps=10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment