如何通过 Powershell 修改文件属性中的 "Media Created" 字段
How to Modify "Media Created" Field in File Properties via Powershell
我正在尝试将几千个家庭视频转换为较小的格式。但是,对视频进行编码会将创建和修改的时间戳更改为今天的日期。我编写了一个 powershell 脚本,它通过将原始文件的修改时间戳写入新文件而成功地(以某种方式)工作。
但是,我无法在 powershell 中找到修改文件详细信息属性中的“媒体创建”时间戳的方法。有没有办法添加一个例程,从原始文件复制所有元数据,或者至少将“媒体创建”字段设置为修改日期?
我搜索文件属性时,好像只有存档、隐藏等选项。附上我制作的powershell脚本(请不要笑得太厉害,哈哈)。谢谢
$filepath1 = 'E:\ConvertedMedia\Ingest\' # directory with incorrect modified & create date
$filepath2 = "F:\Backup Photos 2020 and DATA\Data\Photos\Photos 202121 Part1\Panasonic 3-2-21\A016\PRIVATE\PANA_GRP[=11=]1RAQAM\" # directory with correct date and same file name (except extension)
$destinationCodec = "*.mp4" # Keep * in front of extension
$sourceCodec = ".mov"
Get-ChildItem $filepath1 -File $destinationCodec | Foreach-Object { # change *.mp4 to the extension of the newly encoded files with the wrong date
$fileName = $_.Name # sets fileName variable (with extension)
$fileName # Optional used during testing- sends the file name to the console
$fileNameB = $_.BaseName # sets fileNameB variable to the filename without extension
$filename2 = "$filepath2" + "$fileNameB" + "$sourceCodec" # assembles filepath for source
$correctTime = (Get-Item $filename2).lastwritetime # used for testing - just shows the correct time in the output, can comment out
$correctTime # prints the correct time
$_.lastwritetime = (Get-Item $filename2).lastwritetime # modifies lastwritetime of filepath1 to match filepath2
$_.creationTime = (Get-Item $filename2).lastwritetime # modifies creation times to match lastwritetime (comment out if you need creation time to be the same)
}
更新:
我想我需要使用 Shell.Application,但我收到一条错误消息 “散列文字中不允许重复的键 ' '” 而我不确定如何将其合并到原始脚本中。
我只需要“修改日期”属性与“上次写入时间”相同。添加其他字段只是为了测试。感谢您的帮助!
$tags = "people; snow; weather"
$cameraModel = "AG-CX10"
$cameraMaker = "Panasonic"
$mediaCreated = "2/16/1999 5:01 PM"
$com = (New-Object -ComObject Shell.Application).NameSpace('C:\Users\philip\Videos') #Not sure how to specify file type
$com.Items() | ForEach-Object {
New-Object -TypeName PSCustomObject -Property @{
Name = $com.GetDetailsOf($_,0) # lists current extended properties
Tags = $com.GetDetailsOf($_,18)
CameraModel = $com.GetDetailsOf($_,30)
CameraMaker = $com.GetDetailsOf($_,32)
MediaCreated = $com.GetDetailsOf($_,208)
$com.GetDetailsOf($_,18) = $tags # sets extended properties
$com.GetDetailsOf($_,30) = $cameraModel
$com.GetDetailsOf($_,32) = $cameraMaker
$com.GetDetailsOf($_,32) = $mediaCreated
}
}
Script Example
File Properties Window
我认为你最好的选择是从 Powershell 驱动一个外部 tool/library 而不是使用 shell(不确定你是否真的可以这样设置值 tbh)。
绝对可以使用 FFMpeg 来设置文件的 Media Created 元数据,如下所示:
ffmpeg -i input.MOV -metadata creation_time=2000-01-01T00:00:00.0000000+00:00 -codec copy output.MOV
这会将 复制 input.MOV 文件到新文件 output.MOV 并在新 output.MOV 上设置媒体创建元数据。这是非常低效的 - 但它确实有效。
您可以编写如下的 ffmpeg 脚本。该脚本当前会将 FFMpeg 命令输出到屏幕,注释掉的 Start-Process
行可用于执行 ffmpeg.
gci | where Extension -eq ".mov" | foreach {
$InputFilename = $_.FullName;
$OutputFilename = "$($InputFilename)-fixed.mov";
Write-Host "Reading $($_.Name). Created: $($_.CreationTime). Modifed: $($_.LastWriteTime)";
$timestamp = Get-Date -Date $_.CreationTime -Format O
Write-Host "ffmpeg -i $InputFilename -metadata creation_time=$timestamp -codec copy $OutputFilename"
# Start-Process -Wait -FilePath C:\ffmpeg\bin\ffmpeg.exe -ArgumentList @("-i $InputFilename -metadata creation_time=$timestamp -codec copy $($OutputFilename)")
}
我正在尝试将几千个家庭视频转换为较小的格式。但是,对视频进行编码会将创建和修改的时间戳更改为今天的日期。我编写了一个 powershell 脚本,它通过将原始文件的修改时间戳写入新文件而成功地(以某种方式)工作。
但是,我无法在 powershell 中找到修改文件详细信息属性中的“媒体创建”时间戳的方法。有没有办法添加一个例程,从原始文件复制所有元数据,或者至少将“媒体创建”字段设置为修改日期?
我搜索文件属性时,好像只有存档、隐藏等选项。附上我制作的powershell脚本(请不要笑得太厉害,哈哈)。谢谢
$filepath1 = 'E:\ConvertedMedia\Ingest\' # directory with incorrect modified & create date
$filepath2 = "F:\Backup Photos 2020 and DATA\Data\Photos\Photos 202121 Part1\Panasonic 3-2-21\A016\PRIVATE\PANA_GRP[=11=]1RAQAM\" # directory with correct date and same file name (except extension)
$destinationCodec = "*.mp4" # Keep * in front of extension
$sourceCodec = ".mov"
Get-ChildItem $filepath1 -File $destinationCodec | Foreach-Object { # change *.mp4 to the extension of the newly encoded files with the wrong date
$fileName = $_.Name # sets fileName variable (with extension)
$fileName # Optional used during testing- sends the file name to the console
$fileNameB = $_.BaseName # sets fileNameB variable to the filename without extension
$filename2 = "$filepath2" + "$fileNameB" + "$sourceCodec" # assembles filepath for source
$correctTime = (Get-Item $filename2).lastwritetime # used for testing - just shows the correct time in the output, can comment out
$correctTime # prints the correct time
$_.lastwritetime = (Get-Item $filename2).lastwritetime # modifies lastwritetime of filepath1 to match filepath2
$_.creationTime = (Get-Item $filename2).lastwritetime # modifies creation times to match lastwritetime (comment out if you need creation time to be the same)
}
更新:
我想我需要使用 Shell.Application,但我收到一条错误消息 “散列文字中不允许重复的键 ' '” 而我不确定如何将其合并到原始脚本中。
我只需要“修改日期”属性与“上次写入时间”相同。添加其他字段只是为了测试。感谢您的帮助!
$tags = "people; snow; weather"
$cameraModel = "AG-CX10"
$cameraMaker = "Panasonic"
$mediaCreated = "2/16/1999 5:01 PM"
$com = (New-Object -ComObject Shell.Application).NameSpace('C:\Users\philip\Videos') #Not sure how to specify file type
$com.Items() | ForEach-Object {
New-Object -TypeName PSCustomObject -Property @{
Name = $com.GetDetailsOf($_,0) # lists current extended properties
Tags = $com.GetDetailsOf($_,18)
CameraModel = $com.GetDetailsOf($_,30)
CameraMaker = $com.GetDetailsOf($_,32)
MediaCreated = $com.GetDetailsOf($_,208)
$com.GetDetailsOf($_,18) = $tags # sets extended properties
$com.GetDetailsOf($_,30) = $cameraModel
$com.GetDetailsOf($_,32) = $cameraMaker
$com.GetDetailsOf($_,32) = $mediaCreated
}
}
Script Example File Properties Window
我认为你最好的选择是从 Powershell 驱动一个外部 tool/library 而不是使用 shell(不确定你是否真的可以这样设置值 tbh)。
绝对可以使用 FFMpeg 来设置文件的 Media Created 元数据,如下所示:
ffmpeg -i input.MOV -metadata creation_time=2000-01-01T00:00:00.0000000+00:00 -codec copy output.MOV
这会将 复制 input.MOV 文件到新文件 output.MOV 并在新 output.MOV 上设置媒体创建元数据。这是非常低效的 - 但它确实有效。
您可以编写如下的 ffmpeg 脚本。该脚本当前会将 FFMpeg 命令输出到屏幕,注释掉的 Start-Process
行可用于执行 ffmpeg.
gci | where Extension -eq ".mov" | foreach {
$InputFilename = $_.FullName;
$OutputFilename = "$($InputFilename)-fixed.mov";
Write-Host "Reading $($_.Name). Created: $($_.CreationTime). Modifed: $($_.LastWriteTime)";
$timestamp = Get-Date -Date $_.CreationTime -Format O
Write-Host "ffmpeg -i $InputFilename -metadata creation_time=$timestamp -codec copy $OutputFilename"
# Start-Process -Wait -FilePath C:\ffmpeg\bin\ffmpeg.exe -ArgumentList @("-i $InputFilename -metadata creation_time=$timestamp -codec copy $($OutputFilename)")
}