最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

electron - How to read and parse srt Subtitle file with Javascript? - Stack Overflow

programmeradmin5浏览0评论

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 puter'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)
    }

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 puter'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)
    }
Share Improve this question edited May 28, 2022 at 11:35 Sunderam Dubey 8,83712 gold badges24 silver badges42 bronze badges asked May 28, 2022 at 11:34 THW 10THW 10 1212 silver badges8 bronze badges 1
  • I wrote a dependency free srt parser: github./plussub/srt-vtt-parser it is a simple state machine which interpret the input. Maybe it help to get an idea. – ste-xx Commented May 30, 2022 at 15:52
Add a ment  | 

3 Answers 3

Reset to default 5

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./package/srt-parser-2

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.

We can do this using fs.createReadStream.

export function convertSrtToText(srtFilePath: string) {
  let srtPath = "downloads/" + srtFilePath;
  let fileName = "textSrt_" + srtFilePath;
  let outputPath = "downloads/textSrt_" + srtFilePath;
  let output = "";
  fs.createReadStream(srtPath)
    .pipe(parse())
    .on("data", (node) => {
      output += node.data.text+" ";
    })
    .on("error", console.error)
    .on("finish", () => {
      filehandle: fs.writeFileSync(outputPath, output);
    });
  return fileName;
}
发布评论

评论列表(0)

  1. 暂无评论