Last active
March 31, 2016 21:42
-
-
Save vsemozhetbyt/6ff38bbc49536a449bab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************/ | |
'use strict'; | |
/******************************************************************************/ | |
const fs = require('fs'); | |
const path = require('path'); | |
const outCoding = 'utf8'; | |
process.argv.slice(2).forEach(processSRT); | |
/******************************************************************************/ | |
function processSRT(fpath) { | |
const inCoding = guessEncoding(fpath); | |
if (!inCoding) return; | |
const fext = path.extname(fpath); | |
if (fext != '.srt') { | |
console.error(`Wrong file: ${fpath}`); | |
return; | |
} | |
const fname = path.basename(fpath, fext); | |
let ftext = fs.readFileSync(fpath, inCoding) | |
.replace(/^\uFEFF/, '').replace(/(\r?\n)+$/, '\r\n'); | |
const subts = ftext.split(/(?:\r?\n){2,}/); | |
subts.forEach((s, i) => { | |
subts[i] = subts[i].split(/\r?\n/); | |
subts[i][1] = subts[i][1].split(' --> '); | |
subts[i][1][0] = timecode2ms(subts[i][1][0]); | |
subts[i][1][1] = timecode2ms(subts[i][1][1]); | |
}); | |
subts[0][1][0] = ms2timecode(subts[0][1][0]); | |
subts.forEach((s, i) => { | |
if (i < subts.length-1) { | |
let curEnd = subts[i][1][1]; | |
let nextStart = subts[i+1][1][0]; | |
const halfGap = Math.floor((nextStart - curEnd) / 2); | |
if (halfGap > 0) { | |
curEnd += (halfGap - 1); | |
nextStart -= halfGap; | |
} | |
subts[i][1][1] = ms2timecode(curEnd); | |
subts[i+1][1][0] = ms2timecode(nextStart); | |
} | |
}); | |
subts[subts.length-1][1][1] = ms2timecode(subts[subts.length-1][1][1]); | |
subts.forEach((s, i) => { | |
subts[i][1] = subts[i][1].join(' --> '); | |
subts[i] = subts[i].join('\r\n'); | |
}); | |
fs.writeFileSync(path.join(path.dirname(fpath), `${fname}.long${fext}`), | |
`\uFEFF${ subts.join('\r\n\r\n').replace(/(\r\n)+$/, '\r\n\r\n') }`, outCoding | |
); | |
//fs.renameSync(fpath, path.join(path.dirname(fpath), `${fname}.bak${fext}`)); | |
} | |
/******************************************************************************/ | |
function timecode2ms(timecode) { | |
const timeParts = timecode.split(/:|,/); | |
return ( | |
Number(timeParts[3]) + | |
Number(timeParts[2]) * 1000 + | |
Number(timeParts[1]) * 60000 + | |
Number(timeParts[0]) * 3600000); | |
} | |
/******************************************************************************/ | |
function ms2timecode(ms) { | |
return ( | |
Math.floor( ms / 3600000) + ':' + | |
Math.floor((ms % 3600000) / 60000) + ':' + | |
Math.floor((ms % 60000) / 1000) + ',' + | |
Math.floor( ms % 1000) | |
).replace(/\b(\d)\b/g, '0$1').replace(/,(\d\d)\b/, ',0$1'); | |
} | |
/******************************************************************************/ | |
function guessEncoding(fpath) { | |
try { | |
const fd = fs.openSync(fpath, 'r'); | |
const bf = new Buffer(2).fill(0); | |
fs.readSync(fd, bf, 0, 2, 0); | |
fs.closeSync(fd); | |
return bf[0] === 0xFF && bf[1] === 0xFE ? 'utf16le' : 'utf8'; | |
} catch(e) { | |
console.error(`Error: ${e.message}.`); | |
return null; | |
} | |
} | |
/******************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment