Created
August 31, 2019 02:24
-
-
Save quannt/7f70b08222e1eb06f982910f5527cfeb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//requiring path and fs modules | |
const path = require('path'); | |
const fs = require('fs'); | |
const readline = require('readline'); | |
//joining path of directory | |
const directoryPath = path.join(__dirname, 'srt'); | |
const outputPath = path.join(__dirname, 'dfxp'); | |
//passsing directoryPath and callback function | |
fs.readdir(directoryPath, { | |
encoding: 'utf8', | |
withFileTypes: true | |
}, function (err, files) { | |
//handling error | |
if (err) { | |
return console.log('Unable to scan directory: ' + err); | |
} | |
//listing all files using forEach | |
files.forEach(function (file) { | |
if (isValidSubFile(file)) { | |
handleReadFile(path.join(directoryPath, file.name), file.name) | |
} | |
}); | |
}); | |
const isValidSubFile = (dirent) => { | |
if (dirent.isFile() && dirent.name.endsWith('.srt')) { | |
return true | |
} | |
return false | |
} | |
const handleReadFile = (filePath, fileName) => { | |
let currentIndex = 0; | |
let newParagraph = { | |
timeStampStart: null, | |
timeStampEnd: null, | |
text: null | |
} | |
let paragraphs = [] | |
const rl = readline.createInterface({ | |
input: fs.createReadStream(filePath) | |
}); | |
rl.on('line', (line) => { | |
if (!line) { | |
console.log(newParagraph) | |
paragraphs.push(JSON.parse(JSON.stringify(newParagraph))) | |
currentIndex = 0; | |
newParagraph.timeStampStart = null; | |
newParagraph.timeStampEnd = null; | |
newParagraph.text = null | |
return | |
} | |
currentIndex = currentIndex + 1; | |
if (currentIndex === 2) { | |
let lines = line.split('-->') | |
newParagraph.timeStampStart = lines[0].trim().replace(',', '.') | |
newParagraph.timeStampEnd = lines[1].trim().replace(',', '.') | |
} else if (currentIndex > 2) { | |
if (newParagraph.text) { | |
newParagraph.text = `${newParagraph.text}<br />${line}` | |
} else { | |
newParagraph.text = `${line}` | |
} | |
} | |
}); | |
rl.on('close', () => {f | |
let subs = ''; | |
console.log(paragraphs) | |
paragraphs.forEach((item) => { | |
console.log(item) | |
subs = subs + `<p begin="${item.timeStampStart}" end="${item.timeStampEnd}"> | |
${item.text} | |
</p>` | |
}) | |
let newContent = `<?xml version="1.0" encoding="UTF-8"?> | |
<tt xml:lang='en' xmlns='http://www.w3.org/2006/10/ttaf1' xmlns:tts='http://www.w3.org/2006/10/ttaf1#style'> | |
<head></head> | |
<body> | |
<div xml:id="captions"> | |
${subs} | |
</div> | |
</body> | |
</tt> | |
` | |
fs.writeFile(path.join(outputPath, `${fileName}.dfxp`), newContent, (err) => { | |
if (err) console.log(err); | |
console.log(`Successfully converted ${fileName}`); | |
}); | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment