Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Fix WebVTT parsing when frame numbering is missing
Browse files Browse the repository at this point in the history
As per <https://en.wikipedia.org/wiki/WebVTT>:

    The frame numbering/identification preceding
    the timecode is optional
  • Loading branch information
gorhill committed Jan 29, 2020
1 parent 3f09e31 commit 0c062df
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion manifest-chromium.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"activeTab",
"<all_urls>"
],
"version": "1.0.0"
"version": "1.0.1"
}
2 changes: 1 addition & 1 deletion manifest-firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"activeTab",
"<all_urls>"
],
"version": "1.0.0"
"version": "1.0.1"
}
16 changes: 11 additions & 5 deletions src/assign-captions.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,26 @@
video.pause();
};

// https://en.wikipedia.org/wiki/WebVTT#Main_differences_from_SubRip

const srtParse = function(raw) {
const vtt = [ 'WEBVTT', '' ];
const entries = raw.replace(/(\r\n|\n\r)/g, '\n')
.trim()
.split(/\s*\n\n+\s*/);
for ( const entry of entries ) {
let i = 0;
const lines = entry.split(/\s*\n\s*/);
if ( /^\d+$/.test(lines[0]) === false ) { continue; }
const times = /(\S+)\s+--+>\s+(\S+)/.exec(lines[1]);
// "The frame numbering/identification preceding the
// "timecode is optional"
if ( /^\d+$/.test(lines[i+0]) ) {
i += 1;
}
const times = /^(\S+)\s+--+>\s+(\S+)/.exec(lines[i+0]);
if ( times === null ) { continue; }
vtt.push(
lines[0],
times[1].replace(/,/g, '.') + ' --> ' + times[2].replace(/,/g, '.'),
lines.slice(2).join('\n'),
lines.slice(i+1).join('\n'),
''
);
}
Expand All @@ -136,7 +142,7 @@
track.setAttribute('label', 'CCaptioner');
track.setAttribute('srclang', 'zz');
track.setAttribute('src', blobURL);
track.setAttribute('data-vtt-delta', '0');
track.setAttribute('data-vtt-offset', '0');
track.setAttribute('data-vtt', vtt);
video.appendChild(track);
track.track.mode = 'showing';
Expand Down
5 changes: 3 additions & 2 deletions src/offset-captions.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,20 @@

for ( let i = 0; i < entries.length; i++ ) {
const lines = entries[i].split(/\n/);
const times = /(\S+) --> (\S+)/.exec(lines[1]);
const times = /(\S+) --> (\S+)/.exec(lines[0]);
if ( times === null ) { continue; }
const t0 = timeShift(times[1]);
if ( t0 === '' ) { return; }
const t1 = timeShift(times[2]);
lines[1] = `${t0} --> ${t1}`;
lines[0] = `${t0} --> ${t1}`;
entries[i] = lines.join('\n');
}
vtt = entries.join('\n\n');

const blob = new Blob([ vtt ], { type: 'text/vtt' });
const blobURL = URL.createObjectURL(blob);
const newTrack = oldTrack.cloneNode(false);
newTrack.setAttribute('data-vtt-offset', timeDelta);
newTrack.setAttribute('src', blobURL);
oldTrack.replaceWith(newTrack);
newTrack.track.mode = 'showing';
Expand Down

0 comments on commit 0c062df

Please sign in to comment.