Skip to content

Instantly share code, notes, and snippets.

@carlos-algms
Last active February 16, 2021 15:40
Show Gist options
  • Save carlos-algms/a55c409ac2d48bd9a959dd9dbd6ad730 to your computer and use it in GitHub Desktop.
Save carlos-algms/a55c409ac2d48bd9a959dd9dbd6ad730 to your computer and use it in GitHub Desktop.
Convert video to webm format using ffmpeg

Convert video to webm format using ffmpeg

Docs: https://trac.ffmpeg.org/wiki/Encode/VP9

INPUT="video.mov"
OUTPUT="converted-video.webm"
CPUS=4                # Adjust the number of cores your CPU have
FRAMERATE=15          # Adjust for your needs
VARIABLE_BITRATE=100K # Adjust for your needs, can be represented as 1M, 1000k, etc.
MAX_BITRATE=200k      # adjust for your needs
WIDTH=800
HEIGHT=-1             # setting width or height as -1 means to keep aspect ratio

# We are going to use 2-pass as suggested by the docs

# First pass is super fast and will generate a *.log file to help the coded better convert the video
ffmpeg -i "$INPUT" \
  -threads $CPUS -cpu-used $CPUS \
  -c:v libvpx-vp9 \
  -r $FRAMERATE \
  -b:v $VARIABLE_BITRATE \
  -maxrate $MAX_BITRATE \
  -vf scale=${WIDTH}:${HEIGHT} \
  -pass 1 -an -f null \
  /dev/null

# Second pass will be slower, and will actually convert the video using the log from first step
ffmpeg -i "$INPUT" \
  -threads $CPUS -cpu-used $CPUS -deadline realtime \
  -c:v libvpx-vp9 \
  -r $FRAMERATE \
  -b:v $VARIABLE_BITRATE \
  -maxrate $MAX_BITRATE \
  -vf scale=${WIDTH}:${HEIGHT} \
  -pass 2 -y \
  "$OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment