Skip to content

Instantly share code, notes, and snippets.

@jkcgs
Last active July 5, 2021 21:09
Show Gist options
  • Save jkcgs/6bf948dabdf71e1f078ed15b9e6b1671 to your computer and use it in GitHub Desktop.
Save jkcgs/6bf948dabdf71e1f078ed15b9e6b1671 to your computer and use it in GitHub Desktop.
Encodes MKV files from a folder using ffmpeg and NVENC H265 encoder
#!/bin/bash
set -e
# Reencodes a folder with mkv files, *not* using 10bit pixel format
# Usage: ./h265_nvenc.sh <path of mkv files>
DIR=$(realpath "$1")
FILES=`find "$DIR" -type f -not -path '*/\.*' -name "*.mkv"`
IFS='
'
encode() {
#ffmpeg -y -hwaccel cuvid -hwaccel_output_format cuda -i "$f" -map 0 -c copy -c:v hevc_nvenc "$output_temp"
ffmpeg -i "$1" -pass 1 \
-c:v hevc_nvenc -profile:v main -preset slow -rc vbr -multipass 2 \
-qmin 15 -qmax 20 -pix_fmt yuv420p -2pass 1 -c:a:0 copy -f mp4 -y /dev/null &&
ffmpeg -i "$1" -pass 2 -map 0 -c copy \
-c:v hevc_nvenc -profile:v main -preset slow -rc vbr -multipass 2 \
-qmin 15 -qmax 20 -pix_fmt yuv420p -2pass 1 -c:a:0 copy -y "$2"
}
echo "DIR: $DIR"
for f in $FILES
do
output_temp=$(mktemp "$DIR/.tmp.XXXXXX.mkv")
echo "Target: \"$f\", Temporal: \"$output_temp\""
encode "$f" "$output_temp"
mv "$output_temp" "$f"
rm -f "$output_temp"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment