-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support srt subtitle format (#743)
* feat: support srt subtitle format * chore: add example and usage monitoring * chore: esm examples * chore: esm example * chore: examples * feat: support srt subtitle format
- Loading branch information
Showing
10 changed files
with
149 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import srtTextTracks from './srt-text-tracks'; | ||
|
||
export default async function srtTextTracksPlugin(config) { | ||
const player = this; | ||
try { | ||
player.ready(() => srtTextTracks(config, player)); | ||
} catch (error) { | ||
console.error('Failed to load plugin:', error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import srtParser2 from 'srt-parser-2'; | ||
|
||
function srtTextTracks(config, player) { | ||
// Load the SRT file and convert it to WebVTT | ||
const initSRT = async () => { | ||
let srtResponse; | ||
if (config.src) { | ||
try { | ||
srtResponse = await fetch(config.src); | ||
if (!srtResponse.ok) { | ||
throw new Error(`Failed fetching from ${config.src} with status code ${srtResponse.status}`); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
if (!srtResponse.ok) return; | ||
|
||
const srtData = await srtResponse.text(); | ||
const webvttCues = srt2webvtt(srtData); // Get the array of cues | ||
|
||
const srtTrack = player.addRemoteTextTrack({ | ||
kind: config.kind || 'subtitles', | ||
label: config.label || 'Subtitles', | ||
srclang: config.srclang, | ||
default: config.default, | ||
mode: config.default ? 'showing' : 'disabled' | ||
}); | ||
|
||
// required for Safari to display the captions | ||
// https://github.com/videojs/video.js/issues/8519 | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
||
// Add the WebVTT data to the track | ||
webvttCues.forEach(cue => { | ||
if (cue) { | ||
srtTrack.track.addCue(new VTTCue(cue.startTime, cue.endTime, cue.text)); | ||
} | ||
}); | ||
}; | ||
|
||
player.one('loadedmetadata', () => { | ||
initSRT(); | ||
}); | ||
} | ||
|
||
// SRT parser | ||
const srt2webvtt = data => { | ||
const SRTParser = new srtParser2(); | ||
|
||
const cues = SRTParser.fromSrt(data); | ||
|
||
return cues.map(cue => ({ | ||
startTime: cue.startSeconds, | ||
endTime: cue.endSeconds, | ||
text: cue.text | ||
})); | ||
}; | ||
|
||
export default srtTextTracks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters