Reputation: 551
The Windows Shell's property system defines a standard property called System.Video.FrameRate
(documented here), which is available on most video files. The surrounding documentation only covers unmanaged APIs for getting these values.
How do I access this Windows Property in C#?
Upvotes: 4
Views: 3694
Reputation: 14547
To access a shell property (and that's what this is - these 'Windows' properties are just shell properties predefined by Windows, as distinct from application-specific properties) there are two usual ways to do this: interop, or the Windows API code pack.
The Windows API Code Pack is likely to be the easiest way to do this. To obtain this property, first you need to determine which shell object you'd like to use. (Properties are always found on some particular objects. So this property is not some global value telling you the system frame rate - it is typically present on video files, and it tells you the frame rate of that file.) This code gets the shell object for a video I happen to have on my system, retrieves the property and displays its value.
ShellObject obj = ShellObject.FromParsingName(@"D:\Video\IanAndDeborahTree.mp4");
ShellProperty<uint?> rateProp = obj.Properties.GetProperty<uint?>("System.Video.FrameRate");
Debug.WriteLine("{0:G3}FPS", rateProp.Value/1000.0);
Note that the property's value is a nullable unsigned int. If you ask for a property of type int
, it'll fail, indicating that no such property is available. So you have to go for uint?
. You should really also check whether the property was actually present - I'm not bothering here because I know it will be.
The other approach would be to use interop to talk directly to the shell API (which is what the API code pack does for you). That's a lot of work though - the number of hoops you'd have to jump through to achieve what these three lines do is massive, because you end up needing to define a surprisingly large number of COM interfaces in a form that .NET interop can handle.
Upvotes: 4