Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SRT Captions/Subtitles in Videojs #5923

Closed
Anan5a opened this issue Apr 10, 2019 · 5 comments
Closed

Using SRT Captions/Subtitles in Videojs #5923

Anan5a opened this issue Apr 10, 2019 · 5 comments

Comments

@Anan5a
Copy link

Anan5a commented Apr 10, 2019

Description

I want to use SRT subtitles in my videos alongside WebVTT because SRT has a huge selection of subs in sites like subscene.com But it seems support for it was dropped since v4.12+ Additionally suggested BubblesJS library /repository seems dead. the provided link no longer work.

Steps to reproduce

Explain in detail the exact steps necessary to reproduce the issue.
Add a <track> element with srt source file inside <video>

Results

Expected

Have option for displaying captions

Actual

Nothing there

Error output

Additional Information

Please include any additional information necessary here. Including the following:

versions

videojs

7.3.0

browsers

All?

OSes

Android, Windows, Linux

plugins

NO PLUGIN

@mister-ben
Copy link
Contributor

The easiest option is probably just to convert srt to webvtt in advance.

You could convert to webvtt on the fly and load as a remote text track, along these lines.

var srtText = `1
00:00:00,500 --> 00:00:02,000
One caption

2
00:00:03,000 --> 00:00:05,000
Another caption`;
var srtRegex = /(.*\n)?(\d\d:\d\d:\d\d),(\d\d\d --> \d\d:\d\d:\d\d),(\d\d\d)/g;
var vttText = 'WEBVTT\n\n' + srtText.replace(srtRegex, '$1$2.$3.$4');
var vttBlob = new Blob([vttText], {type : 'text/vtt'});
var blobURL = URL.createObjectURL(vttBlob);
player.on('loadedmetadata', function() {
  this.addRemoteTextTrack({
    src: blobURL,
    srclang: 'en',
    label: 'english',
    kind: 'subtitles'
  }, true);
});

The regex is a bit simplistic and there will be edge cases it doesn't work with, there's more robust code to do the conversion part at https://github.com/silviapfeiffer/silviapfeiffer.github.io/blob/master/index.html

@Anan5a
Copy link
Author

Anan5a commented Apr 11, 2019

@mister-ben thanks. But is there any way to convert them on the fly when the video is playing?

@mister-ben
Copy link
Contributor

That's the idea of the js code there. Fetch the captions, convert to vtt and add to the player as a text track with the addRemoteTextTrack API.

@Anan5a
Copy link
Author

Anan5a commented Apr 11, 2019

@mister-ben very very thanks!
I'm now trying to write a function to do the job
But how can I load more than one subtitle using addRemoteTextTrack?
Also how can I sync the sub? The sub is out of sync by 0.5s after conversion

@Anan5a
Copy link
Author

Anan5a commented Apr 12, 2019

@mister-ben Thank you for the RegEx. Successfully implemented on the fly conversion
Screenshot_2019-04-12-15-25-51

@Anan5a Anan5a closed this as completed Apr 12, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants