miguel.nz

How to Create a Simple GIF-to-WebM Converter Bash Shortcut

May 1, 2025   |   2 minutes read.

If you frequently convert GIFs to WebM format (which offers better compression and quality), creating a custom terminal shortcut can save you time. Here’s how I set up a simple towebm command in my .zshrc file to streamline the process.

Why Convert GIFs to WebM?

GIFs are widely used, but they often have large file sizes and limited color support. WebM (using the VP9 codec) provides:

  • Smaller file sizes (often 50-80% reduction)
  • Better quality (supports more colors and smoother gradients)
  • Wider compatibility (works well in modern browsers)

Instead of typing a long ffmpeg command every time, I automated it with a simple bash function.

How to Set Up the towebm Shortcut

Step 1: Edit Your .zshrc (or .bashrc)

Open your shell configuration file:

nano ~/.zshrc  # for Zsh users
# or
nano ~/.bashrc # for Bash users

Step 2: Add the Conversion Function

Paste this function at the end of the file:

towebm() {
    if [ -z "$1" ]; then
        echo "Usage: towebm [inputFile.gif]"
        return 1
    fi
    
    input_file="$1"
    output_file="${input_file%.*}.webm"
    
    ffmpeg -i "$input_file" -c:v vp9 -b:v 0 -crf 41 "$output_file"
    
    echo "✅ Converted: $output_file"
}

Step 3: Reload Your Shell

Apply the changes:

source ~/.zshrc  # or ~/.bashrc

Step 4: Use It!

Now, just run:

towebm my_animation.gif

It will generate my_animation.webm in the same folder.

How It Works

  • -c:v vp9 → Uses the VP9 video codec (efficient for WebM).
  • -b:v 0 → Lets -crf control quality (no bitrate limit).
  • -crf 41 → Adjusts compression (lower = better quality, higher = smaller file).
  • ${input_file%.*}.webm → Keeps the original filename but changes the extension.

Bonus: Extra Features

Want more control? Modify the function to accept quality settings:

towebm() {
    if [ -z "$1" ]; then
        echo "Usage: towebm [inputFile.gif] [quality=20-50, default:41]"
        return 1
    fi
    
    input_file="$1"
    quality="${2:-41}"  # Default CRF=41 if not provided
    output_file="${input_file%.*}.webm"
    
    ffmpeg -i "$input_file" -c:v vp9 -b:v 0 -crf "$quality" "$output_file"
    
    echo "✅ Converted with CRF $quality: $output_file"
}

Now you can specify quality:

towebm my_animation.gif 35  # Higher quality (lower CRF)