wpfmp4device-orientationtaglib-sharp

WPF: How to read video orientation (i.e. side data: displaymatrix) in mp4 with TagLibSharp?


I'm trying to display videos in a WPF MediaElement. I took them with my phone. Videos taken in landscape mode show fine, in portrait mode they are rotated by 90 degrees. When looking at them in Windows, they are shown with the proper orientation.

I tried to detect the orientation by using TagLib#, which reports both for landscape and portrait the same dimensions VideoHeight:2160 and VideoWidth: 3840, regardless of orientation.

I then used ffprobe to investigate further and found that indeed width and height are the same regardless of orientation, but that the video orientation information is in "side data: displaymatrix: rotation of -90.00 degrees".

Question: How can I use TagLib-Sharp to read the side datta: displaymatrix from a mp4 file ?

If TagLib-Sharp cannot do it, how else can I get this information ?


Solution

  • It seems TagLib-Sharp can simply not do it. At least I could not figure out a way how to do it.

    But I found another meta data reading library which can read the Orientation of a video or photo: MetadataExtractor, which one can install using Nuget.

    Here is the code how to read the Orientation, which MetadataExtractor actually calls Rotation:

    using MetadataExtractor;
    
    var isLandscape = true;
    var directories = ImageMetadataReader.ReadMetadata("PathAndFilename);
    var isRotationFound = false;
    foreach (var directory in directories) {
      if (directory.Name=="QuickTime Track Header") {
        foreach (var imageMetadataTag in directory.Tags) {
          if (imageMetadataTag.Name=="Rotation") {
            isRotationFound = true;
            if (imageMetadataTag.Description=="-90") {
              isLandscape = false;
            }
            break;
          }
        }
      }
      if (isRotationFound) break;
    
    }
    
    VideoPlayer.Source = new Uri(startFile.FullName);
    VideoPlayer.LayoutTransform =
      isLandscape ? new RotateTransform { Angle = 0 } : new RotateTransform { Angle = 90 };
    VideoPlayer.Play();
    

    Important difference

    TagLib-Sharp: Provides properties for metadata like Width or ISO or ... This is very convenient, but a problem if no property is provided, like for Orientation.

    MetadataExtractor: Gives access to all metadata, but the developer needs to know what name is used for that data, an information I could not find easily on the internet.

    See my article WPF: Displaying photos and videos properly in portrait and landscape orientation on CodeProject.com for a more detailed explanation.