Search code examples
xmlpowershellexiftool

Reading exiftool XML into Variable in PowerShell


I am attempting to read exiftool output in XML format into a PowerShell Variable.

I am using the exiftool as found here - https://exiftool.org/

The command to view the XML in a command window is:

C:\Temp\exiftool.exe -a -g -struct -X "C:\Temp 1\20190813 0100.mp4"

This produces the following XML:

<?xml version='1.0' encoding='UTF-8'?>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>    

<rdf:Description rdf:about='C:/Temp 1/20190813 0100.mp4'
  xmlns:et='http://ns.exiftool.ca/1.0/' et:toolkit='Image::ExifTool 11.93'
  xmlns:ExifTool='http://ns.exiftool.ca/ExifTool/1.0/'
  xmlns:System='http://ns.exiftool.ca/File/System/1.0/'
  xmlns:File='http://ns.exiftool.ca/File/1.0/'
  xmlns:QuickTime='http://ns.exiftool.ca/QuickTime/QuickTime/1.0/'
  xmlns:Track1='http://ns.exiftool.ca/QuickTime/Track1/1.0/'
  xmlns:Track2='http://ns.exiftool.ca/QuickTime/Track2/1.0/'
  xmlns:ItemList='http://ns.exiftool.ca/QuickTime/ItemList/1.0/'
  xmlns:XMP-x='http://ns.exiftool.ca/XMP/XMP-x/1.0/'
  xmlns:XMP-iptcExt='http://ns.exiftool.ca/XMP/XMP-iptcExt/1.0/'
  xmlns:XMP-dc='http://ns.exiftool.ca/XMP/XMP-dc/1.0/'
  xmlns:XMP-photoshop='http://ns.exiftool.ca/XMP/XMP-photoshop/1.0/'
  xmlns:XMP-xmp='http://ns.exiftool.ca/XMP/XMP-xmp/1.0/'
  xmlns:Composite='http://ns.exiftool.ca/Composite/1.0/'>
 <ExifTool:ExifToolVersion>11.93</ExifTool:ExifToolVersion>
 <System:FileName>20190813 0100.mp4</System:FileName>
 <System:Directory>C:/Temp 1</System:Directory>
 <System:FileSize>45 MB</System:FileSize>
 <System:FileModifyDate>2020:04:13 22:38:44+01:00</System:FileModifyDate>
 <System:FileAccessDate>2020:04:26 16:24:41+01:00</System:FileAccessDate>
 <System:FileCreateDate>2019:08:13 01:00:09+01:00</System:FileCreateDate>
 <System:FilePermissions>rw-rw-rw-</System:FilePermissions>
 <File:FileType>MP4</File:FileType>
 <File:FileTypeExtension>mp4</File:FileTypeExtension>
 <File:MIMEType>video/mp4</File:MIMEType>
 <QuickTime:MajorBrand>MP4  Base Media v1 [IS0 14496-12:2003]</QuickTime:MajorBrand>
 <QuickTime:MinorVersion>0.2.0</QuickTime:MinorVersion>
 <QuickTime:CompatibleBrands>
  <rdf:Bag>
   <rdf:li>isom</rdf:li>
   <rdf:li>iso2</rdf:li>
   <rdf:li>avc1</rdf:li>
   <rdf:li>mp41</rdf:li>
  </rdf:Bag>
 </QuickTime:CompatibleBrands>
 <QuickTime:MovieHeaderVersion>0</QuickTime:MovieHeaderVersion>
 <QuickTime:CreateDate>0000:00:00 00:00:00</QuickTime:CreateDate>
 <QuickTime:ModifyDate>0000:00:00 00:00:00</QuickTime:ModifyDate>
 <QuickTime:TimeScale>1000</QuickTime:TimeScale>
 <QuickTime:Duration>22.35 s</QuickTime:Duration>
 <QuickTime:PreferredRate>1</QuickTime:PreferredRate>
 <QuickTime:PreferredVolume>100.00%</QuickTime:PreferredVolume>
 <QuickTime:MatrixStructure>1 0 0 0 1 0 0 0 1</QuickTime:MatrixStructure>
 <QuickTime:PreviewTime>0 s</QuickTime:PreviewTime>
 <QuickTime:PreviewDuration>0 s</QuickTime:PreviewDuration>
 <QuickTime:PosterTime>0 s</QuickTime:PosterTime>
 <QuickTime:SelectionTime>0 s</QuickTime:SelectionTime>
 <QuickTime:SelectionDuration>0 s</QuickTime:SelectionDuration>
 <QuickTime:CurrentTime>0 s</QuickTime:CurrentTime>
 <QuickTime:NextTrackID>3</QuickTime:NextTrackID>
 <QuickTime:HandlerType>Metadata</QuickTime:HandlerType>
 <QuickTime:HandlerVendorID>Apple</QuickTime:HandlerVendorID>
 <QuickTime:MediaDataSize>47401322</QuickTime:MediaDataSize>
 <QuickTime:MediaDataOffset>30971</QuickTime:MediaDataOffset>
 <Track1:TrackHeaderVersion>0</Track1:TrackHeaderVersion>
 <Track1:TrackCreateDate>0000:00:00 00:00:00</Track1:TrackCreateDate>
 <Track1:TrackModifyDate>0000:00:00 00:00:00</Track1:TrackModifyDate>
 <Track1:TrackID>1</Track1:TrackID>
 <Track1:TrackDuration>21.55 s</Track1:TrackDuration>
 <Track1:TrackLayer>0</Track1:TrackLayer>
 <Track1:TrackVolume>0.00%</Track1:TrackVolume>
 <Track1:MatrixStructure>1 0 0 0 1 0 0 0 1</Track1:MatrixStructure>
 <Track1:ImageWidth>2688</Track1:ImageWidth>
 <Track1:ImageHeight>2688</Track1:ImageHeight>
 <Track1:MediaHeaderVersion>0</Track1:MediaHeaderVersion>
 <Track1:MediaCreateDate>0000:00:00 00:00:00</Track1:MediaCreateDate>
 <Track1:MediaModifyDate>0000:00:00 00:00:00</Track1:MediaModifyDate>
 <Track1:MediaTimeScale>1000000</Track1:MediaTimeScale>
 <Track1:MediaDuration>21.55 s</Track1:MediaDuration>
 <Track1:MediaLanguageCode>und</Track1:MediaLanguageCode>
 <Track1:HandlerType>Video Track</Track1:HandlerType>
 <Track1:HandlerDescription>VideoHandler</Track1:HandlerDescription>
 <Track1:GraphicsMode>srcCopy</Track1:GraphicsMode>
 <Track1:OpColor>0 0 0</Track1:OpColor>
 <Track1:CompressorID>avc1</Track1:CompressorID>
 <Track1:SourceImageWidth>2688</Track1:SourceImageWidth>
 <Track1:SourceImageHeight>2688</Track1:SourceImageHeight>
 <Track1:XResolution>72</Track1:XResolution>
 <Track1:YResolution>72</Track1:YResolution>
 <Track1:BitDepth>24</Track1:BitDepth>
 <Track1:VideoFrameRate>60.049</Track1:VideoFrameRate>
 <Track2:TrackHeaderVersion>0</Track2:TrackHeaderVersion>
 <Track2:TrackCreateDate>0000:00:00 00:00:00</Track2:TrackCreateDate>
 <Track2:TrackModifyDate>0000:00:00 00:00:00</Track2:TrackModifyDate>
 <Track2:TrackID>2</Track2:TrackID>
 <Track2:TrackDuration>22.35 s</Track2:TrackDuration>
 <Track2:TrackLayer>0</Track2:TrackLayer>
 <Track2:TrackVolume>100.00%</Track2:TrackVolume>
 <Track2:MatrixStructure>1 0 0 0 1 0 0 0 1</Track2:MatrixStructure>
 <Track2:MediaHeaderVersion>0</Track2:MediaHeaderVersion>
 <Track2:MediaCreateDate>0000:00:00 00:00:00</Track2:MediaCreateDate>
 <Track2:MediaModifyDate>0000:00:00 00:00:00</Track2:MediaModifyDate>
 <Track2:MediaTimeScale>44100</Track2:MediaTimeScale>
 <Track2:MediaDuration>22.35 s</Track2:MediaDuration>
 <Track2:MediaLanguageCode>und</Track2:MediaLanguageCode>
 <Track2:HandlerType>Audio Track</Track2:HandlerType>
 <Track2:HandlerDescription>SoundHandler</Track2:HandlerDescription>
 <Track2:Balance>0</Track2:Balance>
 <Track2:AudioFormat>mp4a</Track2:AudioFormat>
 <Track2:AudioChannels>2</Track2:AudioChannels>
 <Track2:AudioBitsPerSample>16</Track2:AudioBitsPerSample>
 <Track2:AudioSampleRate>44100</Track2:AudioSampleRate>
 <ItemList:Encoder>Lavf57.83.100</ItemList:Encoder>
 <XMP-x:XMPToolkit>Image::ExifTool 11.93</XMP-x:XMPToolkit>
 <XMP-iptcExt:PersonInImage>
  <rdf:Bag>
   <rdf:li>Jessica Collinge</rdf:li>
  </rdf:Bag>
 </XMP-iptcExt:PersonInImage>
 <XMP-dc:Rights>Me</XMP-dc:Rights>
 <XMP-dc:Subject>
  <rdf:Bag>
   <rdf:li>C:</rdf:li>
  </rdf:Bag>
 </XMP-dc:Subject>
 <XMP-photoshop:DateCreated>2019:08:13 01:00</XMP-photoshop:DateCreated>
 <XMP-xmp:CreateDate>2019:08:13 01:00</XMP-xmp:CreateDate>
 <XMP-xmp:MetadataDate>2019:08:13 01:00:00+00:00</XMP-xmp:MetadataDate>
 <XMP-xmp:ModifyDate>2019:08:13 01:00:00+00:00</XMP-xmp:ModifyDate>
 <Composite:ImageSize>2688x2688</Composite:ImageSize>
 <Composite:Megapixels>7.2</Composite:Megapixels>
 <Composite:AvgBitrate>17 Mbps</Composite:AvgBitrate>
 <Composite:Rotation>0</Composite:Rotation>
