Monday, May 25, 2020

How to iterate over image, music and video properties using Microsoft Windows API Code Pack

Here's how to iterate over SystemProperties.System.... properties to get metadata for an music, image or video file (there are many other filetypes as well) using Microsoft Windows API Code Pack

From MS SystemProperties.System documentation 
https://docs.microsoft.com/en-us/uwp/api/windows.storage.systemproperties?view=winrt-18362 we get following metadata categories to interrogate. 

SystemProperties.System.

TABLE 2
Audio
Gets an object that provides the indexing names of Windows file properties for System.Audio.
Author
Gets the name of the System.Author property (one of the Windows file properties.
Comment
Gets the name of the System.Comment property (one of the Windows file properties.
GPS
Gets an object that provides the indexing names of Windows system file properties for System.GPS.
Image
Gets an object that provides the indexing names of Windows file properties for System.Image.
ItemNameDisplay
Gets the name of the System.ItemNameDisplay property (one of the Windows file properties.
Keywords
Gets the name of the System.Keywords property (one of the Windows file properties.
Media
Gets an object that provides the indexing names of system media file properties such as System.Media.Duration.
Music
Gets an object that provides the indexing names of Windows file properties for System.Music.
Photo
Gets an object that provides the indexing names of Windows file properties for System.Photo.
Rating
Gets the name of the System.Rating property (one of the Windows file properties.
Title
Gets the name of the System.Title property (one of the Windows file properties.
Video
Gets an object that provides the indexing names of Windows file properties for System.Video.


There is not collection that works for all the above categories, so I had to implement the following. There is a default collection and for photo only.

Warning! This code does not sniff the properties of underlying type format.  So if you have an image that is webp, but mislabel as an jpg an imageisreallywebp.jpg or worse imageisreallywebp.webp (but is an exe) then this library will NOT detect that!

This code base is not maintained, and the BIT Depth for example is not reported accurately, test using samples here - https://etc.usf.edu/techease/win/images/what-is-bit-depth/

Code to iterate over Media Properties


Console.WriteLine("");
Console.WriteLine("SystemProperties.System.Media - Media Category Properties");
Console.WriteLine("");
string mediaProp = string.Empty;
PropertyInfo[] mediaPI = typeof(SystemProperties.System.Media).GetProperties();
foreach (PropertyInfo property in mediaPI)
{
 //https://docs.microsoft.com/en-us/windows/win32/properties/props-system-photo-aperture !@@@@@@@
 mediaProp = "System.Media." + property.Name; //@@@@@@@@@@@! This took  tooo long to figure out!
   
 //NOT -  mediaProp = "SystemProperties.System.Media"
 
 try
 {
  IShellProperty ishellprop = picture.Properties.GetProperty(mediaProp); //very slow

  if (ishellprop != null && ishellprop.ValueAsObject != null)
   Console.WriteLine(ishellprop.Name + "=" + ishellprop.ValueAsObject.ToString());

 }
 catch
 {
  continue;
 }

}

No comments:

Post a Comment