AppleScript Code for Converting an AVI to an MP4 in QuickTime Pro

November 19th, 2007

Anther Simple Answer to a Specific Quesion:

As part of my ongoing obsession with converting and storing video on the Mac, one of the obvious tools I’ve looked at for converting from .avi files .mp4 files QuickTime. Assuming you can get your video to play (hint: http://www.divx.com/), Quicktime Pro has an Export feature that produces very acceptable video using the H.264 codec.

Since there didn’t seem to be any mechanism for batch processing, I looked into using Applescript to automate the task. I found numerous examples online that almost did what I needed, but nothing that was exactly right. In every case, the format was wrong, and the script in question did way more than I wanted. So here’s the bare bones, stripped down AppleScript code for…

…saving MPEG4 export settings to an external file:

tell app "QuickTime Player"
    tell first movie
        save export settings for MPEG4 to file "Users:yourname:MPEG4 Settings"
    end tell
end tell

…and converting an .avi file to an .mp4 using a saved external settings file:

tell application "QuickTime Player"
    activate
    close every window
end tell
tell application "QuickTime Player"
    open "Data:Upload:Harvey Birdman - 4x02 - Incredible Hippo.avi"
    if (can export front movie as MPEG4) then
        set theTime to duration of front movie
        with timeout of theTime seconds
            export front movie to ("Data:Upload:Harvey Birdman - 4x02 - Incredible Hippo.mp4") as MPEG4 using settings file "Users:yourname:MPEG4 Settings"
        end timeout
    end if
end tell
quit application "Quicktime Player"

How Documentation Can Impede Development

November 18th, 2007

I’ve had this noble desire to document my development work for a long time, but whenever I try, the first casualty always seems to be productivity.

One of the projects I’ve been working on for a while now, for example, is documentation on encoding DVDs and AVI files for iTunes/AppleTV. So far, I’ve documented my hardware setup, and the method I use for ripping and encoding DVDs, as well as tools I use to convert AVI files to compatible MP4 files. This documentation, though, only accounts for half, maybe a quarter of what I’ve implemented. I’ve got scripts for automating the converting process and moving the files back and forth between several computers, scripts for grabbing Plot, Relase Dates, Ratings and Posters from iMDB and automatically adding them to iTunes. I’ve got scripts for displaying parsing iTunes and uploading all that data to my web page, so I have remote access to view the movies in my collection.

None of this is documented yet. In fact, I find myself reluctant to continue to develop new features and new scripts because I don’t want the divide between the code and the documentation to grow any more than it already has. At the same time, the weight of the documentation is high, the desire do it is low, and the call of Oblivion is strong.

Doing nothing isn’t even a guaranteed method to preventing the Documentation Divide1 from growing. My windows system started to go south, so I was forced to buy a new computer ahead of a schedule (a shiny new Intel-basic Mac mini; I call it Cletus).

Not only does this render most of my existing documentation obsolete (or at least irrelevant), it means that my model changes from a cross-platform, Frankenstein style system running on multiple machines to an integrated application running on a single box. Development gets easier, new features are possible, and my audience goes from three people who happen to be running the same outdated, multi-cultural hardware setup as me to anyone running a halfway decent Mac. The gulf between what’s documented and what I’m running is wider than ever, and finally having the speedy new Mac I wanted probably means it will continue to grow.

… and maybe that’s the lesson here. When I started this blog, I made the slogan “The Blog is Not the Point” as a reminder to myself that this was a web log, and that my projects were more important than usage statistics, getting Dugg, or trying to generate ad revenue. That same slogan can be equally effective in reminding myself that I don’t have to wait for my blog to catch up with the code before I get back to work: the Blog is Not the Point.

  1. Someone make a note of the term “Documentation Divide”. If there isn’t already a phrase for this phenomenon, I want to nominate this. []

How to set a value as “Primary” in the Mac Address Book

October 22nd, 2007

Another Simple Answer to a Specific Question:

As of OS X 10.4.10, at least, the Mac Address has the concept of “Primary” email addresses, phone numbers and street addresses for each contact, but you cannot change them unless you install a plug-in.

My First Digg

October 21st, 2007

The other day, for the first time, I felt like I had something interesting and timely to say, so I submitted the story to Digg. Yeah, it was weak, but I read Digg regularly, and was curious about what would happen. It turns out, not much: I got a few hits and a handful of people yelled at me1. Fortunately, at least a few people thought it was a good idea, because I got a couple of diggs. Which is good, because I avoided that pathetic, I-dugg-myself “1 digg”… which, I now understand, is the real reason one might not want to Digg one’s own work. But I digress…

In retrospect, my title, “Buy a new Mac now to avoid having to pay for Leopard on your old Macs”, was probably my first mistake. Not only does it sound like an ad and sound like piracy, it sounds like an ad about piracy. In my own defense, though, I swear that it didn’t occur to me that I was advocating piracy; I thought I was just pointing out a good deal. But no, let’s be clear: you can only install your copy of OS X on a single computer unless you purchase the family pack.

To be fair, though, I was suggesting that people buy a new Mac, which, at the absolute minimum will run you about $600… and I was suggesting it to people who already own at least one Mac. In other words, even if you’ve already made a significant financial investment, and you make an additional financial investment, it’s still piracy; people will yell at you if you suggest it.

This is why people hate Mac users.

  1. Which actually sounds like the Internet in a nutshell, now that I think about it []

Buy a new Mac now to avoid having to pay for Leopard on your old Macs

October 20th, 2007

If you’re thinking about buying a new Mac, do it now to avoid having to buy Leopard separately to upgrade your older Macs.

I just bought a new mini to replace my ailing Windows PC, and it came with a model-specific install/restore disc labeled “Mac mini: Mac OS X Install Disc”. Had I waited and bought a machine that shipped with Leopard, I wouldn’t be able to use that disc to upgrade my Quicksilver, my Powerbook, or (presumably) my old G4 mini. If I wanted to update those machines, I would be forced to spend $120 on a retail copy.

However, through December 31st Apple is offering a copy of Leopard for $10 to anyone who purchases a new Mac that comes with Tiger. If history is any indication, that disc should work for all models, and you’ll save $110.

Updated 10/21/2007: It’s been pointed out to me that, assuming that the Leopard software license matches the Tiger software license (PDF), installing this on more than one machine would be considered piracy, so I no longer officially advocate this course of action. Apologies to Apple, and to all the people who worked hard to make OS X a great operating system.

Things to Do

October 19th, 2007

My productivity dipped, not surprisingly, after a bout of food poisoning coinciding with the brief lull preceding the start of the fall television schedule prompted me to buy an Xbox 360. In an effort to get back on track, I’ve compiled a to-do list, of sorts, to jump-start my flagging… something.

Finish Video Server series.

I got off to a good start with my series of entries on ripping and encoding files for my iTunes/AppleTV video server, but I’m sort of stuck on what was to be the final entry- adding meta-data to the files to make them pretty. It comes down, in part, to not being sure how to distribute a couple of accompanying scripts. They’re too long to post as part of the entry, so they’ll need to be archived and put up for download as a tar file. The real sticking point, though, is that they need more documentation, and I just haven’t been able to bring myself to do it.

Rework skedevel.com.

Moving skettle.com to skedevel.com was far simpler than I’d hoped, but that was only the first step. I need to reduce the complexity of the site, removing the ill-conceived user-centric directory structure for public facing content, and making everything function privately. In other words, go back to a log-in to use the tools model, and decide after the fact what is publicly accessible. The .site.com model is great for complex social-networking type sites, but since I don’t hve much interest in doing that, the complexity is making everything else three times more difficult. While I’m in there, I also need to streamline the database access and see if I can come up with a way to benchmark the capacity of the site. It would be nice to know exactly what kind of pounding the codebase could take.

Implement OpenID on skettle.com.

I was driving back from Sacramento one night when I had a revelation about how one might implement decentralized centralized user authentication for Websites. When I started looking around to see if anyone was doing what I was thinking about, I found that the guys at sxip.org were doing almost exactly what I had thought of, though there were some issues with the implementation at the time. I carefully weighed my options, and rather than using my copious free time and obvious genius to get involved and fix the perceived problems, I think I elected to watch TV and play video games. Eventually the sxip technology morphed in OpenID, and, for the most part, it seems to work. I implemented one of the early incarnations of the library for the skedevel.com code, and for all I know it still works, but I’ve been less successful finding a good WordPress plugin. One guy seems pretty close, and if he hasn’t released a stable version by the time to get around to working on this, I’ll try playing around with his beta.

Unit testing and monitoring precautionmail.com.

One of my chief goals is to build a codebase I can use for rapid development of new sites. I had two sites in mind when I started, and one of them, precautionmail.com, I already built before realizing that it wasn’t very exciting and not something I had any desire to work on long term. However, that doesn’t mean don’t want to it stay up and running. Since it’s running on the same alpha code that runs skedevel.com, there’s a very real chance that any changes I make on skedevel will break precautionmail, and it’s unlikely I’d notice it for weeks, since, let’s face it, precautionmail is boring, and there’s not a lot of incentive to make sure it’s working properly. Therefore, I need to implement some sort of functionality to monitor that the major functions of the site are working correctly, beyond just some sort of simple pattern matching HTTP check (though that would be a good start). Since fully testing the site involves logging in, writing a message, and verifying that it was delivered after a preset period of time, there could be some fairly major engineering involved.

Find a new WordPress theme.

When I first implemented WordPress, I liked the default theme, and I thought that most people using WordPress would change the theme- thus rendering my site, using the default, kind of original. I don’t know if that’s the case or not, but I have decided that it’s always lame to use the default. Even I were the only one in world doing it, I would still be lame, because using the default is lame almost by definition.

Removing Duplicate Files from iTunes

September 29th, 2007

While iTunes has its issues, for the most part it makes for a fairly passable media organizer. As long as I let it do its job, I’m pretty happy with how it handles the files on the backend. When I moved my iTunes library to a new external drive, however, I came across a pretty egregious “bug” that left me with thousands of duplicate files and no good way to remove them.

By default, iTunes stores files in “/Users//Music/iTunes/iTunes Music”. When you drag and drop a file from your desktop, or wherever, it copies the file into this directory, tossing it into a subdirectory based on Artist and Album name and renaming it according to the track info you specify in iTunes. When you change the Album name or Track name, the file is is updated and moved automatically, so the location and name of the file always reflect what you see in iTunes. Personally, I like this behavior.

I didn’t run into problems until I attempted to move my files from the default directory to a directory on my new drive, a 500G firewire affair. At first, everything worked properly when I changed the settings; it took while, but it slowly copied all the the files to the new location, “/Data/iTunes”, and once it was done, everything was great. And everything remained great until I made the mistake of opening iTunes while my new drive was unmounted.

Little did I know that when iTunes couldn’t find “/Data/iTunes”, the new directory, it changed itself back to its default: “/Users//Music/iTunes/iTunes Music”. Upon finding all the files still there, it quickly settled in and changed all the entries in the database to point to these files. When I finally realized what had happened, I remounted the new drive and changed the iTunes directory again- and that’s when all my troubles began.

iTunes once again began to copy my files to new the drive- only this time, the files already existed. Rather than simply use the existing files, it created new copies, appended with ” 1″, and I was left with several thousand duplicate files and no easy way to remove them.

To deal with this issue, I wrote a simple PHP script to crawl through my iTunes directory and deal with files that end in ” 1″. In the case where both files exist (with and without ” 1″), the script compares the two files. If they are identical, it tries to rectify the situation by removing the old file and re-adding the remaining file to iTunes using osascript, OS X’s command line Applescript tool. Adding the file triggers iTunes to automatically rename the file properly, thus removing the ” 1″. By default, you’re prompted before any file is deleted. You can disable this by hitting ‘a’ at the prompt.

In the case where the two files are not equal, they are skipped. You’re going to have to deal with those manually. Sorry. It also has problems with files that contain special characters. You’ll have to deal with those as well… but come on- how many Blue Öyster Cult, Mötley Crüe or Björk songs do you have, anyway?

In the case where only one file exists, it will simply re-add the file to iTunes, on the theory that iTunes will fix it if the name is wrong, and ignore it otherwise.

fix_itunes_dupes.php

#!/usr/bin/php
<?php
/*
* Name: fix_itunes_dupes.php
* Author: patrick
* Usage: fix_itunes_dupes.php
*
* This script is provided AS-IS.  No warranty is either expressed or implied.
* Use at your own risk.
* Feel free to use, modify, duplicate, or take credit.
*/
// Set this variable to your iTunes directory.
$itunes_dir = "/Users/{$_ENV['USER']}/Music/iTunes/iTunes Music";

if (!file_exists($itunes_dir)) {
    exit("{$itunes_dir} does not exist.");
}

// Collect itunes files that end in " 1".
$find = "find \\\\"{$itunes_dir}\\\\" -type f -name \\\\"* 1.*\\\\"";
$files = explode("\\n", `$find`);
$file_count = count($files) - 1;
// 'find' comes with a bonus carriage return.  Get rid of it or try to remove
// '.' (that's bad).
unset($files[$file_count]);
// Set some defaults.
$all = false;
$count = 0;

// Loop through the files.
foreach ($files as $file1) {
    $count++;
    print "Processing {$count}/{$file_count}:\\n";
    // Skip Movies and TV Shows.
    if (preg_match("/\\/Movies\\//", $file1) || preg_match("/\\/TV Shows\\//", $file1)) {
        print "Skipping TV Show or Movie:\\n";
        print "{$file1}\\n";
    } else {
        preg_match("/^(.+) [0-9].([^.]+)$/", $file1, $matches);
        $file = "{$matches[1]}.{$matches[2]}";
        if (file_exists($file)) {
            $md5 = md5(file_get_contents($file));
            $md51 = md5(file_get_contents($file1));
            print "{$file} ({$md5})\\n";
            print "{$file1} ({$md51})\\n";
            if ($md5 == $md51) {
                // If the two files are identical, we can space one of them.
                print "{$file} = {$file1}\\n";
                if (!$all) {
                    // Options are:
                    //  y - delete the file. default.
                    //  n - skip this file.
                    //  a - stop asking, just do them all.
                    //  q - quit.
                    print "Delete? [Y/n/a/q]: ";
                    $char = substr(fgets(STDIN), 0, 1);
                }
                if ($char == "q") {
                    exit;
                } elseif ($char != "n") {
                    if ($char == "a") {
                        $all     = true;
                    }
                    print "Deleting {$file}\\n";
                    unlink($file);
                    add2itunes($file1);
                }
            } else {
                print "Files do not match.  Skipping.\\n";
            }
        } else {
            // There is only one file; go ahead and add it to iTunes-
            // shouldn't hurt.
            print "{$file1} exists on its own.\\n";
            add2itunes($file1);
        }
    }
    print "\\n";
}

function add2itunes($file) {
    $file = preg_replace("/\\//", ":", $file);
    // Please forgive the toothpicks- numerous files have 's.
    $command = "osascript -e \\\\"tell application \\\\\\\\"iTunes\\\\\\\\" to add file \\\\\\\\"{$file}\\\\\\\\" to playlist \\\\\\\\"Library\\\\\\\\" of source \\\\\\\\"Library\\\\\\\\"\\\\"\\n";
    print $command;
    passthru($command);
    return;
}
?>

Sample Output

Processing 31/39:
/Data/iTunes/The Beastie Boys/Licensed To Ill/13 Time to Get Ill.m4a (42cbc19b9d2feb83899201793ee1e00e)
/Data/iTunes/The Beastie Boys/Licensed To Ill/13 Time to Get Ill 1.m4a (e2f49dfc12db5fb8f8d3954b9671870c)
Files do not match.  Skipping.
Processing 32/39:
/Data/iTunes/The Bloodhound Gang/One Fierce Beer Coaster/07 Asleep at the Wheel.mp3 (6d12ce65c4cba6748224e2262feba67a)
/Data/iTunes/The Bloodhound Gang/One Fierce Beer Coaster/07 Asleep at the Wheel 1.mp3 (6d12ce65c4cba6748224e2262feba67a)
/Data/iTunes/The Bloodhound Gang/One Fierce Beer Coaster/07 Asleep at the Wheel.mp3 = /Data/iTunes/The Bloodhound Gang/One Fierce Beer Coaster/07 Asleep at the Wheel 1.mp3
Delete? [Y/n/a/q]: y
Deleting /Data/iTunes/The Bloodhound Gang/One Fierce Beer Coaster/07 Asleep at the Wheel.mp3
osascript -e "tell application \"iTunes\" to add file \":Data:iTunes:The Bloodhound Gang:One Fierce Beer Coaster:07 Asleep at the Wheel 1.mp3\" to playlist \"Library\" of source \"Library\""
file track id 53184
Processing 33/39:
Skipping TV Show or Movie:
/Data/iTunes/TV Shows/Battlestar Galactica/3-03 Exodus_ Part 1.mp4