Simple-K-replacer tutorial
From Aegisub Wiki
|
This tutorial will teach you how to use the simple-k-replacer.lua script to create simple karaoke effects.
Prerequisites:
- A karaoke timed subtitle file
- Some knowledge of ASS override tags (the more the better)
- Updated Automation 3 scripts
[edit] Basic usage of simple-k-replacer
Load your timed song. If you don't have a karaoke timed subtitle file, now is the time to create one.
If you don't have one right now, but really just want to get on with the tutorial, create a new file in Aegisub and paste this on the first line:
{\k10}ka{\k5}ra{\k50}o{\k50}keLoad the simple-k-replacer.lua script:

Click the Apply button in the Automation Manager window.
You now get a dialog box asking for options for the simple-k-replacer script:
The long text at the top in this dialog is a quick manual to the simple programming language used by this script. This tutorial will teach you more in-depth how to use this mini-language to create simple karaoke effects.
Don't click Ok just yet. First change the text in the \k replacement field to the following:
{\r\t($start,$mid,\fscy200)\t($mid,$end,\fscy100)}Then click the Ok button.Notice how the subtitles changed. For example, the sample line from step one would become this:
{\r\t(0,0,\fscy200)\t(0,0,\fscy100)}{\r\t(0,50,\fscy200)\t(50,100,\fscy100)}ka{\r\t(100,125,\fscy200)\t(125,150,\fscy100)}ra{\r\t(150,400,\fscy200)\t(400,650,\fscy100)}o{\r\t(650,900,\fscy200)\t(900,1150,\fscy100)}keNotice how the times in the \t tags correspond with the times in the \k tags. Take a guess at what the effect does. Then close the Automation Manager, save the subtitle file (you'll probably want to use a new name if you loaded it from disk) and preview the effect on some video of your choice.
For more on the language used to create effects, see below.
After previewing your effect, pick Edit→Undo to get back to your blank timed subs.
While using the Apply button can seem convenient, it can also quite easily get tiresome having to go back, undo, enter Automation Manager again and try again if you make a mistake. Or having to close Automation Manager, save under a different filename, undo and open Automation Manager again each time you want to preview.
There is an easier way.
While having you blank timed subs open and simple-k-replacer.lua loaded, go to File→Export Subtitles. See how "Basic \k replacer" also pops up there? And the options dialog you got when pressing Apply before also appears. It even has the effect you entered before in it!
Uncheck everything but "Basic \k replacer".

Now, how about a more advanced effect, maybe adding a calculation?
Enter the following effect string:
{\r\t($start,%$start+$dur%,\bord4)\t($start,%$end+750%,0.5,\frx%360*3%)\t(%$start+$dur%,$end,\bord0)}The percent-signs used here mean that everything between such a pair is a calculation. You can make it add, multiply, divide numbers and much, much more, though those will probably be what you do the most. It should be quite straight forward.
The only thing here that might a little explanation is the %$start+$dur% part. Why add the duration to the start time, won't that just yield the end time? No, because $dur is the duration in centiseconds, while $start and $end are in milliseconds, ie. ten times as big a scale. $start+$dur instead means one tenth into the syllable time, and you can also use this trick to get other basic fractions of the syllable. For example, %$start+$dur*3% is three tenths into the syllable time.
Click Export and save with a new filename.
When you preview it, it might look a little odd. This one effect looks the best if alignment is 5, ie. [[\an]5, set either in the style or at the beginning of the line. The \r tag won't reset \an.
Also note that this doesn't change your subtitles loaded into Aegisub, it just generates a new file. If you need to tweak your effect, just go File→Export Subtitles again, change the effect and Export again.
This concludes the first introduction to the simple-k-replacer.lua script. You should now be ready to write your own effects. If you need some inspiration, take a look at the next section. Also take a look at the section on the effect language for some more tips and tricks.
[edit] More example effects
Simple shadow pop:
{$kind$dur}{\r\t($start,$start,\shad0)\t($start,$end,\shad2)}
This section is a stub. Someone needs to fill in some samples.
[edit] More on the effect language
While the most important variables ($dur, $start, $end, $mid) are already presented, these are not the only ones available. Here are some additional variables you can also use:
- $kind - the \k tag used, ie. one of: \k, \K, \kf, \ko. You can use this for something like {$kind$dur}, to re-create the original \k tag including timing.
- $text - the text in the syllable. While the use of this might not be immediately obvious, it can be used. This will be explained further below.
- $i - the number of the syllable on the line. Ie. the first syllable is 1, the second is 2 and so on. You can use this to create some effects that rely not on the timing of the syllables but their number instead. This is especially useful for entry/exit effects.
Now, the real interesting thing is that everything between two percent signs are actually read as a tiny Lua script, meaning you can do pretty much anything Lua can do there. Most useful is using mathematical functions. For example:
- {\r\t($start,$start,\bord%math.exp($dur/5)%)\t($start,%$start + math.log($dur)*100%,\bord2)} - this uses the log (logarithm) and exp (<math>e^x</math>) functions to do some funky stuff with the border.
- {\r\org(10000,0)\t($start,$mid,\frz-%math.random()/10%)\t($mid,$end,\frz0)} - this uses the random function along with the rotation jump trick to make each syllable jump a random amount. math.random() alone generates a random number between 0 and 1. You can also use math.random(a) to make a random whole number between 1 and a, and math.random.(a,b) to make a random whole number between a and b.
- {\r\shad0\t($start,$start,\shad%string.len("$text")+2%)\t($start,$end,\shad0)} - here the string.len function is used to get the number of characters in some text, and this is where $text becomes useful. Note the quotation marks around $text. These are required, otherwise Lua will think the text means something special, and is not just a text string.
- A full list of all mathematical functions you can use can be found here.

