I’ve created a Subtitle system for my “Offline” world. This was originally intended for me to watch a JP movie with English subtitles from YouTube.
Subtitle systems already exist but the few ones I know are designed for movie lists. This one is specifically for letting users pasting subtitles for arbitrary videos.
SubtitleManager.cs - Shared to twitter
Another user has built upon this implementation.
https://twitter.com/loljacklul/status/1510610024925741065
Here’s a postmortem of my experience while creating this subtitle prefab.
I’ve settled with the SRT subtitle format (SubRip), as it appeared to be widely supported. It can be easily parsed, and there’s no much layout concerns about it.
Subtitles are usually sorted by the time they appear, and this becomes an assumption in my Udon implementation: Parsed order is assumed to be the appearance order.
The only weird quirk about this format is that it’s not really specified what should happen when two ranges of timestamps overlap ( https://www.youtube.com/watch?v=FqJ21-GaYew ).
sx_2022-02-22_02-49-08_StTU4xK63P.mp4
The approach I’ve taken is that when two subtitles overlap, multiple subtitle text will be displayed with a line break in between, and the display order is always the timestamp order; not the “last appearance” order.
In addition, it is assumed that an overlapping subtitle A will not overlap again with a subtitle C if a subtitle B that started after A started, finishes before A finishes.
<aside> 🚫 This article was written in April 2022, half a year before the introduction of the native String Loader. Using a string loader is arguably the better solution today, but this section still may contain relevant information for other uses.
</aside>
I am making a video player world, not a movie world, so there is no predefined list of movies. I cannot preload subtitles, the user is free to use any SRT subtitle of their choosing.
Some movies or documentaries have very large subtitles, crossing the 300KB range in file size. However, Unity Text fields can only support around 16K characters, which makes copy pasting fail.
To work around this, I am using a TextMeshPro Text field, which does not have this limitation, letting the user paste absurd amounts of data. This causes several problems:
TextMeshPro text is not accessible via Udon. In order to copy the text, I had to wire up some Unity events to set the Text element of a Unity UI Text object. Then, I could access the UI Text in Udon.