Skip to content

Instantly share code, notes, and snippets.

@mrcoles
Last active September 15, 2022 12:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrcoles/51530f33c16f7f54e8fc74f478d243a2 to your computer and use it in GitHub Desktop.
Save mrcoles/51530f33c16f7f54e8fc74f478d243a2 to your computer and use it in GitHub Desktop.
Script for compressing a video to .mp4 and .webm with ffmpeg
#!/usr/bin/env bash
# inspired by: https://gist.github.com/Vestride/278e13915894821e1d6f
INPUT=$1
OUTDIR="out/"
if [ -z "$INPUT" ]; then
echo "No file specified"
exit 1
fi
if [ ! -f "$INPUT" ]; then
echo "Input is not a file: $INPUT"
exit 1
fi
echo $INPUT
FILENAME=$(basename -- "$INPUT")
FILENAME="${FILENAME%.*}"
mkdir -p "$OUTDIR"
OUTFILE_NO_EXT="$OUTDIR$FILENAME"
set -e
date -u
# make mp4 file
#
# * -an = disable audio stream
# * -pix_fmt yuv420p = quicktime support
# * -profile:v baseline = maximum compatability but may increase bitrate a bit
#
ffmpeg -an -i "$INPUT" -vcodec libx264 -pix_fmt yuv420p -profile:v baseline -level 3 "$OUTFILE_NO_EXT.mp4"
date -u
# make webm file
#
ffmpeg -i "$INPUT" -vcodec libvpx-vp9 -b:v 1M -acodec libvorbis "$OUTFILE_NO_EXT.webm"
date -u
# # make super webm file!
# #
# ffmpeg -i "$INPUT" -c:v libvpx-vp9 -pass 1 -b:v 1000K -threads 1 -speed 4 \
# -tile-columns 0 -frame-parallel 0 -auto-alt-ref 1 -lag-in-frames 25 \
# -g 9999 -aq-mode 0 -an -f webm /dev/null
# ffmpeg -i "$INPUT" -c:v libvpx-vp9 -pass 2 -b:v 1000K -threads 1 -speed 0 \
# -tile-columns 0 -frame-parallel 0 -auto-alt-ref 1 -lag-in-frames 25 \
# -g 9999 -aq-mode 0 -c:a libopus -b:a 64k -f webm "$OUTFILE_NO_EXT.super.webm"
# date -u
set +e
@mrcoles
Copy link
Author

mrcoles commented Feb 28, 2019

🎥 Video compressor

Installation 💻

  1. Install Homebrew: https://brew.sh/ or if you already have it, run brew update from the Terminal App.

  2. Run: brew install ffmpeg

  3. Download this file compress-video.sh into a folder where you’ll run these scripts

Running the script 🏃‍♂️

Using the Terminal App, navigate to wherever this script lives and then run it, e.g., if it lives in a folder on your desktop called "compressor":

cd ~/Desktop/compressor/
bash compress-video.sh path/to/video/file.mov

Then wait for it to do its work (compression takes about ~10x the length of the video to complete). It will create files in the "out/" directory next to the script.

For extra webm compression (takes a lot longer), comment out the "make super webm file!" section and run that. ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment