srt parser and composer
A couple of modules to parse and generate srt files. No external dependencies needed.
- parse an srt file or srt string (content of an srt file) into a json or plain text.
- create an srt file from json
- parse an srt file into a csv file.
Parser
Give an srt
file it returns json or a plain text of all the lines.
Srt file
See exampel in nroman_door_manual_transcription.srt
100:00:00,160 --> 00:00:04,890There’s this door on the 10th floor I justhate so much. 200:00:04,890 --> 00:00:05,798Goddammit! 300:00:05,799 --> 00:00:11,629Do you ever get this door wrong? “prettyregularly.” 400:00:11,629 --> 00:00:12,000How often? “like 30% of the time.”
Srt lines vs file lines
In the code comments I refer both to srt lines and file lines, so I thought I'd clarify.
Srt flines
1
00:00:00,160 --> 00:00:04,890
There’s this door on the 10th floor I just
hate so much.
File lines
While in terms of file lines the following are all considered individual lines.
1
00:00:00,160 --> 00:00:04,890
There’s this door on the 10th floor I just
hate so much.
The srt parser has for possible outputs - srt parser
The parser has 4 functions,
they both take in an srt file (file path/name) as input parseSrtFileToJson
returns a json and parseSrtFileToText
returns plain text without timecodes.
You can also parse the content of an srt file, what I refer to as an srt string
directly to plain text or json using using parseSrtContentToJson
and parseSrtContentToText
parseSrtFileToJson
parseSrtFileToJson
returns a json like the one below.
and can be used as follows.
//var parser = require("./index.js");var srtParser = parser;var srtFile = './example/nroman_door_manual_transcription.srt' srtParser;
Example json output
example_output/norman_door_human_transcription.json
, , , , ,...]
parseSrtFileToText
while parseSrtFileToText
returns a plain text like the one below and can be used like so.
var fs = ;var srtParser = parser;var srtFile = './example/nroman_door_manual_transcription.srt' srtParser;
Plain text output Example
There’s this door on the 10th floor I just
hate so much.
Goddammit!
Do you ever get this door wrong? “pretty
regularly.”
How often? “like 30% of the time.”
Have you seen people misuse it?
All the time. Every day. Constantly.
I hate this door.
Me too Kelsey.
working with srt string
I was had a use case of a speech to text API that returned an srt file. but rather then returning the file, It was returning a string with the content of the srt file.
So for flexibility, you can also parse the content of an srt file, what I refer to as an srt string
directly to plain text or json using.
parseSrtContentToJson
and parseSrtContentToText
.
And they give the same output as their file opening counterpart described above. Only difference they take in the content of the srt as a string rather then file path/name of the srt file.
Examples
You can find an example srt
in the example folder. And some example of the output int he example_output
folder.
you can run following command in terminal to try the example
node parser_test.js
Composer
Give an srt json
it returns an srt file either as a string content of the srt or as a path to where the srt file as been written to disk.
For example of srt json
specs see example_output/norman_door_human_transcription.json
Usage
var fs = ;var srtComposer = composer; var srtJson = './example_output/norman_door_human_transcription.json';var srtJsonContent = JSON; //creating srt string from jsonsrtComposer //creating srt file from jsonvar JsonToSrtTest = './example_output/composer_test_srt_from_json.srt'; srtComposer
Srt file to CSV
Sometimes you want to share an srt and get some feedback on it. A google spreadsheet is generally the most effective. With this module you can convert your srt to a csv file, so that it can be uploaded to google spreadsheet, or some other use case.
./example_output/demo_test.csv
n | In | Out | Text |
---|---|---|---|
1 | 00:00:00,160 | 00:00:04,890 | There’s this door on the 10th floor I just hate so much. |
2 | 00:00:04,890 | 00:00:05,798 | Goddammit! |
3 | 00:00:05,799 | 00:00:11,629 | Do you ever get this door wrong? “pretty regularly.” |
4 | 00:00:11,629 | 00:00:12,000 | How often? “like 30% of the time.” |
Usage
Takes in file path/name of the srt file, the desired name/path for the csv file, and the callback returns the csv file name you specified once it's done writing to disk.
var srtToCsv = srtToCsv; var demoCsvFile = './example_output/demo_test.csv'
Examples
You can run the example with
node composer_test.js