Import Podcasts into iTunes

Import Podcasts into iTunes

Use Perl scripts and iTunes' COM interface to automate MP3 importing on Windows; on the Mac, it's even easier.

iTunes for Windows supports a powerful scripting layer using Windows COM interfaces. ActiveState Perl ( complements thisby making it easy to automate COM objects. I merged these two together to build a Perl script that complements the command-line podcatcher [Hack #3].

The podcatcher downloaded all the enclosure files into an enclosures directory that was organized by show. This means that the script in this hack needs to crawl the directory structure to find the MP3s to import them, and then update your iPod. This is not a problem, though, since the File::Find module [Hack #7] makes searching through a tree of directories a snap.

With the problem of finding the files out of the way, the script uses the Win32::OLEmodule to tell iTunes to import the files.

The Code

Save this code to a file named

   #!perl -w
   use Win32::OLE;
   use File::Find;
   use strict;

   # iTunes refers to importing as 'converting' in its COM interface.
   # So we convert each path.

   sub convert($$)
     my ( $itunes, $path ) = @_;

     # Make sure it's an MP3 file

     return unless ( $path =~ /[.]mp3$/ );

     print "Converting $path\n";

     # Start the conversion

     my $progress = $itunes->ConvertFile2( $path );

     # Monitor the SLOW progress

     my $done = 0;
     my $lp = -100;
     while( !$done )
       my $p = int( $progress->ProgressValue( ) );
       my $m = int( $progress->MaxProgressValue( ) );

       if ( $m > 0 )
         my $percent = int( ( $p / $m ) * 100.0 );
         $percent = 0 if ( $percent < 0 );

my $delta = $percent - $lp;
         if ( $delta >= 5 )
           print "\t$percent%\n";
           $lp = $percent;
         sleep( 2 );
        $done = 1 if ( $p >= $m );
      print "\n";

   # Make sure we get a real path

   my $searchpath = $ARGV[0];
   die "Must specify search path\n" unless ( $searchpath );

   # Start up iTunes

   my $itunes = new Win32::OLE( "iTunes.application" ); 
   die "Could not open iTunes\n" unless ( $itunes );

   # Convert all of the files
   find( sub { convert( $itunes, $File::Find::name ); }, $searchpath );
   # Update your iPod. Comment this out if you don't have an
   # iPod, or don't want it automagically updated.

   $itunes->UpdateIPod( ); 

   # Quit iTunes

   $itunes->Quit( );

The script itself is very simple. It walks the directory that was specified as an argument on the command line to find MP3 files. For each MP3 file, it calls the ConvertFile2 method on the itunes object. This method returns a progress object that is polled to see how far the conversion has gone. On long files, this conversion process takes a whilea long whileso the script puts up a percentage indicator that tells us how much of the file it has imported.

As a bonus, the script tells iTunes to update the iPod after importing the files, which pushes the podcasts onto the player. You can comment out that line if you don't have an iPod or don't want to do automatic updating.

Running the Hack

I recommend running this script after the command-line podcatcher [Hack #3]. You can automate that by building a batch file that runs the podcatcher first, and then runs this import script. On Windows XP, you can specify that you want the script run at a specific hour of the day or week using Schedule Tasks, which is located in the System Tools section of the Start Programs Accessories menu.

On a Macintosh

On a Macintosh, you can use AppleScript or Perl to do the importing. Save this Perl script as

     #!/usr/bin/perl -w
     use Mac::Glue;
     use Mac::Path::Util;
     use Cwd 'abs_path';
     use strict;

     # Convert the path to an absolute Mac path

     my $macpath = Mac::Path::Util->new( abs_path( $ARGV[0] ) );

     # Open iTunes and convert the contents of the path

     my $itunes = Mac::Glue->new( 'iTunes' );
     $itunes->convert( $macpath->mac_path( ) );

This script requires that theMac::Glue and Mac::Path::Util modules are installed [Hack #7] on your system. Both of these modules are available through CPAN ( Mac::Glue is very cool; you can use it to drive any Macintosh application that supports AppleScript directly through Perl syntax.

Use the Terminal to run the script this way:

% perl /Users/jherr/mymp3s

This will import the directory you specify into iTunes.

See Also

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows