Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

411
Vistas
How to read and parse srt Subtitle file with Javascript?

I'm trying to make a program that will translate subtiltes file from a given path. The programm is running inside electron - so I have access to the computer's files. The problem is I couldn't find explanation on how to read and parse srt file is it possible?

    function translateSubs(path, newPath){
      var srt = readFile(path)
      var translatedOutput = []
      srt.data.foreach(line => {
        line.text = translateToEnglishline.text()
      })
      parseFile(srt, newPath)
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

So this is how I did it:

var { default: srtParser2} = require('srt-parser-2');
var parser = new srtParser2()
const fs = require('fs');
//srt => json
fs.readFile(path,(err,data) =>{
  if (err) {
    console.error(err);
    return;
  }
  var subObj = parser.fromSrt(data.toString())
  console.log("lines in srt:", subObj.length)
  //json => srt
  outputSrt = ""
  subObj.forEach(item =>{
    translatedSub += "\n" + item.id + "\n"+ item.startTime + " --> " + item.endTime + "\n" + item.text + "\n" 
  })
  fs.writeFile(outputPath, outputSrt, (err) => {console.log(err)})
  console.log("wrote file into path:", outputPath)
})

Reading srt: First, we read the srt using FS, then we convert the output to string and read it with srt-parser-2 which gives us the srt in JSON

Creating Srt: we create new string which has this format:

1
00:00:11,544 --> 00:00:12,682
Hello

2
00:00:17,123 --> 00:00:19,345
There

3
00:00:30,123 --> 00:00:31,345
General Kenobi

then we write it to a file.

for more information you can see srt-parser-2 docs here: https://www.npmjs.com/package/srt-parser-2

about 4 years ago · Juan Pablo Isaza Denunciar

0

Looking at SRT File Format we can see that example of data inside file is:

1
00:05:00,400 --> 00:05:15,300
This is an example of
a subtitle.

2
00:05:16,400 --> 00:05:25,300
This is an example of
a subtitle - 2nd subtitle.

Since I see you want to translate text, then all you need to do is get text part, translate it and overwrite it.

There are a lot of ways of doing this but how I would do it is by loading file into string[] array. Then loop through it, separate on blank line and parse that into separate objects. Now I have object that look like this

class SubItem
{
    string[] lines
}

Third part is going through each SubItem, translate every string in lines expect first two (first is ID, second time frame).

Now I have array of translated SubItems. Put them back into files with separator of blank line and you are done.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda