Skip to content

Instantly share code, notes, and snippets.

@LukeL99
Created December 29, 2015 21:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukeL99/7bad762cc90ee0fc181d to your computer and use it in GitHub Desktop.
Save LukeL99/7bad762cc90ee0fc181d to your computer and use it in GitHub Desktop.
Compress video output for HTML5 background video
#!/bin/bash
# Make sure ffmpeg is installed with the following flags
# brew install ffmpeg --with-libvpx
#
# Take source video, convert to raw grayscale (-pix_fmt gray),
# remove audio track (-an), darken it by 50% (-vf "lutyuv=y=val*.5"),
# and output to an avi container for maximum compatibility.
# Pipe all that to stdout
# (ffmpeg -i - \) take input rawvideo track, and...
#
# (-c:v libvpx-vp9 -vf format=yuv420p -qmin 0 -qmax 50 -b:v 1.25M rw-vp9.webm \)
# convert it to VP9 webm, which is the most compressed and least compatible
# with an average bitrate of 1.25 mbs (-b:v 1.25M),
# and set a constraint on the maximum quantizer to 50/63 (sweet numbering system),
# which sets the maximum amount of crappification the encoder can do to the video
#
# (-c:v libvpx -vf format=yuv420p -qmin 0 -qmax 50 -b:v 1.5M rw.webm)
# convert it to VP8 webm, which is the 2nd most compressed
# and 2nd least compatible, with a higher bitrate to compensate for the less
# advanced codec
#
# (-c:v libx264 -vf format=yuv420p -qmin 0 -qmax 50 -b:v 2M rw.mp4)
# convert it to mp4 x264, which is the least compressed but most compatible
# with a higher bitrate to compensate for the less advanced codec
ffmpeg -i rw.mov -c:v rawvideo -pix_fmt gray -an -vf "lutyuv=y=val*.5" -f avi - | \
ffmpeg -i - \
-c:v libvpx-vp9 -vf format=yuv420p -qmin 0 -qmax 50 -b:v 1.25M rw-vp9.webm \
-c:v libvpx -vf format=yuv420p -qmin 0 -qmax 50 -b:v 1.5M rw.webm \
-c:v libx264 -vf format=yuv420p -qmin 0 -qmax 50 -b:v 2M rw.mp4
# Example Video Tag
# <video controls autoplay loop>
# <source src="rw-vp9.webm" type="video/webm;codecs=vp9">
# <source src="rw.webm" type="video/webm;codecs=vp8">
# <source src="rw.mp4" type="video/mp4">
# </video>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment