From 2de0f29bab007a5c73ceae33ebf5a7362142a110 Mon Sep 17 00:00:00 2001 From: Jagdish Chaudhary Date: Wed, 20 May 2020 12:03:50 +0530 Subject: [PATCH 1/3] fix rounding error in timestamp --- lib/compiler.js | 2 +- lib/parser.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index a781839..20cb898 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -156,7 +156,7 @@ function calculateSeconds (time) { } function calculateMs (time) { - return Math.floor((time % 1) * 1000); + return Math.floor((time % 1).toFixed(3) * 1000); } module.exports = { CompilerError, compile }; diff --git a/lib/parser.js b/lib/parser.js index ebc3316..4a70e59 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -184,10 +184,9 @@ function validTimestamp (timestamp) { function parseTimestamp (timestamp) { const matches = timestamp.match(TIMESTAMP_REGEXP); - - let secs = parseFloat(matches[3]); + let secs = parseFloat(matches[1] || 0) * 60 * 60; // hours secs += parseFloat(matches[2]) * 60; // mins - secs += parseFloat(matches[1] || 0) * 60 * 60; // hours + secs += parseFloat(matches[3]); // secs += parseFloat(matches[4]); return secs; } From 4f60c0f68ecfdf0d28b7100326657cfcaac35ede Mon Sep 17 00:00:00 2001 From: Jagdish Chaudhary Date: Wed, 20 May 2020 14:47:13 +0530 Subject: [PATCH 2/3] increase precision --- lib/compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compiler.js b/lib/compiler.js index 20cb898..7fba580 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -156,7 +156,7 @@ function calculateSeconds (time) { } function calculateMs (time) { - return Math.floor((time % 1).toFixed(3) * 1000); + return Math.floor((time % 1).toFixed(4) * 1000); } module.exports = { CompilerError, compile }; From 71303937e04432be11c3b62437f9238293595149 Mon Sep 17 00:00:00 2001 From: Jagdish Chaudhary Date: Wed, 20 May 2020 19:42:37 +0530 Subject: [PATCH 3/3] add test cases --- test/compiler.test.js | 44 +++++++++++++++++++++++++++++++++++++++++++ test/parser.test.js | 10 ++++++++++ 2 files changed, 54 insertions(+) diff --git a/test/compiler.test.js b/test/compiler.test.js index 66bbe31..4594329 100644 --- a/test/compiler.test.js +++ b/test/compiler.test.js @@ -271,6 +271,50 @@ Ta en kopp compile(input).should.equal(output); }); + it('should compile with accurate milliseconds', () => { + + const input = { + cues: [{ + end: 1199.539, + identifier: '1', + start: 1199.529, + styles: '', + text: 'Ta en kopp varmt te.\nDet är inte varmt.' + }, { + end: 1199.549, + identifier: '2', + start: 1199.539, + styles: '', + text: 'Har en kopp te.\nDet smakar som te.' + }, { + end: 1199.558, + identifier: '3', + start: 1199.549, + styles: '', + text: 'Ta en kopp' + }], + valid: true + }; + const output = `WEBVTT + +1 +00:19:59.529 --> 00:19:59.539 +Ta en kopp varmt te. +Det är inte varmt. + +2 +00:19:59.539 --> 00:19:59.549 +Har en kopp te. +Det smakar som te. + +3 +00:19:59.549 --> 00:19:59.558 +Ta en kopp +`; + + compile(input).should.equal(output); + }); + it('should round properly', () => { const input = { diff --git a/test/parser.test.js b/test/parser.test.js index d1dfc72..94dc426 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -413,4 +413,14 @@ Text Position: 5% result.errors.length.should.equal(1); result.errors[0].message.should.equal('Invalid cue timestamp (cue #14)'); }); + + it('should parse cue w/o round-off', () => { + const input = `WEBVTT + + 01:24:39.06 --> 01:24:40.060 +a`; + + parse(input).cues[0].start.should.equal(5079.06); + parse(input).cues[0].end.should.equal(5080.06); + }); });