Skip to content

Instantly share code, notes, and snippets.

@kevinseelbach
Last active April 10, 2023 06:42
Show Gist options
  • Select an option

  • Save kevinseelbach/08c516adf89720b76c49e7ac781c79c1 to your computer and use it in GitHub Desktop.

Select an option

Save kevinseelbach/08c516adf89720b76c49e7ac781c79c1 to your computer and use it in GitHub Desktop.
h264 transcode using ffmpeg/vaapi for older hardware
#!/usr/bin/env bash
# Description: Convert all mkv files in a directory to mp4 using ffmpeg and vaapi
# set -x
convert_file() {
input_filename="$1"
output_filename="${input_filename%.mkv}.mp4"
python3 -c 'header=" BEGIN converting '"$input_filename "'"; print(f"{header:*^80}")'
# I was running this in a jellyfin LXC with GPU passthrough, you may need to change the path to the ffmpeg binary
/usr/lib/jellyfin-ffmpeg/ffmpeg \
-hide_banner \
-loglevel quiet \
-stats \
-hwaccel vaapi \
-hwaccel_device /dev/dri/renderD128 \
-hwaccel_output_format vaapi \
-i "$input_filename" \
-c:v h264_vaapi \
-b:v 4M \
-maxrate 8.5M \
-bufsize 8.5M \
-ac 2 \
-c:a libfdk_aac \
-vbr 3 "$output_filename"
# XXX: This is a hack to get around the fact that I can't get the above command to work as a script
# For some reason ffmpeg prints out a ton of stuff if I try to run as a script (looks like a memory dump), but it works find when I print the commands and run them manually
# Really not sure why this is happening, but I'm not going to spend any more time on it
# cmd="/usr/lib/jellyfin-ffmpeg/ffmpeg -hide_banner -loglevel quiet -stats -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i \"$input_filename\" -c:v h264_vaapi -b:v 4M -maxrate 8.5M -bufsize 8.5M -ac 2 -c:a libfdk_aac -vbr 3 \"$output_filename\""
# echo "$cmd"
}
# You can edit this find command to match your needs and filter out stuff if needed
find . -type f -iname '*.mkv' | while read line; do
start_time=$(date +%s.%N)
convert_file "$line"
end_time=$(date +%s.%N)
duration=$(echo "scale=3; ${end_time} - ${start_time}" | bc)
echo "Finished converting $line in $duration seconds"
done
# set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment