Automating QuickTime at the Command Line on Windows…
June 26th, 2006 by Luc-Eric - Viewed 12259 times - Popularity: 15%QuickTime is in mutation. Let’s quote from Apple’s technote 2140 about the recent API changes :
“QuickTime has been around for some time now and has a deep rich history. First introduced in May 1991 and originally available for System 6.07 and System 7, it was designed as a full media architecture and offers a complete range of services some of which include: media creation, editing, capture from external devices, delivery via the web, streaming, and of course playback.
The upshot of this is an absolutely huge API, consisting of more than 2500 functions. The API uses a large chunk of the old MacOS API, which is now obsolete with OS X, so it’s time for house cleaning, and that brings us new functionnality under Windows.
Fun fact: The QuickTime API is so dependant on Mac OS data structure that the Windows port of QuickTime necessarily had to port part of the MacOS toolbox API to Windows. This port is so complete that a few developers, including the people behind Commotion, have used QuickTime as a “Macintosh emulator” to port their application onto Windows. Which you shouldn’t consider doing under any circumstance.
The QuickTime engineers have a problem. The API is huge and difficult to get into, various parts are obsolete and replaced by newer ones and therefore it is error-prone, and it needs to get away from the old MacOS API.
For OS X developers, Apple has made QtKit for Objective C developers so that normal people (as normal as a person who choses Object C can be, anyway) can easily add QuickTime support to their app.
At about the same time, for Windows QuickTime 7.0 Apple has done the equivalent and added a new COM/ActiveX Control. This is not to be confused with the control that’s used in the Web Browser, it’s something totally different that is meant to be used in Visual Basic, C#, and C++ and other COM-enabled languages.
This new access to QuickTime is mostly meant for application developers in these programming languages, however it is possible to access these COM objects from ActiveScripting, so this means VBScript, JScript which come with Windows, and others like Python and Perl.
One little known fact is that you can run VBScripts and JScripts at the command-line on any Windows 2000 and XP machine. The command-line intepreter is called “cscript.exe” and is located in your “Windows\System32″ directory. “wscript.exe” is a windowed equivalent which will pop message boxes instead of displaying messages at the command line. If for some reason you don’t have it installed, it’s a free download.
The scripting interface to QuickTime is still largely undocumented, however you can check out the methods and properties with an OLE object viewer. I’ve also found some good examples on the web site for the book QuickTime for .NET and COM Developers , in the (Chapter 8) samples.
Time for some code.
I’ve created a script below which will convert an image sequence to a QuickTime movie.
The only hard part was the issue with codec settings. As you may know from XSI and other products, codec settings are a binary blob of data, and are not suitable to be set with a script, or nice human-readable properties: you have to go through the codec-specific custom user interface. However the QuickTime control does allow to save codec settings into an XML file, to be re-applied later. Again, the data is still not human-editable in this form — the XML file contains Base64 data — you will need to use the codec dialog to change the settings. But at least you only need to pick it interactively once and can re-use the XML file for batch processing.
The first time you run my script, you’ll pick the codec with the dialog box, and these settings will then be saved into an XML file on the C:\ for the next time. Only known issue: the UI for the QuickTime player will apear during the export process, but that shouldn’t cause any problem, the script doesn’t return until the task is done.
QuickTime detects image sequences automatically when you give it an image file with a number in it. To run the script, type something like
cscript jsQTtest.js D:\clock001.pic C:\clock.mov
To get access to the “Import Image Sequence” in QuickTime 7.0, normally you need to upgrade to QuickTimePro, but I’ve tested my script after disabling my QuickTime Pro license and it seems to work anyway.
Here is the script… click on the image to download it.
credits : I’ve received some from the QuickTime API mailing list on apple.com to resolve a codec setting issue, and I’ve used the samples from the book above.
This script converts images sequences to movies. It will exit and do nothing if you try to convert a movie to another movie Try changing the call to CreateNewMovieFromImages to CreateNewMovie to do that!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | // to run from the command line : // cscript jsQTtest.js sourcepath, destpath // Get script arguments if (WScript.Arguments.Length >= 2) { sourcePath = WScript.Arguments(0); destPath = WScript.Arguments(1); } else { WScript.Echo("not enough parameters"); WScript.Quit(); } // Launch QuickTime Player Application var qtPlayerApp = WScript.CreateObject("QuickTimePlayerLib.QuickTimePlayerApp"); if (qtPlayerApp == null) { WScript.Echo("Unable to launch QuickTime Player!"); WScript.Quit(); } // Get the QuickTime controler var qtPlayerSrc = qtPlayerApp.Players(1); if (qtPlayerSrc == null) WScript.Quit(); var qtControl = qtPlayerSrc.QTControl; // Set up the exporter and have it configured var qt = qtPlayerSrc.QTControl.QuickTime; qt.Exporters.Add(); var qtExporter = qt.Exporters(1); var CodecInfoFileName = "C:\QuickTimCodecInfo.xml"; var FileSystemObject = WScript.CreateObject("Scripting.FileSystemObject"); var CodecFileInfo; if ( FileSystemObject.FileExists(CodecInfoFileName) ) CodecFileInfo = FileSystemObject.OpenTextFile( CodecInfoFileName ); qtExporter.TypeName = "QuickTime Movie"; qtControl.CreateNewMovieFromImages( sourcePath, 30, // frame rate true ); // rate is in frames per seconds var qtMovie = qtControl.Movie; qtExporter.SetDataSource( qtMovie ); if ( CodecFileInfo ) { var xmlCodecInfoText = CodecFileInfo.ReadAll(); // cause the exporter to be reconfigured // http://developer.apple.com/technotes/tn2006/tn2120.html var tempSettings = qtExporter.Settings; tempSettings.XML = xmlCodecInfoText; qtExporter.Settings = tempSettings; } else { qtExporter.ShowSettingsDialog(); var xmlCodecInfoText = qtExporter.Settings.XML; CodecFileInfo = FileSystemObject.CreateTextFile( CodecInfoFileName ); if ( CodecFileInfo ) { CodecFileInfo.WriteLine(xmlCodecInfoText); CodecFileInfo.Close(); } } // do the actual export qtExporter.DestinationFileName = destPath; qtExporter.ShowProgressDialog = true; qtExporter.BeginExport(); qtPlayerSrc.Close(); |





