Skip to content

Instantly share code, notes, and snippets.

@ToniMaunde
Last active April 11, 2026 20:36
Show Gist options
  • Select an option

  • Save ToniMaunde/31587bbad11651f321325227a6bb1350 to your computer and use it in GitHub Desktop.

Select an option

Save ToniMaunde/31587bbad11651f321325227a6bb1350 to your computer and use it in GitHub Desktop.
Video compressing using Powershell and FFmpeg
param (
[Parameter(Mandatory, HelpMessage = "The working directory is mandatory")]
[String]$WorkDir,
[Parameter()]
[String[]]$ExcludedVideos,
[Parameter()]
[bool]$recursive
)
$WorkDir = $WorkDir.Trim()
if ($WorkDir.EndsWith("\")) {
$WorkDir = $WorkDir.SubString(0, $WorkDir.Length - 1)
}
if (-not (Test-Path $WorkDir)) {
Write-Output "The working directory provided does not exist"
Exit 1
}
function CompressVideos {
param (
[Parameter(Mandatory, HelpMessage = "The inner working directory is mandatory")]
[string]$innerWorkingDirectory
)
if ($recursive) {
$videos = @(Get-ChildItem -Path $WorkDir\$innerWorkingDirectory -Name -Recurse -Include *.mp4, *.mkv -Exclude *.srt)
} else {
$videos = @(Get-ChildItem -Path $innerWorkingDirectory -Name -Recurse -Include *.mp4, *.mkv)
}
foreach($excludedVideo in $ExcludedVideos) {
[void]$videos.Remove($excludedVideo)
}
foreach($video in $videos) {
$videoPath
if($recursive) {
$videoPath = "$WorkDir\$innerWorkingDirectory"
} else {
$videoPath = "$innerWorkingDirectory"
}
Write-Output "Compressing the video $videoPath\$video"
ffmpeg -i $videoPath\$video -vcodec libx265 -crf 24 $videoPath\new_$video
}
write-output "`n"
}
if ($recursive) {
$subDirectories = @(Get-ChildItem -Path $WorkDir -Directory -Name)
foreach($directory in $subDirectories) {
write-output "Compressing videos from $directory`n"
CompressVideos $directory
}
} else {
CompressVideos $WorkDir
}
Exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment