Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs')
- const path_in = './<original>.srt'
- const path_out = './<output>.srt'
- const input_encoding = 'latin1'
- const output_encoding = 'latin1'
- const src = fs.readFileSync(path_in).toString(input_encoding)
- const parseTime = (time) => {
- const [h, m, s] = time
- .replace(',', '.')
- .split(':')
- .map(Number)
- return (h*60 + m)*60 + s
- }
- const toTime = (parsed) => {
- const s = parsed % 60
- parsed = Math.round((parsed - s)/60)
- const m = parsed % 60
- const h = Math.round((parsed - m)/60)
- return `${
- h.toString().padStart(2,0)
- }:${
- m.toString().padStart(2,0)
- }:${
- s.toFixed(3).padStart(6,0).replace('.', ',')
- }`
- }
- // Calculate speed change and displacement
- // const cur_t0 = parseTime('00:02:03,924')
- // const cur_t1 = parseTime('00:44:42,015')
- // const fix_t0 = parseTime('00:02:03,924')
- // const fix_t1 = parseTime('00:44:42,015')
- // const mul = (fix_t1 - fix_t0)/(cur_t1 - cur_t0)
- // const sum = fix_t0 - cur_t0*mul
- // Define displacement with no speed change
- const mul = 1
- const sum = 6.5
- const res = src
- .replace(/\r/g, '')
- .trim()
- .split(/\n\n/)
- .map(sub => {
- let [, n, a, b, text ] = sub
- .trim()
- .match(/^(\d+)\s+([^\s]+) --> ([^\s]+)\s+((.|\s)*)$/)
- a = toTime(parseTime(a)*mul + sum)
- b = toTime(parseTime(b)*mul + sum)
- return `${n}\n${a} --> ${b}\n${text}\n\n`
- })
- .join('')
- .replace(/\n/g, '\r\n')
- fs.writeFileSync(path_out, Buffer.from(res, output_encoding))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement