Skip to content

Commit

Permalink
Merge pull request video-dev#1131 from flavioribeiro/master
Browse files Browse the repository at this point in the history
avoid showing repeated webvtt cues
  • Loading branch information
mangui authored May 3, 2017
2 parents 4f58d4b + bd2bb52 commit 17fcc09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/controller/timeline-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,15 @@ class TimelineController extends EventHandler {

// Parse the WebVTT file contents.
WebVTTParser.parse(payload, this.initPTS, vttCCs, frag.cc, function (cues) {
const currentTrack = textTracks[frag.trackId];
// Add cues and trigger event with success true.
cues.forEach(cue => {
textTracks[frag.trackId].addCue(cue);
// Sometimes there are cue overlaps on segmented vtts so the same
// cue can appear more than once in different vtt files.
// This avoid showing duplicated cues with same timecode and text.
if (!currentTrack.cues.getCueById(cue.id)) {
currentTrack.addCue(cue);
}
});
hls.trigger(Event.SUBTITLE_FRAG_PROCESSED, {success: true, frag: frag});
},
Expand Down
14 changes: 14 additions & 0 deletions src/utils/webvtt-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ const cueString2millis = function(timeString) {
return ts;
};

// From https://github.com/darkskyapp/string-hash
const hash = function(text) {
let hash = 5381;
let i = text.length;
while (i) {
hash = (hash * 33) ^ text.charCodeAt(--i);
}
return (hash >>> 0).toString();
};

const calculateOffset = function(vttCCs, cc, presentationTime) {
let currCC = vttCCs[cc];
let prevCC = vttCCs[currCC.prevCC];
Expand Down Expand Up @@ -81,6 +91,10 @@ const WebVTTParser = {
cue.startTime += cueOffset - localTime;
cue.endTime += cueOffset - localTime;

// Create a unique hash id for a cue based on start/end times and text.
// This helps timeline-controller to avoid showing repeated captions.
cue.id = hash(cue.startTime) + hash(cue.endTime) + hash(cue.text);

// Fix encoding of special characters. TODO: Test with all sorts of weird characters.
cue.text = decodeURIComponent(escape(cue.text));
if (cue.endTime > 0) {
Expand Down

0 comments on commit 17fcc09

Please sign in to comment.