Skip to content

Instantly share code, notes, and snippets.

@lnikell
Created June 28, 2022 20:36
Show Gist options
  • Save lnikell/7a241aa2fe371170f0fb4a9fb92549ac to your computer and use it in GitHub Desktop.
Save lnikell/7a241aa2fe371170f0fb4a9fb92549ac to your computer and use it in GitHub Desktop.
ffmpeg-web-optimized-vp9-h265
# Script accepts 5 parameters: input file, output folder, filename, -crf and number of pixels in width for scale options
# Validate that all parameters are passed
if [ $# -ne 5 ]; then
echo "Example usage: ffmpeg_crf.sh input.mp4 output-folder output-filename 3840 32"
exit 1
fi
# Validate that 5th parameter is a number within a range from 0 to 51
if [ $5 -lt 0 ] || [ $5 -gt 51 ]; then
echo "CRF value must be a number between 0 and 51"
exit 1
fi
# Validate that 3rd parameter is an even number
if [ $(($4 % 2)) -ne 0 ]; then
echo "Width must be an even number"
exit 1
fi
# Validate that 1st parameter is a file
if [ ! -f $1 ]; then
echo "Input file does not exist"
exit 1
fi
# Validate that 2nd parameter is a folder
if [ ! -d $2 ]; then
echo "Output folder does not exist"
exit 1
fi
# Delete slash from the end of the folder path if it exists
if [ "${2: -1}" == "/" ]; then
folder=$(echo $2 | sed 's/\/$//')
else
folder=$2
fi
# CRF * 63 / 51 = CRF for VP9
crf=$(($5 * 63 / 51))
printf "CRF for VP9: %d\n" $crf
printf "CRF for H265: %d\n" $5
# Encoding to VP9
# Example command ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 40 -vf scale=3840:-1 -deadline best -an output.webm
ffmpeg -i $1 -c:v libvpx-vp9 -crf $crf -vf scale=$4:-1 -deadline best -an $folder/$3.webm
# Encoding to H265
# Example command ffmpeg -i input.mp4 -c:v libx265 -crf 32 -vf scale=3840:-1 -preset veryslow -tag:v hvc1 -movflags faststart -an output.mp4
ffmpeg -i $1 -c:v libx265 -crf $5 -vf scale=$4:-1 -preset veryslow -tag:v hvc1 -movflags faststart -an $folder/$3.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment