How to generate a dynamic SMIL based .mov using PHP

A SMIL document is a file that describes a set of media and how it should be presented. For the sake of this application, we’re going to use SMIL as a playlist.

You can use SMIL to create a single file that will play multiple files in sequence. You can use php to generate a SMIL document dynamically .. altering the contents of the SMIL (and its playlist) based on any number of things. Further you can tell PHP to output this SMIL document as a QuickTime .mov file (Which is handy in all sorts of situations).

If you want the SMIL to be sent out as a .mov file the first step is to tell PHP that the content-type of the page its generating is a quicktime video. You do that with the following code:

<?php
header("Content-Type: video/quicktime");
?>

Next you want to output the SMIL document. Because we ended the php code block above (with ‘?>’) we can now just type out the begining (and always the same) part of the SMIL document directly.

SMIL documents usually start with “<smil>”. However because we’re creating a .mov we need to tweak that a bit. QuickTime Player will only recognize a MOV as a SMIL document if it begins with “SMILtext”. So we start our document with:

SMILtext<smil>

Now we want to output the header portion of the SMIL document. This part describes the layout of the SMIL document. Now, we only need one viewing area for this exercise. SMIL calls a viewing area a “region”, and you can have multiple regions on the overal canvas. (SMIL calls the canvas the “root-layout”) Since we only need one region (viewing area) on our canvas (root-layout), you can see that I’ve set our region (viewing area) to be 100% of the canvas (root-layout).

<head>
<layout>
<root-layout id="vlogsphere" width="480" height="270" background-color="black"/>
<region id="r1" width="100%" height="100%" fit="meet" background-color="black"/>
</layout>
</head>

With that out of the way we can get on to building out the playlist. First we start the body of the SMIL document and tell it that a sequence of media files is to be listed

<body>
<seq>

Now we just add the list of media files. Lets say that you want to start each play list with a particular graphic. You just list the image file and assign it to the viewing area (region) we defined above.

<img src="http://www.slackspace.net/psb.jpg" region="r1" background-color="black"/>

Now at this point we continue listing the media to display in order. Video files are added in the same way we added the image above, excpet we use “video” instead of “img”. For example a video entry might look like:

<video src="http://blip.tv/file/get/DavidMeade-VloggerShrine307.mov" region="r1" background-color="black"/>

So at this point we go back to PHP code and get a list of all the videos we want to play and print out an entry for each one as shown above. You can get this list of videos however you like (Database calls, parsing RSS feeds, etc). You might even take variables from the query string to dynamicaly alter what media files your program goes out to get.

Once you have created an entry for each media file, you can close out the php code block and go back to writing normal SMIL. Now we have to tell the SMIL document that we’re done listing the sequence of files, that we’ve reached the end of the body of SMIL document, and indeed we’ve reached the end of the SMIL file itself:

</seq>
</body>
</smil>

Thats it!

Wanna see an example? Here’s a link to a php page I made that generates a SMIL based quicktime file.

http://www.slackspace.net/dynamic-media/smil.mov

When you go to that page, it creates a SMIL file whose media content is parsed from an RSS feed of recent videos posted to the ‘videoblogging’ category at blip.tv. The contents of this SMIL will change based on the contents of that RSS feed. As new videos are add at blip.tv, new videos will play in the above SMIL document.

You can also tell the above page to ues a different RSS feed. Lets say you wanted to view all the videos from DavidMeade.com (and who wouldn’t want to do that?!) You can go to the same url, but pass it the url of the feed you want the SMIL document to use:

http://www.slackspace.net/dynamic-media/
smil.mov?feed=http://www.davidmeade.com/feed
(no line break)

Experiment with various feeds. I’ve had a good time watching my MeFeedia queue with this SMIL document.

Because the file output by the PHP code is a Quicktime Movie file, you can use this url in places that normally expect a .mov. For example I’ve used the above url to watch videoblogs in Second Life!

So you may have noticed that my file ends in .mov. Typically php files end in .php. Either will work here, however if you want or need to have the php file actually have a .mov extension, you may be wondering “How did you get a php page to end with .mov?!”

All you have to do is tell your webserver that files that end in .mov are php files. This is actually something you dont want to do for your whole website. I however told it that .mov files within a specific directory should be treated as php files. (All other .movs will be regular .mov files)

How to do that depends on your webserver for for apache server, you just create a directory and include a .htaccess file in that directory. the .htaccess file should contain the following line:

AddHandler application/x-httpd-php mov

And that should do it.

Update: See the comments if you are having issues with QuickTime X

  • this is really cool, I only understond about 3 words but it’s really cool……looks like a neat way to view multiple feeds at once…..hmmm

  • For me… this worked if I went and opened up the QuickTime player. But didn’t work in Firefox with the QuickTime plug-in.

    This is pretty kool though. I didn’t know you could do this.

    – Charles

  • Wow. This is the greatest! And, and…I think I kinda get it. Can\’t wait to try it out.

  • Amazing – fantastic discovery – kudos.

  • This is broken on QuickTime X systems! Dynamically generated QuickTime media links don’t work either — all you get is the broken “Q” logo with a question mark. Static SMIL and reference movies work fine, though.

    I sent a comment to Apple about this. If anyone has a workaround for PHP/SMIL/QTX, please share!

  • I recently discovered SMIL wasn’t working in QTX as well. The reason, I found out, is that QuickTime X still leverages QuickTime 7 for many tasks.

    Not all functions are released in QuickTime X yet. So on a QuickTime X system you need to have QuickTime 7 installed ALSO. The system will then choose if it should use QuickTime X or 7 based on if the action required is supported in QuickTime X yet.

    For more info check out this great review of QuickTime X and how it still leverages QuickTime 7: http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars/6

    (thanks to Michael Verdi for pointing me to this article)

    I hope this help, Jim; and thanks for stopping by and leaving a comment!

  • Thanks Dave. With your advice, I got it working. Here is what I had to do:

    1) Manually install QuickTime 7 from the Snow Leopard installation DVD. Instructions on how to do this are here:
    http://support.apple.com/kb/HT3678

    2) Manually re-install the latest Mac OS X 10.6.2 Combo Update, which you can download from here:
    http://support.apple.com/kb/DL959

    3) Additionally, after restarting, I found that I had to fully shut down (power off) my computer once more, then start it again, before the videos would play correctly.

    I’m not sure if all of these steps are necessary — and I’m not sure exactly what I accomplished with step #3 — but this is what I did, and dynamic SMIL files appear to work as intended now.

You can follow any responses to this entry through the RSS 2.0 feed.