Reputation: 165
How do I find out if a file is a photo or a video in Qt?
Upvotes: 4
Views: 4066
Reputation: 33607
You ask if Qt can tell if something is "a photo or a video", which is ambiguous, but I'll translate it to:
If I have a filename, does Qt have a feature to tell me if that is (a) an image file, (b) a video file, or (c) some other kind of file?
Knowing a file's type can come from a few sources. There's the extension in the file's name, there's "sniffing" the contents of the file's data and looking for recognized magic header sequences, and there's random metadata which may live somewhere else that's very specific to the applications or OS managing the file.
Someone asked a question which was how to get the "MIME type" of a file in Qt:
Find out the mime type and associated applications with Qt
If you could do that, you could see if the string you got back was in the image/
hierarchy:
http://en.wikipedia.org/wiki/Internet_media_type#Type_image
But there's no Qt functions for that. So you're left pretty much checking the file extension, or if you specifically want to know if a given file is an image format that Qt can read, then you can use QImageReader::imageFormat() to "sniff" a file:
QByteArray imageFormat = QImageReader::imageFormat(fileName);
If the image is in the set of image types that Qt considers loadable, it will hand back a string of bytes that corresponds to that format. Otherwise you'll get an empty string. That isn't to say that failing this test means something isn't an image of some other kind, of course.
As for determining if something is a video file that Qt can play, there seems to be a possibility to do a similar "sniff" of video files if you're using Phonon via MediaSource::type() and checking to see that it's not Phonon::MediaSource::Invalid
.
Upvotes: 4