</rdf:Description>
</rdf:RDF>

I am reading this information into a PowerShell Variable using:

$meta=([xml](C:\Temp\exiftool.exe -a -g -struct -X `"C:\Temp 1\20190813 0100.mp4")).rdf.description

When I view the Viarable data:

$meta

I get:

about              : C:/Temp 1/20190813 0100.mp4
et                 : http://ns.exiftool.ca/1.0/
toolkit            : Image::ExifTool 11.93
ExifTool           : http://ns.exiftool.ca/ExifTool/1.0/
System             : http://ns.exiftool.ca/File/System/1.0/
File               : http://ns.exiftool.ca/File/1.0/
QuickTime          : http://ns.exiftool.ca/QuickTime/QuickTime/1.0/
Track1             : http://ns.exiftool.ca/QuickTime/Track1/1.0/
Track2             : http://ns.exiftool.ca/QuickTime/Track2/1.0/
ItemList           : http://ns.exiftool.ca/QuickTime/ItemList/1.0/
XMP-x              : http://ns.exiftool.ca/XMP/XMP-x/1.0/
XMP-iptcExt        : http://ns.exiftool.ca/XMP/XMP-iptcExt/1.0/
XMP-dc             : http://ns.exiftool.ca/XMP/XMP-dc/1.0/
XMP-photoshop      : http://ns.exiftool.ca/XMP/XMP-photoshop/1.0/
XMP-xmp            : http://ns.exiftool.ca/XMP/XMP-xmp/1.0/
Composite          : http://ns.exiftool.ca/Composite/1.0/
ExifToolVersion    : 11.93
FileName           : 20190813 0100.mp4
Directory          : C:/Temp 1
FileSize           : 45 MB
FileModifyDate     : 2020:04:13 22:38:44+01:00
FileAccessDate     : 2020:04:26 16:27:21+01:00
FileCreateDate     : 2019:08:13 01:00:09+01:00
FilePermissions    : rw-rw-rw-
FileType           : MP4
FileTypeExtension  : mp4
MIMEType           : video/mp4
MajorBrand         : MP4  Base Media v1 [IS0 14496-12:2003]
MinorVersion       : 0.2.0
CompatibleBrands   : CompatibleBrands
MovieHeaderVersion : 0
CreateDate         : {0000:00:00 00:00:00, 2019:08:13 01:00}
ModifyDate         : {0000:00:00 00:00:00, 2019:08:13 01:00:00+00:00}
TimeScale          : 1000
Duration           : 22.35 s
PreferredRate      : 1
PreferredVolume    : 100.00%
MatrixStructure    : {1 0 0 0 1 0 0 0 1, 1 0 0 0 1 0 0 0 1, 1 0 0 0 1 0 0 0 1}
PreviewTime        : 0 s
PreviewDuration    : 0 s
PosterTime         : 0 s
SelectionTime      : 0 s
SelectionDuration  : 0 s
CurrentTime        : 0 s
NextTrackID        : 3
HandlerType        : {Metadata, Video Track, Audio Track}
HandlerVendorID    : Apple
MediaDataSize      : 47401322
MediaDataOffset    : 30971
TrackHeaderVersion : {0, 0}
TrackCreateDate    : {0000:00:00 00:00:00, 0000:00:00 00:00:00}
TrackModifyDate    : {0000:00:00 00:00:00, 0000:00:00 00:00:00}
TrackID            : {1, 2}
TrackDuration      : {21.55 s, 22.35 s}
TrackLayer         : {0, 0}
TrackVolume        : {0.00%, 100.00%}
ImageWidth         : 2688
ImageHeight        : 2688
MediaHeaderVersion : {0, 0}
MediaCreateDate    : {0000:00:00 00:00:00, 0000:00:00 00:00:00}
MediaModifyDate    : {0000:00:00 00:00:00, 0000:00:00 00:00:00}
MediaTimeScale     : {1000000, 44100}
MediaDuration      : {21.55 s, 22.35 s}
MediaLanguageCode  : {und, und}
HandlerDescription : {VideoHandler, SoundHandler}
GraphicsMode       : srcCopy
OpColor            : 0 0 0
CompressorID       : avc1
SourceImageWidth   : 2688
SourceImageHeight  : 2688
XResolution        : 72
YResolution        : 72
BitDepth           : 24
VideoFrameRate     : 60.049
Balance            : 0
AudioFormat        : mp4a
AudioChannels      : 2
AudioBitsPerSample : 16
AudioSampleRate    : 44100
Encoder            : Lavf57.83.100
XMPToolkit         : Image::ExifTool 11.93
PersonInImage      : PersonInImage
Rights             : Me
Subject            : Subject
DateCreated        : 2019:08:13 01:00
MetadataDate       : 2019:08:13 01:00:00+00:00
ImageSize          : 2688x2688
Megapixels         : 7.2
AvgBitrate         : 17 Mbps
Rotation           : 0

I need to be able to see the prefix of the attribute for example:

Duration           : 22.35 s

Should be:

QuickTime:Duration           : 22.35 s

How can I achieve this?


Solution

  • PowerShell's convenient adaptation of the XML DOM via dot notation ignores namespace prefixes such as QuickTime: in element names - both on element access and in the default output formatting.

    Therefore, in order to get the element names with their namespace prefix, you need to access the child elements of the target element explicitly, via System.Xml.XmlElement.ChildNodes, and extract their .Name and .InnerText properties:

    PS> $x.rdf.Description.ChildNodes | Select-Object Name, InnerText
    
    Name                         InnerText
    ----                         ---------
    ExifTool:ExifToolVersion     11.93
    System:FileName              20190813 0100.mp4
    ...
    QuickTime:Duration           22.35 s
    ...
    

    Note: Due to use of Select-Object, the above outputs an array of [pscustomobject] instances, each with a .Name and .InnerText property, and you're seeing the default output formatting of these objects.

    If you only need the representation above for display purposes, you could substitute
    Format-Table for Select-Object.