Skip to content

Instantly share code, notes, and snippets.

@hekmon
Created August 6, 2024 12:49
Show Gist options
  • Save hekmon/3077cf218fb4d57da9f35525612eb1f7 to your computer and use it in GitHub Desktop.
Save hekmon/3077cf218fb4d57da9f35525612eb1f7 to your computer and use it in GitHub Desktop.
Analyze SubStationAlpha subtitles (ASS) within a MKV file to extract the used font and add them back to the MKV file as attachments
#!/usr/bin/env bash
set -e
FONTSDIR="/fonts/dir/change/me"
attachment_exists() {
local mkv_file="$1"
local attachment_name="$2"
mkvmerge -i "$mkv_file" | grep -q -E "^Attachment ID [0-9]+: .+ file name '$attachment_name'$"
}
tmpdirsaferemove() {
test -z $1 && return 1
rm $1/*.ass
test -f $1/fonts && rm $1/fonts
rmdir $1
}
IFS=$'\n'
for file in $(ls "$1"); do
# Skip non mkv file
if ! [ -f "$1/$file" ]; then
echo "Skipping non regular file: $file"
continue
fi
clean=$(echo $file | sed -r 's/^.+\.mkv$//')
if [ -n "$clean" ]; then
echo "Skipping non mkv file: $file"
continue
fi
# Process
echo "Processing '$file'"
# Get ASS subtitles
SubStationSubsTracksIDs=$(mkvmerge -i "$1/$file" | grep -E '^Track ID [0-9]+: subtitles \(SubStationAlpha\)' | sed -r 's/^Track ID ([0-9]+):.+$/\1/')
nbSubs=$(echo "$SubStationSubsTracksIDs" | wc -l)
echo -e "\tFound $(echo "$SubStationSubsTracksIDs" | wc -l) ASS subtitle(s)."
if [ $nbSubs -eq 0 ]; then
continue
fi
# Extract them
tmpdir=$(mktemp -d)
cmd="mkvextract tracks \"${1}/${file}\""
for subID in $SubStationSubsTracksIDs; do
cmd+=" ${subID}:${tmpdir}/sub${subID}.ass"
done
eval $cmd
# Inspect them and extract needed fonts
fontsfile="$tmpdir/fonts"
for sub in $(ls "$tmpdir"); do
echo -e "\tAnalyzing $sub"
for font in $(grep --no-filename -E '^Style:' "$tmpdir/$sub" | sed -r 's/^Style: [^,]+,([^,]+),.+$/\1/' | sort | uniq); do
echo "$font" >> $fontsfile
done
done
fonts=$(cat $fontsfile | sort | uniq)
nbFonts=$(echo -e "$fonts" | wc -l)
if [ $nbFonts -eq 0 ]; then
echo "No fonts needed: skipping"
continue
fi
echo -e -n "\tNeed $nbFonts font(s):"
for font in $fonts; do
echo -n " $font"
done
echo
# Add fonts to file if not already present
cmd="mkvpropedit \"$1/$file\""
proceed=0
for font in $fonts; do
if [ "$font" == "Arial" ]; then
fontfile='arial.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
elif [ "$font" == "Comic Sans MS" ]; then
fontfile='comic.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
elif [ "$font" == "Times New Roman" ]; then
fontfile='times.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
elif [ "$font" == "Trebuchet MS" ]; then
fontfile='trebuc.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
elif [ "$font" == "Verdana" ]; then
fontfile='verdana.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
elif [ "$font" == "Courier New" ]; then
fontfile='cour.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
elif [ "$font" == "Rubik" ]; then
fontfile='rubik.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
elif [ "$font" == "Georgia" ]; then
fontfile='georgia.ttf'
if ! attachment_exists "$1/$file" "$font"; then
echo -e "\tFont '$font' is needed and is not attached: schedulling addition"
cmd+=" --attachment-name '$font' --attachment-description 'The $font font as a TrueType font' --add-attachment '${FONTSDIR}/$fontfile'"
proceed=1
else
echo -e "\tFont '$font' is already attached."
fi
else
tmpdirsaferemove $tmpdir
echo
echo -e "\tFont '$font' is not supported. Add the font to the fonts folder and update the script."
exit 1
fi
done
if [ $proceed -eq 1 ]; then
eval $cmd
else
echo -e "\tNothing to add"
fi
# Done, remove tmp dir
tmpdirsaferemove $tmpdir
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment