Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

QTKit duration in Snow Leopard Server

Jerry Stratton, February 8, 2011

In Snow Leopard, the duration method returns a three-value tuple instead of whatever it was returning in Leopard. Instead of named values, you’ll need to change the last two lines (of the Python script) to:

  • timevalue, timescale, unknownInt = movie.duration()
  • duration = timevalue/timescale
  • print filename, "is", duration, "seconds long"

If you’re running it in a terminal on your workstation while you have a GUI session going, everything is fine.

If you’re running it on a server, you will then see an error that looks like this:

  • $ bin/duration octopus.mp4
  • Tue Feb 8 09:07:38 www.example.com QTKitServer[20450] <Error>: kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.
  • _RegisterApplication(), FAILED TO establish the default connection to the WindowServer, _CGSDefaultConnection() is NULL.
  • octopus.mp4 is 13 seconds long

Look closely, and you can see that despite complaining about the lack of a WindowServer, the script did work. Fortunately, the warnings go to stderr, so if you’re using this in a script and parsing the results, you only get the printed duration.

There still appears to be an issue importing QTMovie into models.py on Snow Leopard when running in a server context, although I’m not sure what it is—when I added “from QTKit import QTMovie” to the top of models.py, the server never completed its response, which meant that I never received an error.

Finally, I don’t know what the third item in the tuple is, other than being an int and always zero. I thought at first it might be an error response like in QTMovie.movieWithAttributes_error_(), but that value is None when there’s no error, not zero, so who knows. The QTKit documentation calls it “flags”, but doesn’t explain what the flags are.

QTTime. Defines the value and time scale of a time.

[toggle code]

  • typedef struct {
    • long long timeValue;
    • long timeScale;
    • long flags;
  • } QTTime;

QTTime is a simple data structure that consists of three fields. In this case, the timeScale field is the number of units per second you work with when dealing with time. The timeValue field is the number of those units in duration.

To be safe, you may want to check the value and warn if it is ever not zero.

In response to Media duration in Python on Mac OS X: It turns out to be very easy to get the duration of MP3 files, MPEGs, and other media files on the OS X command line.