Skip to content

Instantly share code, notes, and snippets.

@p-karanthaker
Created June 23, 2023 22:53
Show Gist options
  • Save p-karanthaker/590d7cbe30079b802b569bf6557eeeff to your computer and use it in GitHub Desktop.
Save p-karanthaker/590d7cbe30079b802b569bf6557eeeff to your computer and use it in GitHub Desktop.
Converts Naruto Kai .mkv episodes with separate subtitle streams to .mp4 with hardcoded subtitles. Chromecast doesn't like the subtitle streams and displays them with no spacing. The script uses ffmpeg with nvidia hardware acceleration.
#!/bin/bash
# Converts Naruto Kai .mkv episodes with separate subtitle streams to .mp4 with hardcoded subtitles.
# Chromecast doesn't like the subtitle streams and displays them with no spacing.
# The script uses ffmpeg with nvidia hardware acceleration.
#
# Recommend to dryrun first which will echo all commands that it will run. If you're happy with it,
# then run the script without the dryrun flag
# Usage: ./naruto-kai-sub-fix.sh /dir/to/search -dryrun
# Usage: ./naruto-kai-sub-fix.sh /dir/to/search
DIR=$1
find "$DIR" -type f -name "*.mkv" | while read filename; do
has_subtitles=$(ffprobe -i "$filename" 2>&1 >/dev/null | grep -E "Stream.+Subtitle")
if [[ -n "$has_subtitles" ]]; then
name=$(sed -r 's/(.*)\.mkv/\1/' <<<"$filename")
if [ -f "${name}.mp4" ]; then
echo "$name - already done"
continue
fi
echo "Starting $name"
if [[ "$2" == "-dryrun" ]]; then
echo ffmpeg -loglevel error -hwaccel_device 0 -hwaccel cuda -i "${name}.mkv" -c:v h264_nvenc -vf subtitles="'${name}.mkv'" "${name}.mp4" 2> >(tee errors.txt)
else
ffmpeg -loglevel error -hwaccel_device 0 -hwaccel cuda -i "${name}.mkv" -c:v h264_nvenc -vf subtitles="'${name}.mkv'" "${name}.mp4" 2> >(tee errors.txt)
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment