Help - Search - Members - Calendar
Full Version: How to wait for iTunes to complete conversion
Hydrogenaudio Forums > Lossy Audio Compression > AAC > AAC - General
sehested
Here is a Perl iTunes example. It adds Brain Damage to iTunes, using whatever preference is set. (soundcheck / copy file to music folder according to your settings).

CODE

use Win32::OLE;

$file = "F:/Work/Eminem - Brain Damage.mp3";

printf "Trying to add %s to iTunes\n", $file;

$result = iTunesAdd( $file);

printf "Result %d\n", $result;

sub iTunesOpen {
 my $failure;
 $iTunesApp = new Win32::OLE( "iTunes.Application") or $failure = 1;
 if ($failure) {
   printf "Failed to launch iTunes application through OLE\n";
 } else {
   $iTunesVersion = $iTunesApp->Version;
   printf "OLE connection established to iTunes application $iTunesVersion\n";
   $iTunesOpen = 1;
 }
}

sub iTunesAdd {
 my $file = shift @_;
 my ($mainLibrary, $operationStatus, $trackCollection, $count);

 iTunesOpen( ) unless $iTunesOpen;
 return undef unless $iTunesOpen;

 $mainLibrary = $iTunesApp->LibraryPlayList;
 $operationStatus = $mainLibrary->AddFile( $file);
 unless (defined $operationStatus) {
   printf "File not accepted by iTunes \"%s\"\n", $file);
   return 0;
 }
 sleep 1 while $operationStatus->InProgress;
 $trackCollection = $operationStatus->Tracks;
 $count = $trackCollection->Count;
 printf "Failed to add file to iTunes \"%s\"\n", $file unless $count;
 return $count;
}



Perl is a programming language available for Unix, Mac, Windows, Linux etc.

I use Perl 5.8 available from activestate:
ActiveState Perl

Furthermore the Apple iTunes OLE interface is built-in to iTunes 4.5 and the documentation can be found here:
Apple iTunes scripting SDK

Another example script I made is itunes-rating-from-musicmatch that scan through all tracks in iTunes, read their Music Match rating from the MP3 tags in the file and if applicable, set the iTunes rating accordingly.
FrDakota
You probably don't know it, but since iTunes 4.6 (Library 1.1) it seems now to be possible to poll the status of the conversion.

You should use the '2' extension the the convert functions you're using.

ConvertFile becomes ConvertFile2 and you poll ConvertOperationStatus instead of OperationStatus. (Don't know yet how to do this in JS, but I'll manage.)

BTW, I'm using at the end of a JScript,

i = iTunesApp.Quit;

But I get an error, object doesn't like the property or method, any insight?

Edit : WOW, my joining HA birthday today... 1 year, and no candle emoticon. tongue.gif
Tec9SD
Thanks for the contribution, sehested.
I'm sure someone will get use from this. smile.gif

I didn't even know iTunes could be scripted. Maybe those encoding with iTunes or QuickTime will be able to improve on Qutibacoas.

tec

Edit: Perl ... wo0t
Otto42
Yeah, I posted a script a month or so ago to encode from a command line, but I didn't see the OperationStatus thing. I'm not using iTunes 4.6, however, because it breaks usage of hymn at the moment. So this trick is still kinda useful.

Here's my modifed script. Save it to "encodeAAC.js" and run it on the command line by doing "encodeAAC.js filename.wav". It should wait for the encode to finish and then output the resulting filename after the encode is done. I have not tested yet, however. Might work with EAC. I think EAC expects to be able to specify the output filename though, so that could be added by simply copying the resulting file to the file EAC wants it to be..

CODE

args = WScript.Arguments;
if  (args.length != 1)
{
WScript.Echo("Usage: encodeAAC.js <filename.wav>");
}
else
{
var iTunesApp = WScript.CreateObject("iTunes.Application");
var  encoderCollection = iTunesApp.Encoders;
var encoder=encoderCollection.ItemByName("AAC Encoder");
iTunesApp.CurrentEncoder = encoder;
var  fso = new ActiveXObject("Scripting.FileSystemObject");
var  fullpath = fso.GetAbsolutePathName(args(0));
var  opStatus = iTunesApp.ConvertFile(fullpath);
while (opStatus.InProgress) { WScript.Sleep(1000); }
Wscript.Echo(opStatus.Tracks.Item(1).Location);
}


Modified version to take an output filename as well, since I think EAC expects it.

CODE

args = WScript.Arguments;
if  (args.length != 2)
{
WScript.Echo("Usage: encodeAAC.js <input.wav> <output.m4a>");
}
else
{
var iTunesApp = WScript.CreateObject("iTunes.Application");
var  encoderCollection = iTunesApp.Encoders;
var encoder=encoderCollection.ItemByName("AAC Encoder");
iTunesApp.CurrentEncoder = encoder;
var  fso = new ActiveXObject("Scripting.FileSystemObject");
var  fullpath = fso.GetAbsolutePathName(args(0));
var  opStatus = iTunesApp.ConvertFile(fullpath);
while (opStatus.InProgress) { WScript.Sleep(1000); }
var outfile = fso.GetFile(opStatus.Tracks.Item(1).Location);
outfile.copy(args(1), true);
}


Again, all untested, I'm writing by the seat of my pants here. wink.gif
Latexxx
A little modification for the script can be found at http://www.hydrogenaudio.org/forums/index....ndpost&p=217867 .
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.