Parse a srt file

Aug 24, 2010 at 10:07pm

Parse a srt file

Hello Maxers!!!
Does someone have a idea to how to parse a subtitle file in srt format like this:
00:00:20,430 –> 00:00:24,850
Un lapin à diner

00:00:54,870 –> 00:00:56,420
Comment faire !!!

Best regards

Aug 24, 2010 at 10:36pm

check out jit.textfile and jit.str.regexp. There is an example in the jit.str.regexp help patch that does parsing of html.

You could also probably do all of this in javascript too.


Aug 25, 2010 at 5:46pm

You are lucky.
I’m just working on a patch that parses an srt file using javascript.

Here it is:

– Pasted Max Patch, click to expand. –

and here’s the javascript file: save as “srt.js”:

autowatch = 1;
outlets = 3;
var framerate = 25;
var subfile = “”;

function setFrameRate(fr) {
framerate = fr;
outlet(2, “framerate: ” + framerate);

function setSubFile(sf) {
subfile = sf;
outlet(2, “subtitle file: ” + subfile);

function readSrt() {
var reading = “”;
var f = new File(subfile);;
if (f.isopen) {
reading = “”;
while(f.position < f.eof) {
reading += f.readline(800) + "n";

function parseSrt(data) {
// trim white space start and end
srt = data.replace(/^s+|s+$/g, '');
var caplist = [];
caplist = srt.split('nn');
for (var i = 0; i < caplist.length; i=i+1) {
//get timecode, i love regular expressions
//to learn regexp ->
var timecode = caplist[i].match(/(d+):(d+):(d+),(d+)s–>s(d+):(d+):(d+),(d+)/g).join();
//get subtitle
subtitle = caplist[i].replace(/(d+)s(d+):(d+):(d+),(d+)s–>s(d+):(d+):(d+),(d+)/g, “”).replace(/n/, “”);
//store timecode numbers in an array
//split(/s/); strips out a whitespace at the beginning
var burp = timecode.replace(/[:]|(s–>s)|,/g, ” “).split(/s/);
/*convert h+m+s+ms to framecount
burp[0]/burp[4] –> hour
burp[1]/burp[5] –> minute
burp[2]/burp[6] –> second
burp[3]/burp[7] –> millisecond
var startTime = Math.round(((burp[0] * 3600000) + (burp[1] * 60000) + (burp[2] * 1000) + Math.round(burp[3] * 1)) / (1000 / framerate));
var endTime = Math.round(((burp[4] * 3600000) + (burp[5] * 60000) + (burp[6] * 1000) + Math.round(burp[7])) / (1000 / framerate));
outlet(0, startTime);
outlet(1, subtitle);
outlet(0, endTime);
outlet(1, “”);

I’ve tested it with the open movie Elephant’s Dream

you can find the english subtitles at:

At present, the js file does not parse any tag inside the subtitles (for italic, bold etc…).

Aug 25, 2010 at 5:50pm


First save the javascript somewhere on you hard drive, then create an empty file on the same location. Save the empty file. Then you can paste the patcher code, or you will loose some patch chords between the js and other objects.

Aug 25, 2010 at 5:57pm

another note: you can send the message “leadscale 2.5″ to the object to adjust the line height

Aug 27, 2010 at 1:06pm

Wahoo!!! Bravo Roccapl
Yes i’m lucky.
Thank you very much.
There’s two months that i search to parse the srt file.
I try to make a dubbing application.
I will post it on the forum when finished.
Have a nice day.

Oct 25, 2010 at 9:52pm

Hello dear Maxers,
I have try to make a dubbing software (with your help!!!).
And now i post my work.
Here is the link to download the film:
I hope that it can help.
Best regards

Jan 28, 2011 at 10:44am

@roccapl: can I borrow some part of your patch for a project I have ?

Jan 28, 2011 at 10:46am

@julien bayle: of course, it would be a great pleasure

Jan 28, 2011 at 10:49am

I’m trying to make it working.
It doesn’t work because I probably forgot something.

Feb 5, 2011 at 7:41pm

little problem in opening my srt file :-(
dealing with path etc.

Mar 25, 2011 at 12:54pm

hello, it worked and works very fine.

I just have a little problem.
The character encoding of my .srt files is very important and makes different render.

I used notepad++ in order to make some tests.
* when the character encoding is ANSI, all my accents (éàÔ etc) aren’t rendered well ! the most strange thing: in the coll, when I edit the text, accents are rendered correctly.

* when the character encoding is UTF-8, all my accents (éàÔ etc) are rendered well !
In that case too, in the coll, when I edit the text, accents are rendered correctly and the coll is the same than before.

any ideas?


You must be logged in to reply to this topic.