How can I get programmatic access to the “Date taken” field of an image or video using powershell?

前端 未结 2 1391
清酒与你
清酒与你 2021-02-13 03:37

I\'m trying convert a bunch of pictures and videos, but when I convert it to a new format I obviously lose the properties of the original file. I\'d like to be able to read the

相关标签:
2条回答
  • 2021-02-13 04:22

    I can't test it right now (don't have any images with XIF data laying around, but I think this should work:

    [reflection.assembly]::LoadWithPartialName("System.Drawing")
    $pic = New-Object System.Drawing.Bitmap('C:\PATH\TO\SomePic.jpg')
    $bitearr = $pic.GetPropertyItem(36867).Value 
    $string = [System.Text.Encoding]::ASCII.GetString($bitearr) 
    $DateTime = [datetime]::ParseExact($string,"yyyy:MM:dd HH:mm:ss`0",$Null)
    $DateTime
    
    0 讨论(0)
  • 2021-02-13 04:34

    In general, you can access any extended property for a file shown in explorer through the shell GetDetailsOf method. Here's a short example, adapted from another answer:

    $file = Get-Item IMG_0386.jpg
    $shellObject = New-Object -ComObject Shell.Application
    $directoryObject = $shellObject.NameSpace( $file.Directory.FullName )
    $fileObject = $directoryObject.ParseName( $file.Name )
    
    $property = 'Date taken'
    for(
      $index = 5;
      $directoryObject.GetDetailsOf( $directoryObject.Items, $index ) -ne $property;
      ++$index ) { }
    
    $value = $directoryObject.GetDetailsOf( $fileObject, $index )
    


    However, according to the comments on another question, there is no general-purpose mechanism for setting these properties. The System.Drawing.Bitmap class that EBGreen mentioned will work for images, but I'm afraid I also do not know of a .NET option for video files.

    0 讨论(0)
提交回复
热议问题