I have a video file on my local disk. I also have some subtitles (in HTML5's WebVTT format).
I want to create an HTML file to play that video with subtitles, all from the local filesystem.
Loading an external VTT file from the local filesystem fails due to same-origin policy.
Disable cross-origin protection in the browser.
While it works, it is troublesome and dangerous.
Try embedding the VTT file as a data:
URI inside <track src="">
.
Unfortunately, I still got issues related to cross-origin, so I abandoned this idea.
Try embedding the VTT file inside the HTML file, generating a Blob URL, and adding a new <track src="">
to the video.
Unfortunately, it did not work for me.
Try embedding the SRT file inside the HTML file and use JavaScript code to dynamically parse and display the subtitles.
That was my previous attempt, I used a highly modified version of VideoSub. Still, this solution felt a bit bloated, specially because it runs JavaScript code while the video is playing. In addition, it was insecure, because it added the subtitle text directly through innerHTML
.
Try embedding the VTT file inside the HTML file and dynamically create a new Text Track when the document loads.
This is the solution implemented in this Gist. It is relatively small (the bigger portion is the VTT parser), and the JavaScript code runs only once (when the document is loading).
The text style can be tweaked using video::cue
selector. Styling can be refined, for instance, we can set the style of underlined subtitles through video::cue(u)
selector.
In my opinion, this is the best solution so far.