Last active
August 27, 2024 17:24
-
-
Save voidcoefficient/203e81ef1669b2089ee5f99c45b3063d to your computer and use it in GitHub Desktop.
Youtube to GIF [Python]
This file contains hidden or 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
# Youtube to GIF Converter | |
# (with youtube-dl and ffmpeg) | |
# made by Paulo Elienay II | |
# at paulo [at] pauloelienay.com | |
# imports | |
import sys, os | |
# vars | |
# default vars | |
output_name = "o.gif" # very self explanatory | |
output_ext = "mp4" # for a *really* better quality, change this to webm | |
output_name2 = "o." + output_ext # very self explanatory | |
path = "~/Videos/gifs/" # very self explanatory | |
fps = 16 # 16 frames per second | |
duration = 5 # 5 seconds | |
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" # Rick Roll | |
start = 0 # default value | |
size = 430 # default value | |
# defs | |
def clear(): os.system('clear') | |
def check(user, default): | |
if user == "": | |
return default | |
if not user == "": | |
return user | |
def change_fps(): # can work | |
if len(sys.argv) > 1: | |
fps = int(sys.argv[1]) | |
# first / hello screen | |
clear() | |
print ("Youtube to GIF") | |
print ("You can change the fps with 'python3 ygif.py X'") | |
print ("Change quality and path editing the file") | |
print ("Press [Enter] for default") | |
# getting the vars from the user | |
print ("") | |
user_url = input("URL link: ") | |
url = check(user_url, url) | |
user_start = input("Start (seconds) [default: 0s]: ") | |
start = check(user_start, start) | |
user_duration = input("Duration (seconds) [default: 5s]: ") | |
duration = check(user_duration, duration) | |
user_name = input("GIF name: ") | |
output_name = check(user_name, output_name) + ".gif" | |
# using youtube-dl to download the video | |
clear() | |
print ('Downloading the video... (1/2)') | |
os.system(f'youtube-dl -f "{output_ext}" -o {output_name2} {url}') | |
# using ffmpeg to convert the video file | |
clear() | |
print ('Converting the video... (2/2)') | |
output_name = path + output_name # adding path to .gif output | |
os.system(f'ffmpeg -loglevel warning -ss {start} -t {duration} -y -i {output_name2} -vf "fps={fps},scale={size}:-1:flags=lanczos,palettegen" palette.png') # ffmpeg magic | |
os.system(f'ffmpeg -loglevel warning -ss {start} -t {duration} -y -i {output_name2} -i palette.png -filter_complex "fps={fps},scale={size}:-1:flags=lanczos[x];[x][1:v]paletteuse" {output_name}') # ffmpeg magic | |
# removing temp files | |
os.system('rm palette.png') | |
os.system(f'rm {output_name2}') | |
# final screen | |
clear() | |
print(f"Your GIF was saved in {output_name}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment