从 pyobjc 中的 UTType 格式获取编解码器 OSType

发布于 2025-01-07 10:32:34 字数 1517 浏览 0 评论 0原文

我目前正在编写一个处理批量 Quicktime 的脚本,这是我第一次使用 pyobjc (我只在实际的 Objective-C 中编写了另一个非常简单的脚本)。我需要能够确定 Quicktimes 编解码器的四个字符 OSType,以便我可以使用 addImage_forDuration_withAttributes_() 正确使用相同的编解码器来处理图像,

因为 pyobjc 只能访问 obj-c 框架,所以我无法访问任何其中的 C 函数。我能够获取编解码器的字符串格式 UTType:

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'

此时我需要获取 OSType,对于该编解码器来说,它是“AVdn” 我假设我想要这样的东西: https://developer.apple.com/library/mac/#documentation/MobileCoreServices/Reference/UTTypeRef/Reference/reference.html但我无权访问它在 pyobjc

我最后的手段是用这样的东西支付 qt_thing

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

但这比较慢,我宁愿用代码来完成这一切。我一定错过了 Cocoa/QTKit 方面可用的东西。有谁有经验吗?

还有另一个 SO 问题再次引用使用 C api 来解析编解码器: 找出 Quicktime 电影的编解码器,但据我所知,我显然无法直接从 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2025-01-14 10:32:34

当我等待有人回答并继续挖掘,甚至深入到 Carbon 模块时......我发现有相当多的方法包装在 pyobjc 对象中,这些方法并没有真正记录在文档中,甚至可能存在于本机 QTKit 中对象。我认为这是为了弥补无法访问 Quicktime C-api 层的问题。

首先,我的搜索找到了这个 QTKit 类: QTFormatDescription

但没有明确的方法来创建其中一个。显然我不是唯一一个对如何检索感到困惑的人

当我开始搜索 QTMovie、QTTrack 和 QTMedia 对象的实际成员,寻找可能检索 QTFormatDescription 对象的方法时,我偶然发现了一个方法调用: QTTrack.mediaSubType

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

我想他们确实将很多方便的方法包装到 pyobjc 实例中,以便您可以在没有 C-api 的情况下检索此类信息。遗憾的是它如此无证。

对于任何寻找这样的随机功能的人,我所能建议的就是做这样的事情来找到您可能可用的任何添加的非参数方法:

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

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
...
葵雨 2025-01-14 10:32:34

pyobjc 确实包含对位于 LaunchServices 下的 UTType 的访问。

from LaunchServices import *

UTCreateStringForOSType()
UTGetOSTypeFromString()

pyobjc does contain access to UTType it's under LaunchServices.

from LaunchServices import *

UTCreateStringForOSType()
UTGetOSTypeFromString()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文