0

I'm new to JS and Node.js and I'm working on a personnal project to translate webVTT subtitle files using Azure Translator API - To do that, I use the node-webvtt npm package to parse / compile the webVTT file. The parsing operation provides a JSON object that contains an array of thousands of cues that looks like that :

[
      {
         "identifier":"",
         "start":60,
         "end":61,
         "text":"My text to be translated #1",
         "styles":""
      },
      {
         "identifier":"",
         "start":110,
         "end":111,
         "text":"My text to be translated #2",
         "styles":""
      }
]

To translate the "text" property, I use the code sample provide by Microsoft on GitHub and applied the following changes to the translateText function:

  • I create a promise that returns a "cue" object (and not only the translated text)
  • I take a "cue" object as input and the language to be translated to

Then I followed the code provided here, that uses a combination of Promise.all and item.map to translate all the cues text via my async translateText function

Here is the code of my index.js - It works but I don't really like this code and I'm sure it can be optimized and look nicer.

require('dotenv').config();
const { readFileSync, writeFileSync } = require('fs');
const { parse, compile }  = require('node-webvtt');
const { translateText } = require('./translate.js');

const inputStr = readFileSync('./subtitles.vtt', 'utf8');
const webVTT = parse(inputStr, { meta: true, strict : true });

const MSTranslateAsync = async (cue) => {
  return translateText(cue, 'fr')
}

const mapAsync = async (vttCues) => {
  return Promise.all(vttCues.map(cue => MSTranslateAsync(cue)))
}
    
mapAsync(webVTT.cues)
  .then(() => {
    outStr = compile(webVTT);
    writeFileSync('./translated_fr.vtt', outStr, 'utf8');
  });

e.g. I'm looking for a way to use only Promises and get your advices to optimize this code

mapAsync(webVTT.cues)
      .then(compile(webVTT))
      .then((data) => writeFileSync('./translated_fr.vtt', data, 'utf8'));
timoleo23
  • 11
  • 2
  • convert async await function to promises – Sagar Oct 05 '20 at 08:40
  • And is there any issue you are facing in this one ? – Sagar Oct 05 '20 at 08:40
  • no specific issue with the code above except that I don't really like the mix between async / promise - I wanted to get something clean like that https://www.npmjs.com/package/subtitle fs.createReadStream('./my-subtitles.srt') .pipe(parse()) .pipe(resync(-100)) .pipe(stringify({ format: 'WebVTT' })) .pipe(fs.createWriteStream('./my-subtitles.vtt')) – timoleo23 Oct 06 '20 at 11:38

1 Answers1

0
// not required but I'm using async version of fs in my projects
const fsAsync = require('fs').promises;
//
const compileAndWrite = async ()=>{
    await mapAsync(webVTT.cues);
    let outStr = compile(webVTT); //let outStr = await compile(webVTT);
    await fsAsync.writeFile('./translated_fr.vtt', outStr, 'utf8'); // ou use writeFileSync() 
};

compileAndWrite();