-
-
Save bastimeyer/0f6670576f4da5ec80d54e8d29d06c69 to your computer and use it in GitHub Desktop.
WebM (vp8 / no audio) bash script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
INPUT="${1}" | |
OUTPUT="${2:-video.webm}" | |
MAXSIZE="${3:-3}" | |
START="${4}" | |
LENGTH="${5}" | |
if [ $# -eq 0 ]; then | |
echo "Create vp8/webm video files without audio:" | |
echo "webm input [output [size [start length [...]]]]" | |
echo "" | |
echo "The existing output file will always be overwritten (default: video.webm)" | |
echo "size is set in MiB (25 KiB will be subtracted) (default: 3)" | |
echo "start (in secs or timestamp) and length (in secs) need to be set at the same time (default: unset)" | |
echo "accepts additional ffmpeg parameters that can be used for scaling, cropping, etc." | |
exit 0 | |
fi | |
if [ ! -f "${INPUT}" ]; then | |
>&2 echo "Invalid input file" | |
exit 1 | |
fi | |
for dep in ffmpeg ffprobe jq bc mktemp; do | |
if ! which "${dep}" >/dev/null; then | |
>&2 echo "Missing dependency ${dep}" | |
exit 1 | |
fi | |
done | |
set -e | |
THRESHOLD=0.025 | |
THREADS=$(grep -c '^processor' /proc/cpuinfo) | |
if [ -z "${START}" ] || [ -z "${LENGTH}" ]; then | |
START="0" | |
LENGTH="$(ffprobe -v error -show_format -show_streams -print_format json "${INPUT}" | jq ".format.duration | tonumber")" | |
fi | |
BITRATE=$(bc -l <<< "scale=0; ( ${MAXSIZE} - ${THRESHOLD} ) * 8 * 1024 * 1024 / ${LENGTH}") | |
PASSFILE="$(mktemp)" | |
( | |
set -x | |
ffmpeg -hide_banner \ | |
-i "${INPUT}" \ | |
-ss "${START}" \ | |
-t "${LENGTH}" \ | |
-an \ | |
-c:v libvpx \ | |
-b:v "${BITRATE}" \ | |
"${@:6}" \ | |
-quality best \ | |
-threads "${THREADS}" \ | |
-pass 1 \ | |
-passlogfile "${PASSFILE}" \ | |
-f webm \ | |
-y \ | |
/dev/null | |
ffmpeg -hide_banner \ | |
-i "${INPUT}" \ | |
-ss "${START}" \ | |
-t "${LENGTH}" \ | |
-an \ | |
-c:v libvpx \ | |
-b:v "${BITRATE}" \ | |
"${@:6}" \ | |
-quality best \ | |
-threads "${THREADS}" \ | |
-pass 2 \ | |
-passlogfile "${PASSFILE}" \ | |
-f webm \ | |
-y \ | |
"${OUTPUT}" | |
) | |
rm "${PASSFILE}-0.log" 2>/dev/null || true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment