A: 

Create a QTMovie instance, and then query its properties with a -movieAttributes message.

NSResponder
yea...but how can I expand this to other media files (images, audio)?
vfn
It'll work for audio files as well, but not for anything that QuickTime can't handle.
Peter Hosey
+2  A: 

I need to get the same kind of info that you can get on "get info" when using Finder, more specifically I need the same info that is present on "more info" section, like Duration, Bitrate, Dimension, Codecs, Audio channels, etc.

Do it the same way the Finder does: Spotlight. (Don't let the word “Carbon” in the path scare you off: It's part of Core Services, so it is available in 64-bit.) Just instantiate an MDItem for the file you're interested in.

Another way would be to use Foundation's own wrapper of that framework (which is included on that page), but then you need to do an NSMetadataQuery that searches for items whose kMDItemPath is the path to the file you're interested in. A lot of needless hassle—using MDItem directly will get you there faster.

A nice bonus is that this works for more than just video and audio files. Image files also have metadata that Finder and your app can show.

Peter Hosey
Hey Peter, your answer is right...thanks! Although I couldn't use this because I had problem to get the duration of different files...I couldn't get a consistency....for some files the duration was informed as 00:00:00, when the video was a 00:50:00 on Finder info....I am wondering how Finder can do it...maybe Apple is using some third party library. BTW, I will be using some alternative untill I figure out how to do it with Apple frameworks.
vfn
That's strange. How did you determine what duration you were getting?
Peter Hosey
I couldn't understand your question. Well, if it helps, I have got info on finder and compared with the MDItem, and as I said, on MDItem it came 00:00:00 for some videos...i also tried to use ffmpeg and it works better than on Finder...sometimes Finder cannot determine the info, but ffmpeg can. Do you have any thoughts?
vfn
I mean how are you showing yourself the duration value you retrieved in your app? That is, how are you viewing what value it retrieved?
Peter Hosey
Well, I am using kMDItemDurationSeconds, to get the length. I has been working well for most of the mp4 and mp3, but it hasn't working at all for avi and flv. So, I have also tried to compute manually the duration using the size of the file and the bitrate, but for these files the bitrate is not present.
vfn
That's how you retrieve the value. How are you viewing the value you've retrieved?
Peter Hosey
Oh...sorry, with NSLog, for example, the code for bitrate is: MDItemRef metadata = MDItemCreate(NULL, (CFStringRef)fileName); NSNumber *videoBitrate = (NSNumber*)MDItemCopyAttribute(metadata, kMDItemVideoBitRate); NSLog(@"VIDEO BITRATE:%i", [videoBitrate intValue]); NSNumber *audioBitrate = (NSNumber*)MDItemCopyAttribute(metadata, kMDItemAudioBitRate); NSLog(@"AUDIO BITRATE:%i", [audioBitrate intValue]);
vfn
The '*' didn't appeared on the code above...
vfn
Reposting in backquotes: `MDItemRef metadata = MDItemCreate(NULL, (CFStringRef)fileName); NSNumber *videoBitrate = (NSNumber *)MDItemCopyAttribute(metadata, kMDItemVideoBitRate); NSLog(@"VIDEO BITRATE:%i", [videoBitrate intValue]); NSNumber *audioBitrate = (NSNumber *)MDItemCopyAttribute(metadata, kMDItemAudioBitRate); NSLog(@"AUDIO BITRATE:%i", [audioBitrate intValue]);`
Peter Hosey
Have you verified that `videoBitrate` and `audioBitrate` (and, for that matter, `metadata`) are all not `nil`? Messages, such as `intValue`, to `nil` will return 0. An audio file, in particular, will not have a video bit rate. Also, you're not showing where you get and log the duration.
Peter Hosey
This is a generic example that get info for videos, photos, musics, etc. So, when the info is to be 0 or nil (like Video Bitrate for audio) it is OK. The problem is when this info is also nil for a .flv or .avi.
vfn
There's probably no Spotlight importer for those types. You should find that Finder doesn't show bit rates or duration for those files, either. If this is a deal-breaker for you, you could always write a Spotlight importer for flv and avi; just be aware that you may not be able to get that info if Perian isn't installed (certainly true of flv).
Peter Hosey
Hummm...Yep, you are right, Finder also doesn't show these info for these files. Do you know how to get the framerate for video files? I couldn't find how to do it with Spotlight.
vfn
You are already doing it the correct way with Spotlight. The reason it doesn't work for **some** video files (such as avi and flv files) is that you don't have a Spotlight importer installed for those types. Your users won't, either. If you refuse to accept this, your choice is to either write a simple Spotlight importer (and either bundle it with your app or distribute it separately, perhaps as freeware) or specially handle any video types that Spotlight doesn't cover by trying to get the information some other way, such as with QTKit. I would just ignore the problem, as Finder does.
Peter Hosey
Yes, this is OK, but do you know how to get fps using Spotlight? I couldn't find any parameter for this.
vfn
There's no such attribute. See http://developer.apple.com/mac/library/documentation/Carbon/Reference/MetadataAttributesRef/Reference/CommonAttrs.html and the output of mdls for a movie file. If you want one, you should file an enhancement request.
Peter Hosey