Skip to content

Instantly share code, notes, and snippets.

@ergosteur
Created March 13, 2024 19:15
Show Gist options
  • Save ergosteur/077933036b86be5dd8161c78c00b94ad to your computer and use it in GitHub Desktop.
Save ergosteur/077933036b86be5dd8161c78c00b94ad to your computer and use it in GitHub Desktop.
PowerShell script to get the height and width of video files and add them to the Get-ChildItem object array.
<#
.SYNOPSIS
Script to get the height and width of video files and add them to the Get-ChildItem object array.
.DESCRIPTION
Run the script similar to how Get-ChildItem would be used. Parameters Path, Filter and Recurse are accepted.
Can also filter based on MaxHeight.
Depends on ffprobe being in path in oreder to get video dimensions.
Returns an array of type System.IO.FileSystemInfo.
Example usage:
$lowResVideos = .\Get-VideoDimensions.ps1 -Path D:\Videos -Filter "*.mp4" -Recurse
.EXAMPLE
$lowResVideos = .\Get-VideoDimensions.ps1 -Path D:\Videos -Filter "*.mp4" -MaxHeight 480 -Recurse
.EXAMPLE
.\Get-VideoDimensions.ps1 -Filter "*.mp4" | Format-Table Name,Length,Height,Width
.PARAMETER MaxHeight
Positive integer to filter videos based on frame pixel height.
.PARAMETER Path
.PARAMETER Filter
.PARAMETER Recurse
.NOTES
Version: 0.3
Author: ergosteur
Creation Date: 2024-03-13
#>
param (
[int]$MaxHeight = 0,
[string]$Path,
[string]$Filter = "*.mp4",
[switch]$Recurse
)
function Get-VideoFileDimensions {
param (
[string]$Directory,
[string]$Filter,
[bool]$Recurse
)
$Shell = New-Object -ComObject Shell.Application
if ($Recurse) { $VideoFiles = Get-ChildItem -Path $Directory -Filter $Filter -Recurse }
else { $VideoFiles = Get-ChildItem -Path $Directory -Filter $Filter }
$VideoFiles | ForEach-Object {
$VideoFile = $_.FullName
$VideoInfo = & ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 "$VideoFile"
#Write-Host $VideoInfo, $VideoFile
if ($VideoInfo -ne "") {
$Width, $Height = $VideoInfo -split ","
#Write-Host "adding" $_, $Height, $Width
$_ | Add-Member -MemberType NoteProperty -Name "Height" -Value $Height
$_ | Add-Member -MemberType NoteProperty -Name "Width" -Value $Width
#Write-Host ($_ | Format-Table Name,LastWriteTime,Length,Width,Height | Out-String)
}
}
return $VideoFiles
}
# Get all video dimensions
if ($Path) {
$AllVideos = Get-VideoFileDimensions -Directory ($Path) -Filter $Filter -Recurse $Recurse
}
else {
$AllVideos = Get-VideoFileDimensions -Directory (Get-Location) -Filter $Filter -Recurse $Recurse
}
#Write-Host "AllVideos"
#Write-Host ($AllVideos | Format-Table Name,LastWriteTime,Length,Width,Height | Out-String)
# Filter videos with height less than $MaxHeight
if ($MaxHeight -gt 0) {
$FilteredVideos = $AllVideos | Where-Object { [int]$_.Height -le $MaxHeight }
}
else {
$FilteredVideos = $AllVideos
}
#Write-Host "FilteredVideos"
#Write-Host ($FilteredVideos | Format-Table Name,LastWriteTime,Length,Width,Height | Out-String)
# Return the filtered videos
Return $FilteredVideos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment