Copy link to clipboard
Copied
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.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
+ '\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.
Copy link to clipboard
Copied
Thanks a lot.
This script is working for me. Also the script from https://digitalanarchy.com/demos/SRT-importer.html
But the issue i am facing is I am unable to change the font. I tried adding the expression to change it but its not working for me. I am completely new to expression.
Can someone help me out with this will be of great help.
Thanks,
Copy link to clipboard
Copied
Update: I had problem with Hindi font not apprearing as it should. After lot of research found the solution..It was not changing the font was going to help.
Added expression for font, changed default font of after effect, tried different script, nothing worked. Until i went to
Edit>Preferences>Type>(and changed the language input from Latin to) South Asian and Middle Eastern.
Re-run the script and worked.
Copy link to clipboard
Copied
Hi, thanks for the solution, but I had to do a bunch of debugging to get it to work for me. Firstly, I did select 'All files' which let me import the SRT, but I had to import it as a Javascript, not a JSON. The project I'm working on means each lines of subtitles is just one word, so this might not work for everybody. The formatting for my SRT looks like this:
1
00:00:00,000 --> 00:00:00,329
So
2
00:00:00,329 --> 00:00:00,599
I'm
3
00:00:00,853 --> 00:00:01,513
very
4
00:00:01,513 --> 00:00:02,203
much
5
00:00:02,263 --> 00:00:02,863
looking
And my expression is this:
function srt(i) {
origin = lines[i].split('\n');
ID = parseInt(origin[0]);
timeText = origin[1].split(' --> ');
subtitle = origin[2];
return {id:ID, start_time:parseTime(timeText[0]), end_time:parseTime(timeText[1]), sub:subtitle};
}
//------------------------------------
function parseTime(str) {
timeText2 = str.split(':');
hours = parseInt(timeText2[0]);
minutes = parseInt(timeText2[1]);
time_sec = timeText2[2].split(',');
seconds = parseInt(time_sec[0]);
millisesconds = parseInt(time_sec[1]);
t = (hours*60*60) + (minutes*60) + seconds + (millisesconds/1000);
return t;
}
//------------------------------------
var subFile = "audiogram.srt";
var lines = footage(subFile).sourceText.split('\n\n');
if ( time >= srt(lines.length-1).start_time) {
sourceText = srt(lines.length-1).sub;
} else {
for (let n = 0; n < lines.length-1; n++) {
if (time >= srt(n).start_time && time < srt(n+1).start_time) {
sourceText = srt(n).sub;
break;
} else {
sourceText = "";
}
}
}
So I parsed the time slightly differently and there were a couple other bugs I had to clean up. If your subtitles have more than one line, you probably just have to work in the for loop in the 'subtitle' variable. Hope this helps someone.