将 exiftool XML 读入 PowerShell 中的变量

Reading exiftool XML into Variable in PowerShell

我正在尝试将 XML 格式的 exiftool 输出读入 PowerShell 变量。

我正在使用此处找到的 exiftool - https://exiftool.org/

在命令window中查看XML的命令是:

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

这会产生以下结果 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>

我正在使用以下方法将此信息读入 PowerShell 变量:

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

当我查看 Viarable 数据时:

$meta

我得到:

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

我需要能够看到属性的前缀,例如:

Duration           : 22.35 s

应该是:

QuickTime:Duration           : 22.35 s

我怎样才能做到这一点?

PowerShell 的便利 忽略 名称空间前缀,例如元素名称中的 QuickTime: - 都在元素 access 和默认 输出格式.

因此,为了获得带有命名空间前缀的元素名称,您需要通过System.Xml.XmlElement.ChildNodes显式访问目标元素的子元素,并且提取它们的 .Name.InnerText 属性:

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
...

注意:由于使用了Select-Object,上面输出了一个[pscustomobject]个实例的数组,每个实例有一个.Name.InnerText属性,并且您会看到这些对象的默认输出格式。

如果您只是为了 显示 目的而需要上述表示,您可以替换为
Format-Table 对于 Select-Object

不确定这会有多大帮助。您可以像这样与命名空间交互:

$ns = @{rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  QuickTime='http://ns.exiftool.ca/QuickTime/QuickTime/1.0/'}
select-xml /rdf:RDF/rdf:Description/QuickTime:Duration file.xml -na $ns | 
  % node                                     

#text
-----
22.35 s