Skip to content

Instantly share code, notes, and snippets.

@donal56
Created January 12, 2023 20:30
Show Gist options
  • Save donal56/59b5d076742e64f6d59bc6f7af654390 to your computer and use it in GitHub Desktop.
Save donal56/59b5d076742e64f6d59bc6f7af654390 to your computer and use it in GitHub Desktop.
(agumation-utilities) Toma la salida de whisperAI y convierte la salida en SRT válido, a su vez traduce los subtitulos en español. Es necesario agregar las credenciales del sitio junto con la api key de deepl en properties.js.
const fs = require('fs');
const path = require('path');
const deepl = require('deepl-node');
const properties = require('./properties.js');
// Datos de DeepL
const apiKey = properties.deepl.apiKey;
const translator = new deepl.Translator(apiKey);
const targetLanguage = "ES";
const subsFolder = "./subs";
const regex = /\[(\d+):(\d+).(\d+) --> (\d+):(\d+).(\d+)\](.*)/gm;
const files = fs.opendirSync(subsFolder);
let currentFile = null;
while((currentFile = files.readSync()) !== null) {
const currenFilePath = subsFolder + "/" + currentFile.name;
normalizeToSrt(currenFilePath, "EN");
}
files.closeSync();
async function normalizeToSrt(filePath, language) {
const fileData = fs.readFileSync(filePath, 'utf-8');
let newFileData = "";
let matches = null;
let counter = 1;
while((matches = regex.exec(fileData)) !== null) {
if (matches.index === regex.lastIndex) {
regex.lastIndex++;
}
if(matches.length != 8 || matches[1] >= 60 || matches[4] >= 60) {
console.error(`El archivo ${filePath} no cumple con el formato esperado`);
continue;
}
newFileData += counter.toString() + "\n";
newFileData += "00:" + matches[1] + ":" + matches[2] + "," + matches[3];
newFileData += " --> ";
newFileData += "00:" + matches[4] + ":" + matches[5] + "," + matches[6];
newFileData += "\n";
newFileData += matches[7].trim() + "\n\n";
counter++;
}
let extension = filePath.split('.').pop();
let remanentPath = filePath.substring(0, filePath.length - 1 - extension.length);
let newPath = null;
newPath = remanentPath + " [" + language + "]" + ".srt";
fs.writeFileSync(newPath, newFileData);
console.log(`${filePath} normalizado a SRT[${language}]`);
const translation = await translator.translateText(newFileData, language, targetLanguage);
newPath = remanentPath + " [" + targetLanguage + "]" + ".srt";
newFileData = translation.text;
fs.writeFileSync(newPath, newFileData);
console.log(`${filePath} normalizado a SRT[${targetLanguage}]`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment