Adding a timecode in After Effects using an Expression

After Effects After Effects Expressions javascript

In this tutorial, I will show you how to create a customisable timecode/timeline counter using an After Effects Expression. Specifically, something like the following:

If you’re time poor, and just want to skip to the facts, I got you, use the links below to skip to the relevant part for you, or read on to continue.

References

I’ve seen a few videos on YouTube about this topic, and while they are all fine, they just weren’t what I wanted for a video I am currently creating.

Most people were talking about the Timecode Video Effect, which is built-in to After Effects, but that doesn’t allow you to change the font.

Or another example was using an Expression to count, which DID allow you to customise the font, but it didn’t count up based on the current time.

And it’s also possible to do this in Premiere as well, but I really wanted to give it a go in AE, as it’s better for doing animated stuff.

Here’s two example videos that I consulted for some inspiration, but ultimately gave up on as they didn’t meet my unique requirements! 😛

I also consulted an old Creative Cow page, that had an example of an “Expression for Digital 24 Hours clock“. This was a good start, but I wanted to add some more to it to suit my particular requirements.

And finally, I consulted this Adobe page, as it had some code references, if you want to do more with Expressions in After Effects, check this page out.

Ultimately, my goal was to create an old VHS time code effect, something like this…

(Check out this video for some more VHS nostalgia feels)

Read on to see how to do this!

back to top

Step 1 – Creating your After Effects Project

Open After Effects, I’m using the latest version at the time of this post (After Effects CC 2018) but this should work in earlier versions.

Select “New Composition

Update the settings in the Composition Setting dialog to suit your needs, I have a specific width, height, frame rate and duration, but yours may vary (I made the duration go for an hour just to prove the point of this functionality, but it’s not necessary).


back to top

Step 2 – Adding the text layer

With your composition created, create a text layer by selecting “Layer” > “New” > “Text“.

Open the “Character” panel from the right of the After Effects window, and select your chosen font and style, I selected a custom Commodore 64 font, and made it reasonably large and centered it on the composition.

I also typed some numbers on the text layer just to see it, this isnt important, as it will get overwritten, but if you need a handle to grab onto, this is a way to do it.


back to top

Step 3 – Adding the expression

Go to the “Effects & Presets” panel, and under “Expression Controls“, select “Slider Control“, and drag it to your text layer.

Expand the “Text” layer, then the “Text” property of the layer, the “Effects” property, and then the “Slider Control“, it should look as follows:

Then click and hold down your left mouse button on the “Pick Whip” icon on the “Source Text” stop watch, and drag it to the “Slider” stop watch in the “Slider Control” effect, refer to the following:

You will notice that the number on your text layer has changed, that’s because the “Source Text” of the text layer, is being controlled by the “Slider Control“.

Next, select “Expression: Source Text” under the “Source Text” stop watch, and then select and delete the default expression (use CTRL/CMD + A to make sure it’s empty)

Replace it with the following:

function pad(str, max) {
	str = str.toString();
	return str.length < max ? pad("0" + str, max) : str;
}

function digits(myVal, myNumDigits, myPad) {
	var s = myVal.toString();
	while (s.length < myNumDigits) s = '0' + s;	
	return pad(Math.floor(s), myPad);
}

hr = digits(time / 3600, 1, 1);
min = digits(time % 3600 / 60, 2, 2);
sec = digits(time % 60, 2, 2);

"SLP " + hr + ":" + min + ":" + sec;

If all goes well, your composition should look as follows:

Grab the Time scrubber and skip ahead a few frames, the text on the text layer should change to reflect the current time in the composition!

If all goes well, your composition should look as follows…


back to top

Step 4 – The Expression explained

Here’s an explanation of the Expression.

The “pad” and “digits” functions are string formatting functions.

function pad(str, max) {
	str = str.toString();
	return str.length & lt;
	max ? pad("0" + str, max) : str;
}

The “pad” function, adds 0 (zero) to the left of one of the time numbers when it is called, that’s its only purpose.

Next is the digits function:

function digits(myVal, myNumDigits, myPad) {
	var s = myVal.toString();
	while (s.length & lt; myNumDigits) s = '0' + s;
	return pad(Math.floor(s), myPad);
}

The “digits” function formats the values (myVal, myNumDigits and myPad) passed to it. It uses a while loop to restrict the number of digits to the specified  amount passed to it.

This function also calls the “pad” function when returning the Math.floor’d value of the passed time number, and passes on the specified pad amount as well.

The next three lines are variables that store the hours, minutes and seconds values.

hr = digits(time / 3600, 1, 1);
min = digits(time % 3600 / 60, 2, 2);
sec = digits(time % 60, 2, 2);

These variables call the digits function in three different ways, by passing in the “time” value (an AE constant of sorts), performs simple calculations on the time number to get the hours, minutes and seconds, then restricts the numbers to 1 digit for hours, 2 digits for minutes, and 2 digits for seconds. The last passed parameter is the amount of pad required, because our VHS example only has 1 digit required for hours, (most likely because VHS tapes didn’t go past 9 hours? who knows, that’s the retro format).

The last line in the Expression, concatenates the previous variables together, and gets displayed in the Composition (the SLP and colon : characters, complete the effect).

"SLP " + hr + ":" + min + ":" + sec;

back to top

Step 5 – Customising the Expression

To customise the Expression, it all depends on the types of things you want to do.

You can remove the SLP and colon characters from the string by replacing the above with this:

hr + min + sec;

Or you could add something different around the time values.

"The time is " + hr + "|" + min + "|" + sec;

The possibilities are endless.
back to top

Conclusion

And that brings us to the end of this tutorial!

If you want the .aep file for this project, you can download it here:

I hope this was informative, and you can make amazing retro VHS videos to your hearts content!