-
-
Save dcode/d7f7870a82edf9e5632b95d911af5539 to your computer and use it in GitHub Desktop.
Convert a section of a YouTube video to an animated GIF
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
#!/bin/bash | |
# brew install x265 | |
# brew install ffmpeg | |
# brew install youtube-dl | |
# brew install imagemagick | |
ID='c5kuYfTCGLg' # YouTube video ID, i.e. https://www.youtube.com/watch?v={ID} | |
# convert the video file to GIF with ffmpeg | |
START='00:00:04.000' # start 4 seconds in | |
LENGTH='00:00:05.000' # end after 5 seconds | |
SIZE='400x300' # size of the output GIF | |
#if [ ! -f $ID.mp4 ]; then | |
# youtube-dl --output '%(id)s.%(ext)s' --recode-video mp4 $ID | |
# ffmpeg -i $(youtube-dl -f 18 --get-url https://www.youtube.com/watch?v=$ID) -ss $START -t $LENGTH -c:v copy -c:a copy $ID.mp4 | |
#fi | |
ffmpeg -i $(youtube-dl -f 18 --get-url https://www.youtube.com/watch?v=$ID) -ss $START -t $LENGTH -c:v copy -c:a copy $ID.mp4 | |
# Update params; start and end isn't needed here | |
# ffmpeg -ss $START -i $ID.mp4 -pix_fmt rgb8 -r 10 -s $SIZE -t $LENGTH $ID-unoptimized.gif | |
ffmpeg -i $ID.mp4 -pix_fmt rgb8 -r 10 -s $SIZE $ID-unoptimized.gif | |
# optimize the GIF with imagemagick | |
convert -layers Optimize $ID-unoptimized.gif $ID.gif | |
# credits: | |
# http://www.commandlinefu.com/commands/view/10002/create-an-animated-gif-from-a-youtube-video | |
# http://superuser.com/a/436109/106809 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment