Automating QuickTime at the Command Line on Windows…
June 26th, 2006 by Luc-Eric - Viewed 17307 times - Popularity: 30%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
If you’re playing with the script and get an Unspecified Error on line 21, try manually launching the QuickTime Player manually before running the script. commenter Nathang wrote:
I fixed the “C:\qttest\qttest.js(21, 1) (null): Unspecified error ” problem by….
//under here:
var qtPlayerApp = WScript.CreateObject(”QuickTimePlayerLib.QuickTimePlayerApp”);
//Add:
WScript.Sleep(7000)This give quicktime the chance to launch properly
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!
// 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();





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. :)
is quicktime 7 installed?
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!
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?
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.
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.
what is the exact command line that you are typing when you are using the script, and which codec are you using
Is it possible to use the same script for converting mpeg 4 to 3gp?
Or what needs to be changed to make it work?
How can I setup 3gpp exporter instead of QT Movie??
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.
when I run it, it opens a quicktime movie with the correct size and appearently length, but the image is blank
When I run the script, it converts the quicktime startup screen into a 160KB mov.
I tried 10 .pic files at pal and 1080.
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?
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
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 ..?
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!
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?
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
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.
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
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
Thank You Thank You Thank You!
I ‘ve been looking all over for such a script! — Would you be kind enough to let me know how I may change it a bit so to export a movie to a image sequence? And how does one add in one more little thing: frames-per-second in the sequence generation?
By the way, I also ran into the
“C:\qttest\qttest.js(21, 1) (null): Unspecified error ”
problem. I am on Vista Ultimate, I already tried it with administrator privilege but with no success.
Thank you again!
I fixed the “C:\qttest\qttest.js(21, 1) (null): Unspecified error ” problem by….
//under here:
var qtPlayerApp = WScript.CreateObject(”QuickTimePlayerLib.QuickTimePlayerApp”);
//Add:
WScript.Sleep(7000)
This give quicktime the chance to launch properly.
//I’ve changed this so that it can take a mov as an input and output a tga sequence if you want.
// to run from the command line :
// cscript //E:jscript jsQTtest.js sourcepath, destpath
// Get script arguments
if (WScript.Arguments.Length >= 2) {
sourcePath = WScript.Arguments(0);
destPath = WScript.Arguments(1);
temp = sourcePath.split(’.');
sourceExt = temp.pop();
temp = destPath.split(’.');
destExt = temp.pop();
WScript.Echo(”Source File extension is :”+sourceExt);
WScript.Echo(”Dest File extension is :”+destExt);
} else {
WScript.Echo(”not enough parameters”);
WScript.Quit();
}
// Launch QuickTime Player Application
var qtPlayerApp = WScript.CreateObject(”QuickTimePlayerLib.QuickTimePlayerApp”);
WScript.Sleep(2000)
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.Echo(”Null”);
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);
if (destExt == “tga”){
var CodecInfoFileName = “C:\\QuickTimeCodecInfoTGA.xml”;
}else{
var CodecInfoFileName = “C:\\QuickTimeCodecInfo.xml”;
}
var FileSystemObject = WScript.CreateObject(”Scripting.FileSystemObject”);
var CodecFileInfo;
if ( FileSystemObject.FileExists(CodecInfoFileName) )
CodecFileInfo = FileSystemObject.OpenTextFile( CodecInfoFileName );
if (destExt == “tga”){
qtExporter.TypeName = “Image Sequence”
}else{
qtExporter.TypeName = “QuickTime Movie”
}
if (sourceExt == “tga”){
qtControl.CreateNewMovieFromImages( sourcePath,
25, // frame rate
true ); // rate is in frames per seconds
WScript.Echo(sourcePath)
}else if(sourceExt == “mov”){
qtPlayerSrc.OpenUrl(sourcePath)
WScript.Echo(sourcePath)
}
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();
hello,
sorry i’m a great newbe.
How to get quicktime codec in XMl form?
or How to export them?
thank you very much.
Running the script on com seems okay but once dialog box of QT appears a video preprocessing window appears and i’ve waited… waited… waited… ei btw dude does it take a while to process a .avi file? is it supported? Thanks.
Oh it’s working but no audio and video once played…
Do you know if there is a way to suppress the UI when invoking Quicktime like this? I notice the script fails when I attempt to run it remotely via rcmd, presumably because there is no display available.
–SNIP–
H:\scripts>rcmd \\render1 cscript c:\temp\still2qt.js c:\temp\file_101.sgi c:\temp\test.mov
Executing on \\render1 cscript c:\temp\still2qt.js c:\temp\file_101.sgi c:\temp\test.mov
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
Launched Quicktime
c:\temp\still2qt.js(32, 1) Microsoft JScript runtime error: The remote server machine does not exist or is unavailable
Remote server \\render1 disconnected
H:\scripts>
–SNIP–
Line 32 in my copy of your script is:
var qtPlayerSrc = qtPlayerApp.Players(1);
Cheers for this post, regardless. Very nicely done.
[...] the QuickTime conversion? I installed the QuickTime SDK, went looking for examples, and found just what the doctor ordered. Thanks, [...]
Hello Guys,
I’ve been working on this converter project for quite sometime now. Somehow I managed to have it running but I’m having trouble in setting the frame rate, dimension and etc. The xml doc shows no tag for it and the DTD of quicktime also seem to miss that definition. Please see http://www.apple.com/DTDs/PropertyList-1.0.dtd.
Once media to convert is uploaded to server, a node of my converter program is being invoked and it performs the conversion job. In light with that, let us say you have uploaded three videos then three nodes are being run to convert the video. After conversion you may download the file from “view job status” section of my upload page.
Help is very much appreciated. Thanks guys!
Also note that quicktime will not create an image sequence if one of the images is 0k. If you delete the 0k images, it will run fine.
Sorry Alexis D., the role of the XML file is explained in the article and you cannot use it to set codec attributes programmatically. These properties are part of the codec’s binary blob. As mentionned in the article, try Quicktime Mailing list to get help on programming topics.