objective-cquicktimecodecpyobjcqtkit

Getting a codecs OSType from a UTType format in pyobjc


I'm currently writing a script that processes batches of quicktimes, and its my first time using pyobjc (I have only written one other really simple script in actual objective-c). I need to be able to determine the four character OSType of the codec of the quicktimes so that I can properly use the same codec for images using addImage_forDuration_withAttributes_()

Because pyobjc only has access to the obj-c frameworks, I can't access any of the C functions from it. I am able to get the string format UTType of the codec:

from objc import YES, NO, nil
from Cocoa import *
from QTKit import *

movieAttribs = {
    QTMovieOpenAsyncOKAttribute: NSNumber.numberWithBool_(NO),
    QTMovieEditableAttribute: NSNumber.numberWithBool_(YES),
    QTMovieFileNameAttribute: "quicktime.mov"
}

clip, err = QTMovie.movieWithAttributes_error_(movieAttribs, None)
track = clip.tracks()[0]
print track.format()
# u'Avid DNxHD Codec'

At this point I need to get the OSType, which for this codec would be 'AVdn' I'm assuming I want something like this: https://developer.apple.com/library/mac/#documentation/MobileCoreServices/Reference/UTTypeRef/Reference/reference.html But I don't have access to it in pyobjc

My last resort is to shell out to qt_thing with something like this:

qt_thing --type=imco | grep "AVID DNxHD Codec" | awk -F: '{print $2}'
# Result: AVdn

But this is slower and I would rather do it all in code. I must be missing something that is available to me in the Cocoa/QTKit side of things. Anyone with any experience?

There is another SO question that again references using the C api to resolve the codec: Find out the Codec for a Quicktime Movie, but I can't obviously do that directly form pyobjc as far as I know.


Solution

  • While I waited for someone to answer, and kept digging, even into the Carbon module... I found that there are quite a number of methods wrapped into pyobjc objects that are not really documented in the docs or probably even present in the native QTKit objc. I figure this is to offset the lack of access to the Quicktime C-api layer.

    First my search turned up this QTKit class: QTFormatDescription

    But there was no clear way how to create one of these. Apparently I wasn't the only one confused as to how to retrive one

    When I started searching the actual members of QTMovie, QTTrack, and QTMedia objects, looking for possibly a way to retrieve a QTFormatDescription object, I stumbled across a method call: QTTrack.mediaSubType

    >>> movie = QTMovie.alloc().initWithFile_error_("/path/to/quicktime.mov", None)
    >>> track = movie.tracks()[1]
    >>> track.mediaSubType()
    u'AVdn'
    

    I guess they do wrap up a lot of convenience methods into the pyobjc instances so that you can retrieve this kind of information without the C-api. Its just a shame that its so undocumented.

    For anyone looking for random functionality like this, all I can recommend is doing something like this to find any added non-arg methods that might be available to you:

    >>> print '\n'.join([attrib for attrib in dir(QTTrack) if not '_' in attrib])
    ....
    mediaName
    mediaRetained
    mediaSubType
    mediaType
    mediaTypeInMedia
    ...