Why do I do this to myself?
OK, so. Some background.
I’ve been working on a Jellyfin server as something of a side project, with the idea that it will probably teach me something useful that I can apply at some later point in time. I don’t NEED a Jellyfin server, since I usually serve all my content through iTunes, but I can’t deny that it is very handy to be able to just download an MKV file with embedded subtitles and play it without first running the files through handbrake to burn the subs into the video.
Also, files with non-burned subtitles mean that I can do stuff like fix typos in the subtitle script, mostly the ever-painful “YOUR WELCOME”, which I see as a sign that the person proofing the subtitles dropped out of school before hitting, roughly, third grade.
I make this sort of snide holier-than-thou comment despite being fully aware that my personal use (more properly, misuse) of, specifically, commas and (to a lesser extent) apostrophes is egregious enough to be considered a war crime in several of the more refined countries. And also Angola, for some reason.
Note: I’m not sure Angola is a country, but I’m too lazy to google it and for the sake of the joke let’s just roll with it. OK?
But I digress.
Moving on to the topic of today’s post, I have been downloading a bunch of anime to serve as content for the Jellyfin server – and one of the shows I downloaded was season 1 of The Apothecary Diaries. I am not enough of a weeb to call it “Kusuriya no Hitorigoto”, but you can think of it thusly if you prefer.
You can also call it The Maomao Is Just The Best Girl In The World Show, which is also fine! Honestly it would have been a better translation.
However, after downloading a batch of episodes, I noticed a couple of notes on the torrent:

I’m not certain that I would have noticed the missing fonts, personally – but since someone had been gracious enough to point out the errors AND provide links to download the fonts, I decided that I would correct the issues with the mkv files before copying them up to the Jellyfin server for future watching.
Naturally, this led me, as always, to ffmpeg, a lovely all-purpose Cultural Cat Girl tool for manipulating video files.
It also led me, after a couple of false starts on my own, to this superuser.com thread which explained some of the errors I was getting and gave me enough information to write a script to, well, attach fonts to mkv files.
The script is as follows, though I will caution you that it is not really the best thing in the world. For one thing, it doesn’t check whether the font file actually exists! It also doesn’t check that it IS a font file, and the mime type it assigns to the font file is always font/otf and never application/x-truetype-font even if that would be more appropriate. I am relying on the video player to make the right call here.
Furthermore, I didn’t test it very thoroughly at all. I ran it against a single file, and then a folder full of files, and tested the delete option, and these all seemed to work well enough for me.
#!/bin/bash
#
# add_font_to_mkv - adds a font as an attachment to an MKV file (Creates a new mkv file with .font_added.mkv extension)
# Usage: add_font_to_mkv all FONT_FILE - adds font to all mkv files in the folder
# add_font_to_mkv filename FONT_FILE - adds font to specified file
# add_font_to_mkv (filename or all) FONT_FILE delete - delete originals after update
#
if [ "$1" = "all" ];
then
echo "Batch Conversion"
for filename in [ *.mkv ]
do
filenamenoext=${filename%.*}
if [ -f "$filenamenoext".mkv ];
then
ffmpeg -i "$filenamenoext".mkv -map 0 -c copy -attach "$2" -metadata:s:t mimetype=font/otf "$filenamenoext".font_added.mkv
if [ "$3" = "delete" ];
then
rm "$filenamenoext".mkv
fi
fi
done
else
echo "Single File Conversion"
filename="$1"
filenamenoext=${filename%.*}
if [ -f "$filenamenoext".mkv ];
then
ffmpeg -i "$filenamenoext".mkv -map 0 -c copy -attach "$2" -metadata:s:t mimetype=font/otf "$filenamenoext".font_added.mkv
if [ "$3" = "delete" ];
then
rm "$filenamenoext".mkv
fi
fi
fi
Hopefully this is of use to someone at some point in the future.