Skip to content

Instantly share code, notes, and snippets.

@WarpEngineer
Forked from brianloveswords/gif-from-tweet.md
Created June 7, 2018 12:56
Show Gist options
  • Save WarpEngineer/1405d2217a079db420a767cc97a2100c to your computer and use it in GitHub Desktop.
Save WarpEngineer/1405d2217a079db420a767cc97a2100c to your computer and use it in GitHub Desktop.

Revisions

  1. @brianloveswords brianloveswords revised this gist Jun 3, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gif-from-tweet.md
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@ There are so many great GIFs out there and I want to have copies of them. Twitte
    ## Dependencies

    - ffmpeg
    - macOS: `brew install ffmpeg`
    - Ubuntu/Debian: `apt install ffmpeg`
    - macOS: `brew install ffmpeg`
    - Ubuntu/Debian: `apt install ffmpeg`

    ## Install

  2. @brianloveswords brianloveswords revised this gist Jun 3, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gif-from-tweet.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,9 @@ There are so many great GIFs out there and I want to have copies of them. Twitte

    ## Dependencies

    - ffmpeg: `brew install ffmpeg`
    - ffmpeg
    - macOS: `brew install ffmpeg`
    - Ubuntu/Debian: `apt install ffmpeg`

    ## Install

  3. @brianloveswords brianloveswords revised this gist Jun 3, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions gif-from-tweet.md
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,10 @@

    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 bash pipeline that takes a tweet URL and a filename, extracts the MP4 from that tweet and uses ffmpeg to convert back to GIF.

    ## Dependencies

    - ffmpeg: `brew install ffmpeg`

    ## Install

    Stick this in your `~/.profile`:
  4. @brianloveswords brianloveswords created this gist Jun 3, 2018.
    91 changes: 91 additions & 0 deletions gif-from-tweet.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    # gif-from-tweet

    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 bash pipeline that takes a tweet URL and a filename, extracts the MP4 from that tweet and uses ffmpeg to convert back to GIF.

    ## Install

    Stick this in your `~/.profile`:

    ```bash
    video-url-from-tweet() {
    if [ "$1" ]; then
    url=$1
    else
    echo "Must provide a url"
    return 1
    fi

    curl --silent $url |\
    # should find the <meta> tag with content="<thumbnail url>"
    (grep -m1 "tweet_video_thumb" ||\
    echo "Could not find video" && return 1) |\

    # from: <meta property="og:image" content="https://pbs.twimg.com/tweet_video_thumb/xxxxxxxxxx.jpg">
    # to: https://pbs.twimg.com/tweet_video_thumb/xxxxxxxxxx.jpg
    cut -d '"' -f 4 |\

    # from: https://pbs.twimg.com/tweet_video_thumb/xxxxxxxxxx.jpg
    # to: https://video.twimg.com/tweet_video/xxxxxxxxxx.mp4
    sed 's/.jpg/.mp4/g' |\
    sed 's/pbs.twimg.com\/tweet_video_thumb/video.twimg.com\/tweet_video/g'
    }
    video-from-tweet() {
    if [ "$1" ]; then
    url=$1
    else
    echo "Must provide a url"
    return 1
    fi
    curl $(video-url-from-tweet $url)
    }
    video-to-gif() {
    # derived from https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
    if [ "$2" ]; then
    input=$1
    output=$2
    else
    echo "Must provide an input file and output file"
    return 1
    fi

    ffmpeg -i $input \
    -filter_complex "[0:v] split [a][b];[a] palettegen [p];[b][p] paletteuse" \
    -f gif \
    $output
    }
    gif-from-tweet() {
    if [ "$2" ]; then
    url=$1
    output=$2
    else
    echo "Must provide a url and an output filename"
    return 1
    fi
    video-from-tweet $url | video-to-gif - $output
    }

    ```

    ## Usage

    - `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 tweet
    - `video-to-gif <input> <output>`: converts a video to a GIF
    - `gif-from-tweet <url> <output>`: takes a tweet URL and an output filename and saves the MP4 embedded in that tweet as a GIF.

    ### Example

    ```bash
    $ 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