Skip to content

Instantly share code, notes, and snippets.

@vtta
Last active August 16, 2019 17:45
Show Gist options
  • Save vtta/71dfd0212dedfc297cd3cac90fa7b021 to your computer and use it in GitHub Desktop.
Save vtta/71dfd0212dedfc297cd3cac90fa7b021 to your computer and use it in GitHub Desktop.
A script to encode video using ffmpeg and x265 encoder
#!/bin/bash
# need to install ffmpeg with x265 enabled, on macOS:
# sudo port install ffmpeg +nonfree
# or
# brew install ffmpeg --HEAD --with-fdk-aac --with-sdl2 --with-freetype \
# --with-libass --with-libbluray --with-libvorbis --with-libvpx \
# --with-opus --with-webp --with-x265
encode() {
ffmpeg -i "$1" \
-map 0:v \
-map 0:a \
-map 0:s \
-c:v libx265 -preset medium \
-c:a copy \
-c:s ass \
-tag:v hvc1 \
-map_metadata 0:g \
"$2"
}
process_folder() {
SRCDIR="$1"
OUTDIR="$1".x265
mkdir -p "$OUTDIR"
find "$SRCDIR" -type f -print0 | while IFS= read -r -d '' FILE; do
FULLNAME=$(basename "$FILE")
FILENAME="${FULLNAME%.*}"
# EXTNAME="${FULLNAME##*.}"
# read line </dev/tty # waite for enter
echo "Processing $FULLNAME"
OUTFILE="$OUTDIR/$FILENAME".mkv
encode "$FILE" "$OUTFILE" \
&& du -h "$OUTFILE" \
|| echo "encoding failed"
done
}
main() {
for INPUT in "$@" ; do
test -d "$INPUT" && process_folder "$INPUT"
test -f "$INPUT" && encode "$INPUT" "$INPUT".x265.mkv
done
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment