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:
1
00:00:20,430 –> 00:00:24,850
Un lapin à diner

2
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.

-Ben


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);
f.open();
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 -> http://www.regular-expressions.info/
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

http://orange.blender.org/download

you can find the english subtitles at:

http://www.annodex.net/~silvia/itext/elephants_dream/elephant.english.srt

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


August 25, 2010 | 5:50 pm

Note:

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 jit.gl.text3d 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: http://video.blendertestbuilds.de/download.blender.org/ED/elephantsdream-480-h264-st-aac.mov
I hope that it can help.
Best regards

Attachments:
  1. DubbingProto.zip

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 :-(
grrr
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?


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