DVD Scripting

This article was written by Alistair Jackson of EditHouse, and was first published in Digital Media World magazine in March 2003.

The fact that DVD sound and vision quality is so much better than poor old VHS, gets most of the press coverage, but another fabulous aspect of DVD-Video is the interactivity. While skipping back and forwards, or making choices from a menu is a feature of all but the simplest DVD's, more advanced interactivity is not generally taken advantage of.

The extra features on the Matrix and Shrek discs show just how much more is possible, and while these fancy features require more than just the preset options provided by the DVD Studio Pro interface, there are ways to achieve them. By writing scripts you can alter the play structure of a DVD, and it is possible to design simple games or to make the play order of the disc depend on user defined criteria.

I will be describing some of the scripting possibilities for Apple's DVD Studio Pro 1.5, but the process for other authoring tools is quite similar, as they are all creating digital codes to fit into the required syntax of the DVD-Video standard. While the exact script structures may vary, the rules and possibilities are the same.

There are a number of ways of playing video tracks in random order. The easiest is to just use the built in random number generator to decide which track to play, as is shown in figure 1. This is perfectly fine in many situations. The only problem is that each time you call it, a totally random number is generated, so you could end up with track 2 playing, then 2 again, then 3, then 2 again. For some situations this is what you want, but most of the time, having the same tracks repeat will become very annoying.

Script for random play

If you have read your DVDSP manual, you'll recall that values are assigned to variables with the = command and that values are compared with the == command. The DVD-Video standard requires a DVD Player to provide 16 variables where data can be stored. DVDSP uses eight of these variables itself, and allows access to the other eight through scripting commands. It gives the variables the default names A through H, but allows the programmer to rename them if desired, as is shown in figure 2. So, the command "A ?= 6", translates to; variable A equals a random number between 1 and 6.

DVD Studio Pro variable control

To make it less likely that the same track will be played twice in a row, you could use the script in figure 3. This provides a slight improvement, with only a little extra code. It simply checks to see if the last random number was the same as the new one, and if so, it gets another random number. This next random number could of course be the same again, but it's less likely.

Improved script for random play

Another point to note is that a script only runs until the first jump it executes. So if our random number is 2, then as soon as the command "if A == 2 then play ... " is encountered, the DVD plays that track and won’t complete the rest of the script. If you want the random choices to be made continuously, then you need to set "jump when finished" to "Random Number Script Two" at the end of each track.

The final line calling, "Error Menu", is included as a fail-safe. While you are unlikely to get such a simple script wrong, it's a good habit to check for unexpected conditions, and jump to some sort of error message if they occur. In this case I would simply have a blank screen with a "Script error. Press Enter to return to the main menu" type of message. Or you could try Bill Gate's blue screen of death approach, with an inexplicable error code number, if you prefer.

Scripts can be executed from several points in a DVD, most of which are shown in figure 4. A Pre-script is one that plays whenever that track is called. Remote-Control keys can be reprogrammed with DVDSP, however several models of DVD Player will totally ignore reprogramming of the Track, Audio and Subtitle keys, and simply perform their default function instead.

DVD Studio Pro property menu

To keep track of your program flow, it is always useful to have "Lines" turned on (see figure 5) and to have the debugging window open when in preview mode (see figure 6). The debugging menu steps through each script when encountered. It also shows you the value of all 8 variables. In the illustration we can see that variable C has the value of 47. From the code shown, it's clear that 47 must be the number returned by getLastItem(). While DVDSP calls it "FirstVideo.mpg" for our benefit, the encoded DVD simply knows it as track 47.

DVD Studio Pro screen grab

Another solution to the random play issue would be to have six different play lists, and use the random number generator to choose between them. List one plays, 1 then 2 then 3. List two plays 2, 3, 1. List three plays 3, 2, 1. List four 1, 3, 2. List five 2, 1, 3. List six 3, 1, 2.

This works fine for choosing the order for 3 tracks, but for 4 tracks you would need 24 lists and for 5 it would be 120! The scripts take up next to no space on the DVD, but creating them all could be a little laborious.

DVD Studio Pro debugging window

Warning; if you're not a great fan of mathematics, you might want to skip the following paragraph!

To build a true random play feature, you need to either set the play order up front or keep a record of which tracks have been played. However, with only 8 variables available to you, this initially seems impossible. One solution is to break these variables down into their binary values. Each variable is 16 bits big, so track one can be represented by the binary value %0000000000000001, track 2 by %0000000000000010, and so on. You can then turn bits on and off with the bit-wise &=, |= and ^= operands. If you're a fan of Boolean algebra, then you'll see a number of database and other possibilities present themselves here. However, that's the makings of a far longer article than this one.

A particularly useful script is the one which turns off subtitles. An undocumented annoyance in DVD Studio Pro is that once you've included any subtitles in a track, they are turned on as the default setting. In many situations, this isn't what you want. Fortunately, there are two ways around this. One is to create another subtitle track with no actual content, thus making the default a blank subtitle stream. The second is to include a script that fires off as one of the first items on your DVD and simply says "setSubtitleStream 64". The script turns the subtitles off until a viewer hits the subtitle key or another script turns them on. This is a far simpler solution than creating a second subtitle stream for every track.

How about playing one track the first time a menu item is selected, then a different track the next time it's selected? There are a number of ways of doing this, but two methods are shown below.

We could try : G += 1
  if G == 1 then play track "FirstVideo.m2v"
  G = 0
  play track "SecondVideo.m2v"

Or this one :

H = getLastItem()
  if H == track "FirstVideo.m2v" then play track "SecondVideo.m2v"
  play track "FirstVideo.m2v"

The first will play tracks alternately each time, so long as no other script changes the value of G. The second will only play "second video", if it was immediately preceded by "first video". Once again, remember that as soon as one of the "play track" commands is executed, program flow leaves that script, and the rest of the code is ignored. To execute the script again, it must be explicitly called.

Figure 7 combines some of these ideas together. It will play through 3 tracks one after another then return to the main menu. Each track must have its "Jump when finished" set to "Play All Script". If a script elsewhere on the disc has set variable C to equal 2, then the 3 tracks will be played randomly instead. So you could have a "Play Random" button on your main menu that called a script which set C to 2, then played script "Play All Script".

Script for Play All

As a final example, figure 8 shows a script for checking the Region Code of the DVD player being used. The codes are stored as binary flags, so Australia's designation of Region 4 is represented by the fourth bit, which is the binary value %00001000. The code shown will play "OzVersion.m2v" whenever a player has Region 4 amongst its approved regions. This means Australia only machines and also multi-zone ones. To have "OverseasVersion.m2v" play on all but "Region 4 only" machines, then you would remove the line "G &= %00001000". This line performs a bit-wise AND which is used to turn off all the bits except the fourth one.

Script to check Region Codes

So that's some of the possibilities provided by DVD scripts. While some mathematical knowledge helps, trial an error should get you there too. Always test scripts on a few different players, as some manufacturers have been a bit lax with their interpretations of the DVD-Video standard.

The author has made every effort to confirm the accuracy of the information in this article, however, it is his opinion only and comes with no guarantees. Please consult your family technologist if in doubt.