July 19th, 2006 at 6:10 am
hi, what does it mean, after trying to execute the script, winxp commandline tells me:
c:\jsQTTest.js(14, 1) WScript.CreateObject: Could not locate automation class named “QuickTimePlayerLib.QuickTimePlayerApp”. ?
what does that point to?
thanks. :)
July 19th, 2006 at 10:01 am
is quicktime 7 installed?
August 29th, 2006 at 10:01 pm
Hi, I just ”ported” your script to PHP5, and well it works nice. However… did you notice that the Flash 8 export is unavailable? this is really strange. I wanted to automate FLV8 encoding!
September 23rd, 2006 at 9:25 am
I tried your script, and it created a QuickTimeCodeInfo.xml file.
It does not, however, create a movie, and I do not get any error messages.
Is there a way of finding out why it goes wrong?
September 23rd, 2006 at 4:22 pm
does it work with the Quicktime Animation Codec? That’’s the simplest codec there is. I am not sure what can fail, my script doesn”t do error reporting. Perhaps a codec-specific setting is wrong? Some codecs, for example, only accept images that are of size NTSC or PAL.
If you have quicktime pro try to do it manually in the user interface of the quicktime player to see if you get error messages.
September 24th, 2006 at 4:01 am
I tried and I managed to create a movie interactively. The attempt to do the same with yous script failed.
The script just stops, and no movie is created.
September 25th, 2006 at 10:37 am
what is the exact command line that you are typing when you are using the script, and which codec are you using
October 7th, 2006 at 8:31 am
Is it possible to use the same script for converting mpeg 4 to 3gp?
Or what needs to be changed to make it work?
October 16th, 2006 at 1:46 pm
How can I setup 3gpp exporter instead of QT Movie??
October 16th, 2006 at 4:01 pm
Converting to another type than quicktime is left as an exercise to the reader. On line 40, above, it specifically sets the type of the export to ”Quicktime Movie”. One could theorize that if an other typename is used, another type of exporter object will be created.
November 23rd, 2006 at 3:36 pm
when I run it, it opens a quicktime movie with the correct size and appearently length, but the image is blank
December 8th, 2006 at 6:11 pm
When I run the script, it converts the quicktime startup screen into a 160KB mov.
I tried 10 .pic files at pal and 1080.
February 12th, 2007 at 7:07 pm
I couldn’t get this to export anything, so after
qtControl.CreateNewMovieFromImages( sourcePath,
30, // frame rate
true ); // rate is in frames per seconds
I added
if (qtControl.Movie == null) {
WScript.Echo(”Unable to create movie from images!”);
}
Movie does in fact come out to be null- but is that in fact a valid test at that point?
February 12th, 2007 at 7:09 pm
I was also thinking that this could be scripted with something like:
qtPlayerSrc.DoMenu(”File”, “Open Image Sequence…”);
and then somehow fill in the file name in the dialog box and finally
qtPlayerSrc.DoMenu(”File”, “Save”);
and fill in a name.
What’s the DoMenu equivalent of filling in dialog boxes?
Thanks
March 16th, 2007 at 2:36 pm
Hello Luc:
Thanks luc, it works like a charm. I was wondering if its possible to create a video layer (to stamp/watermark with an image) to create the .mov file ..?
May 16th, 2007 at 12:16 pm
Hi guys,
I’ve been spending some hours, just to find out that it’s pretty easy to load a videoclip:
except: qtControl.CreateNewMovieFromImages( sourcePath, 25, true );
write: qtControl.URL = sourcePath;
worked fine using an uncompressed avi - no idea about other file types, though.
Together with
qtExporter.TypeName = “3G”;
I’ve built a 3GP CLI encoder based on Quicktime - something I wanted for several years now. Thanks!
July 9th, 2007 at 1:12 pm
Hey
This is a very good example but does anyone know how to rotate the movie (or access the movie matrix for rotating) before exporting?
August 19th, 2007 at 3:09 pm
Hi,
Thanks for posting this handy code.
I wanted to mention that I ran into into what seems to be a bug and the workaround I found, in case it is messing anyone else up.
Here’s the problem: when I load a folder of images using the “CreateNewMovieFromImages” call, it only loads about 3 images out of a folder of 1500. If I delete some of the images or load a different folder, it does the same thing but with a different small number of images.’
The images I am using have super long filenames and when I rename them to have short filenames there is no problem loading the folder.
So perhaps it has something to do with the length of names permitted on macs that they haven’t fixed in the windows version. Anyway, if you only get short movies that might be the problem.
Another useful thing to know — you can pass QT a list of a folder of shortcuts to your images rather than the actual files which is handy if you are building movies out of images in multiple locations.
Thanks!
Tim
August 20th, 2007 at 3:18 pm
Hi
I had a an issue I was trying to solve with poor quality audio from my digital camera and I thought this approach to extracting the audio from my .mov on the command-line looked promising but came up against:
C:\qttest\qttest.js(21, 1) (null): Unspecified error
Regards
Richard.
November 14th, 2007 at 9:39 am
Hello,
Thanks for the script,
however when i try to run it i end up with the same error as above. The line where the error happens is: var qtPlayerSrc = qtPlayerApp.Players(1);
Thanks in advance for your help.
Regards
Sergey
June 30th, 2008 at 7:33 pm
Great code snippet! I tried it out and it works great when converting a sequence of BMPs I have into an MOV.
Question for you — Do you know if it is possible to add an audio track (i.e. a WAV or MP3) to the creation of the MOV (obviouly adding an extra bit of code to your .js)? If so, how would I do that within the QuickTime API? I’ve been searching thru the QT API at developer.apple.com, but haven’t come across anything yet. Any suggestions or directions you could point me to would be greatly appreciated!
Thx for your help!
-Dan