Last active
November 5, 2024 07:01
-
-
Save eddiezato/23e73cb09328a984074289636e69dbd7 to your computer and use it in GitHub Desktop.
PowerShell: script to optimize JPEG files with mozjpegtran
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clear-Host | |
$host.ui.RawUI.WindowTitle = "JPEG Optimizer" | |
[Console]::CursorVisible = $false | |
$Host.PrivateData.ProgressBackgroundColor = "Gray" | |
$Host.PrivateData.ProgressForegroundColor = "Black" | |
Class ResultItem { | |
[string]$Name | |
[string]$Files_processed | |
[string]$Size_MB | |
} | |
$result = @() | |
$drs = @() | |
$fls = @() | |
ForEach ($item in (Get-Content -Path $Args[0])) { | |
If (Test-Path -LiteralPath $item -PathType Container) { $drs += (Get-Item -LiteralPath $item) } | |
ElseIf ((Test-Path -LiteralPath $item -PathType Leaf) -and (($item.EndsWith(".jpg")) -or ($item.EndsWith(".jpeg")))) { $fls += (Get-Item -LiteralPath $item) } | |
} | |
$addfls = 0 | |
If ($fls.Count -gt 0) { $addfls = 1 } | |
$activity = "Processing $($drs.Count) folder(s), $($fls.Count) file(s)" | |
For ($i = 0; $i -lt ($drs.Count + $addfls); $i++) { | |
$drname = "" | |
$files = @() | |
If (($addfls -eq 1) -and ($i -eq $drs.Count)) { | |
$drname = "Files" | |
$files = $fls | |
} | |
Else { | |
$drname = $drs[$i].Name | |
$files = (Get-ChildItem -LiteralPath $drs[$i] -Include *.jpg,*.jpeg -File) | |
} | |
$totalp = [Math]::Round($i * 100 / ($drs.Count + $addfls), 0) | |
Write-Progress -Id 1 -Activity $activity -Status $drname -PercentComplete $totalp | |
$resflcount = 0 | |
$ressize = 0 | |
For ($j = 0; $j -lt $files.Count; $j++) { | |
$totalp = [Math]::Round(($i + ($j / $files.Count)) * 100 / ($drs.Count + $addfls), 0) | |
Write-Progress -Id 1 -Activity $activity -Status $drname -PercentComplete $totalp | |
$currentp = [Math]::Round(($j + 1) * 100 / $files.Count, 0) | |
Write-Progress -Id 2 -ParentId 1 -Activity "Optimizing $($files.Count) file(s)" -Status $files[$j].Name -PercentComplete $currentp | |
$tempfl = New-TemporaryFile | |
$data = & jpegtran -copy none -optimize -progressive -outfile $tempfl.FullName $files[$j].FullName 2>&1 | |
If ($?) { | |
$resflcount++ | |
$ressize += $tempfl.Length | |
Rename-Item -LiteralPath $files[$j].FullName -NewName "$($files[$j].Name).bak" | |
Move-Item -Path $tempfl.FullName -Destination $files[$j].FullName | |
} | |
Else { | |
Add-Content -LiteralPath "$($files[$j].Directory.FullName)\_errors.log" -Value "Error - $($files[$j].Name)" | |
Remove-Item -Path $tempfl.FullName | |
} | |
} | |
$size1 = [Math]::Round(($files | Measure-Object -Sum Length).Sum / 1MB, 2) | |
$size2 = [Math]::Round($ressize / 1MB, 2) | |
$result += [ResultItem]@{ Name = $drname; Files_processed = "$($files.Count) -> $resflcount"; Size_MB = "$size1 -> $size2" } | |
Write-Progress -Id 2 -ParentId 1 -Activity "Optimizing $($files.Count) file(s)" -Status "Completed" -Completed | |
} | |
Write-Progress -Id 1 -Activity $activity -Status "Completed" -Completed | |
Write-Output $result | |
[Console]::CursorVisible = $true | |
#$Host.UI.RawUI.ReadKey(6) | Out-Null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment