2

What algorithm does a video player use to find the subtitles to show at any given time?

I'm building a video player that displays some notes on different times over a video. Each note has a starting time and an ending time, just like subtitles.

I'm having issues coming up with an algorithm to find the all the "notes" while the video is playing, obviously I want it to be as efficient as possible, and I figured that subtitles work basically the same(If they are ordered by the "start time").

I'm building it with Javascript since the player is intended to work in the browser, and this makes it even more important for it to be performant.

I thought about using a Binary Tree, but I don't think it will work, because a Binary Tree compares against one parameter, and I'm not sure it will be the best option after I modify it.

Robert Harvey
  • 200,592
Samuel E.
  • 233

3 Answers3

6

Unless your subtitles overlap, the algorithm is pretty simple:

  • keep the subtitles in an ordered list.
  • in your event loop, determine the current time
  • if it is larger than the end time of the current subtitle, unpost it
  • if it is larger than the start of the first subtitle in the list, post it and remove it from the list.

Do you expect anything more complicated in your subtitles than a simple sequence of successive items?

Kilian Foth
  • 110,899
4

Subtitles are ordered: the ones which appear later in the video are stored after the ones which appear earlier.

Therefore, use a simple cursor which points to the next subtitle to show.

In order to make the subtitles disappear (while ensuring that overlapping is possible), either set individual timeouts for every subtitle being shown, or use a second cursor. The first approach is much more straightforward in JavaScript.

0

It depends on the player you use, but the vast majority can handle this type of "timed" event through cue point, you should aim your thinking towards this path. Basically it just map a relative time event with the time of that video.