Skip to content

Commit

Permalink
Allow timestamps of srt and vtt files with a 1-digit hour
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni Räsänen committed Aug 10, 2020
1 parent 072cb6e commit 360b157
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/parseTimestamps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import toMS from './toMS'
* @type {RegExp}
*/

const RE = /^((?:\d{2,}:)?\d{2}:\d{2}[,.]\d{3}) --> ((?:\d{2,}:)?\d{2}:\d{2}[,.]\d{3})(?: (.*))?$/
const RE = /^((?:\d{1,}:)?\d{2}:\d{2}[,.]\d{3}) --> ((?:\d{1,}:)?\d{2}:\d{2}[,.]\d{3})(?: (.*))?$/

/**
* parseTimestamps
Expand Down
2 changes: 1 addition & 1 deletion lib/toMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function toMS (timestamp) {
return timestamp
}

const match = timestamp.match(/^(?:(\d{2,}):)?(\d{2}):(\d{2})[,.](\d{3})$/)
const match = timestamp.match(/^(?:(\d{1,}):)?(\d{2}):(\d{2})[,.](\d{3})$/)

if (!match) {
throw new Error('Invalid SRT or VTT time format: "' + timestamp + '"')
Expand Down
21 changes: 21 additions & 0 deletions test/parseTimestamps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ test('parseTimestamps with SRT input', t => {
start: 45296789,
end: 357414321
})

check('0:59:50,050 --> 1:00:20,070', {
start: 3590050,
end: 3620070
})

check('0:00:20,000 --> 0:00:24,400', {
start: 20000,
end: 24400
})
})

test('parseTimestamps with VTT input and short formats', t => {
Expand All @@ -47,6 +57,11 @@ test('parseTimestamps with VTT input and short formats', t => {
start: 3590050,
end: 3620070
})

check('0:59:50.050 --> 1:00:20.070', {
start: 3590050,
end: 3620070
})
})

test('parseTimestamps with VTT settings', t => {
Expand All @@ -63,4 +78,10 @@ test('parseTimestamps with VTT settings', t => {
end: 24400,
settings: 'position:45%,line-right align:center size:35%'
})

check('0:00:20.000 --> 0:00:24.400 position:45%,line-right align:center size:35%', {
start: 20000,
end: 24400,
settings: 'position:45%,line-right align:center size:35%'
})
})

0 comments on commit 360b157

Please sign in to comment.