Lesson03 – Subtitle track from an external file

Learn how create a subtitles track from an external text file or using a markers track

If you are an absolute beginner, please start your learning by checking the essential resources below: http://www.jjgifford.com/expressions/basics/index.html

1. Setup

  • Create in an empty text file a serie of variables using this format
sub1 = [inPoint (frame), outPoint (frame), “subtitle”];

For example

sub1 = [0,45,”this is where it all began”];
sub2 = [50,75,”a simple text file”];
sub3 = [80,135,”that will change the world, \r forever”];
  • Save the text file, and get its absolute path (drag&drop in a Terminal window)
  • In After Effects, create a new comp and a new text layer
  • Rename the layer to sub1 (name of the 1st variable in your text file)

2. Expression writing

  • Add an expression to Text Layer> Text> Source Text
var myIndex = thisLayer.name;
stores the layer’s name in a variable myIndex
use your absolute path to point to your text file
use array position 2 as the text source
  • Add an expression to Text Layer> Opacity
var myIndex = thisLayer.name;
  • Store the in-point and out-point in 2 variables, and convert them in seconds (25fps)
var start = eval(myIndex)[0]/25;
var end = eval(myIndex)[1]/25;
  • Finally create a condition to toggle on/off the opacity
if ( time > start && time < end ) {100} else {0}
if (conditions) { result } else { result if false }
equal to ==
not equal to !=
greater than or equal to >=
lesser than or equal to <=
and &&
or ||
not !
  • (Optional) Apply the textBox preset from the previous lesson, add an expression to the opacity property and use the pick whip to link it to the Text Layer Opacity property; parent the Shape Layer to the Text Layer
  • Reposition the Text layer to the bottom of the screen
  • Duplicate the layer(s) as many times as you have created variables

This setup has the huge advantage to update the subtitles if you modify the external text file (remember to purge memory to force to update after editing) But if you work with a .SRT/.AAS/…, then you need to convert using a powerful text editor to match the formatting.

3. Using Markers

You can import a markers track from Premiere (dynamic link) or FCP (XML); these markers have the ability to store both a timecode and a text input. In After Effects, you can add a marker to the selected layer with shortcut Ctrl+8, if you want to manually create the markers track, and access markers comment by double-clicking on the marker itself.

marker.numKeys() counts markers on the layer
marker.nearestKey() points to the nearest marker
marker.key() points to the specified keyframe
  • Use .nearestKey() to read the comment stored in the nearest keyframe

The subtitle is not synchronized with the markers. nearestKey() behaves like a ceil operation, it rounds the number upwards to the nearest integer. To correct this, we can create a condition that triggers the subtitles only after the current time is greater than the marker position. For argumentation sake, let’s first pretend there is only 1 marker and 2 states, before and after the marker.

  • Using an if condition, compare the marker “1” time position to the current time
var m=1;
if (marker.key(m).time <= time) {
} else {“”}

If the current time is before the marker, then it will display nothing; if after, then it will display the marker “1” comment Now all what we need is to increment m each time we reach a new state. .nearestkey() does just that!

    • redefine m variable using .nearestKey()
var m = marker.nearestKey(time).index;

Subtitles start at the right time but it does not play until the out-point marker

    • Change the else condition to the marker comment at position m-1
var m = marker.nearestKey(time).index;
if (marker.key(m).time <= time) {
} else {marker.key(m-1).comment}

AE returns an error: “this property has no keyframe number 0” When m = 1 then (m-1) = 0; there is no marker index “0”, we need to get rid of this unique event.

  • Add a new condition for m = 1, to cancel marker.key(0)
var m=marker.nearestKey(time).index;
if (marker.key(m).time <= time) {
} else { if (m == 1) {“”} else {marker.key(m-1).comment} }
Script that convert SRT file to a markers track:
build your own presets collection to speed up your workflow
Click icon below to download presets (rename .key to .zip after download) download