ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

SRT files in AE - a working solution -

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Remco Tent
SRT files in AE - a working solution -
on May 1, 2018 at 8:03:47 am

THE ISSUE:
For a while now I've been seaching for an easy way to add subtitles to my After Effects files. I know there are a few different ways to use free and/or paid extendscripts but the ones I've found all ended up with having hard-coded keyframes. When you've made some manual changes to your subs in AE and there were some last-minute changes to your subs you either had to manually do the corrections, or re-run the script with the new sub and re-do all you manual (formatting) changes.

Wouldn't it be ideal if you could just import/link the SRT file into your library like you would with other kinds of footage?

THE ANSWER:
you can!

Since the CC2018 version of AE you can import JSON files to create data driven animations. Well, isn't a JSON file basically just text? So this got me thinking; "what if I tried importing a SRT file instead of JSON?". As it turns out, if you omit the file mask in the import file dialog (All files (*.*)), you can in fact import *.srt files. Using

footage("filename.srt").sourceText

as an expression on a textLayer's Source Text if showed the (complete) contents of the SRT file. All I had to do now is parse the contents, get the in-time, out-time and the actual sub and have it all displayed at the right time. Mind you, I'm not a programmer so there might be a better, more efficient, prettier looking solution than this, but here's my working solution:

var subFile = "sub.srt";

var lines = footage(subFile).sourceText.split('\n\r\n');

for (n = 0; n < lines.length; n++) {
if (time >= srt(lines, n).start && time < srt(lines, n).end) {
sourceText = srt(lines, n).sub;
break;
} else {
sourceText = "";
}
}

//------------------------------------
function srt(lines, i) {
origin = lines[i].split('\n');
ID = parseInt(origin[0]);
startText = origin[1].match(/^[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]/)[0].replace(",", ":");
endText = origin[1].match(/\s[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]/)[0].replace(' ', '').replace(",", ":");
var subtitle = "";
for (var j = 2; j < origin.length; j++) {
subtitle = subtitle + origin[j] + '\n';
}
return {id:ID, start:parseTime(startText), end:parseTime(endText), sub:subtitle};
}

//------------------------------------
function parseTime(str) {
hours = parseInt(str.split(':')[0]);
minutes = parseInt(str.split(':')[1]);
seconds = parseInt(str.split(':')[2]);
millisesconds = parseInt(str.split(':')[3]);
t = (hours*60*60) + (minutes*60) + seconds + (millisesconds/1000);
t = Math.round(t*100)/100;
return t;
}


You simply place the above expression in the Source Text property of a TextLayer and replace "sub.srt" with the name of your imported subtitle file.


Return to posts index

Kalleheikki Kannisto
Re: SRT files in AE - a working solution -
on May 1, 2018 at 4:06:11 pm

No kidding?! That sounds very useful. Thank you for sharing.

Kalleheikki Kannisto
Senior Graphic Designer


Return to posts index

Teague Chrystie
Re: SRT files in AE - a working solution -
on May 2, 2018 at 10:47:58 pm

This is very cool. Thanks for sharing.


Return to posts index


Rudi Nieuwenhuis
Re: SRT files in AE - a working solution -
on Jul 2, 2018 at 9:19:42 pm

The moment i try to import a .srt file, AE gives me an error.
This is in CC2018.
What format do you choose when you import?

Cheers,
Rudi



Return to posts index

Simon Francois
Re: SRT files in AE - a working solution -
on Jan 29, 2019 at 9:31:49 am

Hi all, hi Remco,
I've been Lucky enough to find your script as it was the perfect answer to problem I have. Though, the script doesn't work for me and I get an error message saying:
Cannot read property 'match' of undefined
The error is said to be on line 18 (the script I use i the same the original post mentioned atop).

The .srt files I work with are well formated. I checked that may times, and even threw them in AME to see if it could render proper empty subtitled MP4 files, and it did very well.

Trying and failing, I was wondering if it wasn't me just being stubborn and whether there would be any other method via Premiere or different scripts that could get me there, but doing it directly in AE would be very convenient, since I'm placing it in animations I'm designing, and have to keep an eye on the text layout amd how everything fits together, being able to move things if required by legibility.

Also, doing it by hand is no option since I have to deliver 17 of them, 10 to 15 seconds long, byt the end of this Week.

Any thoughts on that?

Thanks a lot in advance,
Simon


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]