Forums > MaxMSP

Parse a srt file

August 24, 2010 | 10:07 pm

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

August 24, 2010 | 10:36 pm

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.


August 25, 2010 | 5:46 pm

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"; } } f.close(); parseSrt(reading); } 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…).

August 25, 2010 | 5:50 pm


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.

August 25, 2010 | 5:57 pm

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

August 27, 2010 | 1:06 pm

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.

October 25, 2010 | 9:52 pm

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

January 28, 2011 | 10:44 am

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

January 28, 2011 | 10:46 am

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

January 28, 2011 | 10:49 am

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

February 5, 2011 | 7:41 pm

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

March 25, 2011 | 12:54 pm

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?

February 1, 2016 | 11:45 am

Hi @roccapl , thank you for your script

it works great on PC, however now i’m trying to run it on a mac, but now and then it crashes max.

I found out this crash occurs only when the SRT file contained a degree symbol, which on mac shows up
as a infinity symbol.

replacing them by hand is not a option, since i’m batch parsing the SRT files

so.. i’m looking for some kind of code in the javascript which replaces or removes the infinity symbol

any thoughts ?

February 1, 2016 | 12:10 pm

Hi Matt,
The problem seems related to the character encoding of your srt file. Most srt files are encoded in ISO-8859-1, try UTF-8 for maximum compatibility.
Here are some links:

February 3, 2016 | 9:54 am

Thanx @roccapl

this seems to work

iconv -f iso-8859-1 -t utf-8 < >

Viewing 15 posts - 1 through 15 (of 15 total)

Forums > MaxMSP