There are so many great GIFs out there and I want to have copies of them. Twitter makes that harder than it should be by converting them to MP4 and not providing access to the source material. To make it easier, I made a fish pipeline that takes a tweet URL and a filename, extracts the MP4 from that tweet and uses ffmpeg to convert back to GIF.
- ffmpeg
- macOS:
brew install ffmpeg
- Ubuntu/Debian:
apt install ffmpeg
- macOS:
Stick this in your ~/.config/fish/functions/gif-from-tweet.fish
:
function video-url-from-tweet
if count $argv > /dev/null
else
echo "Must provide a url"
return 1
end
set -l thumb (curl --silent $argv[1] | grep -m1 "tweet_video_thumb")
if not [ $thumb ]
echo "Could not find video"
return 1
end
echo $thumb |\
cut -d '"' -f 4 |\
sed 's/.jpg/.mp4/g' |\
sed 's/pbs.twimg.com\/tweet_video_thumb/video.twimg.com\/tweet_video/g'
end
function video-from-tweet
if count $argv > /dev/null
else
echo "Must provide a url"
return 1
end
curl (video-url-from-tweet $argv[1])
end
function video-to-gif
if test (count $argv) -lt 2
echo "Must provide a url and an output filename"
return 1
end
set -l in $argv[1]
set -l out $argv[2]
ffmpeg -i $in \
-filter_complex "[0:v] split [a][b];[a] palettegen [p];[b][p] paletteuse" \
-f gif \
$out
end
function gif-from-tweet
if test (count $argv) -lt 2
echo "Must provide a url and an output filename"
return 1
end
set -l url $argv[1]
set -l out $argv[2]
video-from-tweet $url | video-to-gif - $out
end
video-url-from-tweet <url>
: takes a tweet URL and returns the MP4 embedded in that tweet, or fails if no video is found.video-from-tweet <url>
: returns the raw data of the video that is embedded in the tweetvideo-to-gif <input> <output>
: converts a video to a GIFgif-from-tweet <url> <output>
: takes a tweet URL and an output filename and saves the MP4 embedded in that tweet as a GIF.
$ video-url-from-tweet https://twitter.com/tsunamino/status/1003318804619804672
https://video.twimg.com/tweet_video/DeyBINOUwAAbuif.mp4
# creates `wink.mp4'
$ video-from-tweet https://twitter.com/tsunamino/status/1003318804619804672 > wink.mp4
# creates `wink.gif' from `wink.mp4'
$ video-to-gif wink.mp4 wink.gif
<...a bunch of ffmpeg output...>
# or use this, which pipelines the above and doesn't create intermediate MP4
$ gif-from-tweet https://twitter.com/tsunamino/status/1003318804619804672 wink.gif
### Reference
- [Unix Shells: Bash, Fish, Ksh, Tcsh, Zsh - Hyperpolyglot](http://hyperpolyglot.org/unix-shells)
- [fish: Commands](https://fishshell.com/docs/current/commands.html)