Last active
November 5, 2024 07:05
-
-
Save eddiezato/9c5e5aafe672c8e2784c6fb0abd5ed6b to your computer and use it in GitHub Desktop.
PowerShell: script to test generation loss (cjpeg, cjpegli, cjxl)
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
param ( | |
[Alias('f')][string] $File = '0.png', | |
[Alias('i')][switch] $Images, | |
[Alias('v')][switch] $Video | |
) | |
function Start-Gen { | |
param ([string] $Encoder, [string] $InFile, [long] $TargetSize, [string[]] $ExOptions = @(), [string] $Title, [string] $Font, [string] $Color) | |
$MergeFile = $InFile -replace '\.png$', '_.png' | |
switch ($Encoder) { 'cjpeg' { $OutExt = '.jpg' }; 'cjpegli' { $OutExt = '.jpg' }; 'avifenc' { $OutExt = '.avif' }; 'cjxl' { $OutExt = '.jxl' }} | |
$OutFile = $InFile -replace '\.png$', $OutExt | |
switch -Exact ($Encoder) { | |
'cjpeg' { | |
cjpeg -quality (Get-Random -Minimum 85 -Maximum 95) -outfile $OutFile $InFile *>&1 | Out-Null | |
magick $OutFile -strip -depth 16 png48:$InFile *>&1 | Out-Null | |
$Title = (((cjpeg -version *>&1) -split ' ')[0,2] -join ' ') + " $Title" | |
} | |
'cjpegli' { | |
cjpegli $InFile $OutFile --target_size=$TargetSize *>&1 | Out-Null | |
djpegli $OutFile $InFile --bitdepth=16 *>&1 | Out-Null | |
$Title = 'cjpegli ' + ((cjxl -V *>&1) -split ' ')[1] + " $Title" | |
} | |
'avifenc' { | |
.\avifenc --target-size $TargetSize $InFile $OutFile *>&1 | Out-Null | |
.\avifdec -d 16 $OutFile $InFile *>&1 | Out-Null | |
$Title = 'avifenc ' + ((.\avifenc -V) -split ' ')[1] + " $Title" | |
} | |
'cjxl' { | |
$L = 0; $R = 20000 - 1 | |
while ($L -ne $R) { | |
$m = [Math]::Ceiling(($L + $R) / 2) | |
$CjxlArgs = @($InFile, $OutFile, '-d', ($m / 10000)) | |
if ($ExOptions.Length) { $CjxlArgs += $ExOptions } | |
cjxl $CjxlArgs *>&1 | Out-Null | |
$A = (Get-Item -Path $OutFile).Length | |
if ($A -lt $TargetSize) { $R = $m - 1 } else { $L = $m } | |
} | |
djxl $OutFile $InFile --bits_per_sample=16 *>&1 | Out-Null | |
$Title = (((cjxl -V *>&1) -split ' ')[0..1] -join ' ') + " $Title" | |
} | |
} | |
$OutSize = (Get-Item -Path $OutFile).Length | |
$Text = $Title + ([Math]::Round($OutSize / 1KB, 1)).ToString('0.0KB').PadLeft(10, ' ') | |
magick $InFile -gravity North -pointsize 24 -undercolor "#00000088" -fill white -font $Font -annotate +0+5 "\ $Text " -depth 16 png48:$MergeFile *>&1 | Out-Null | |
Write-Host '═' -NoNewLine -ForegroundColor $Color | |
if ($Encoder -eq 'cjpeg') { return $OutSize } | |
} | |
if ($Images) { | |
$Iosevka = $Env:LOCALAPPDATA + '\Microsoft\Windows\Fonts\iosevka-regular.ttc' | |
$Temp = $Env:TEMP + '\gl_' + (New-Guid).Guid.Substring(0,8) | |
if (Test-Path -Path $Temp -PathType Container) { Remove-Item -Path $Temp -Force } | |
New-Item -Path $Temp -ItemType Directory | Out-Null | |
"`e[90m[{0}] temp:{1}`e[0m" -f (Get-Date).ToString('HH\:mm\:ss'), $Temp | |
if (-not (Test-Path -Path 'frames' -PathType Container)) { New-Item -Path 'frames' -ItemType Directory | Out-Null } | |
'j', 'l', 'a', 'x' | ForEach-Object { Copy-Item -Path $File -Destination "$Temp\$_.png"} | |
1..900 | ForEach-Object { | |
$Gen = $_.ToString('000') | |
Write-Host ("`e[90m[{0}]`e[0m {1} " -f (Get-Date).ToString('HH\:mm\:ss'), $Gen) -NoNewLine | |
$T = Start-Gen -Encoder 'cjpeg' -InFile "$Temp\j.png" -Font $Iosevka -Color 'Yellow' | |
$cjpegli = @{ Encoder = 'cjpegli'; InFile = "$Temp\l.png"; TargetSize = $T; Title = ''; Font = $Iosevka; Color = 'Magenta' } | |
$avifenc = @{ Encoder = 'avifenc'; InFile = "$Temp\a.png"; TargetSize = $T; Title = ''; Font = $Iosevka; Color = 'Red' } | |
$cjxl_gaborish = @{ Encoder = 'cjxl'; InFile= "$Temp\x.png"; TargetSize = $T; ExOptions = @('--gaborish=0'); Title = '--gaborish=0'; Font = $Iosevka; Color = 'Cyan' } | |
$funcDef = ${function:Start-Gen}.ToString() # send function to background foreach | |
$cjpegli, $avifenc, $cjxl_gaborish | ForEach-Object -ThrottleLimit 3 -Parallel { | |
${function:Start-Gen} = $using:funcDef # get function | |
Start-Gen @_ | |
} | |
magick "$Temp\j_.png" "$Temp\l_.png" "$Temp\a_.png" "$Temp\x_.png" +append -depth 16 png48:"frames\f$Gen.png" *>&1 | Out-Null | |
Write-Host '╣' -ForegroundColor 'Green' | |
} | |
if (Test-Path -Path $Temp -PathType Container) { Remove-Item -Path $Temp -Force -Recurse } | |
} | |
if ($Video) { | |
$CascadiaCode = ($Env:LOCALAPPDATA).Replace('\','/').Replace(':','\:') + '/Microsoft/Windows/Fonts/cascadiacode.ttf' | |
$VF = -join @( | |
"settb=1/1000,setpts='if(eq(N,0),0,PREV_OUTPTS+100-67*(min(N,420)/420))',", | |
"drawtext=fontfile='$CascadiaCode':", | |
"text='%{frame_num}':start_number=1:x=(w-tw)/2:y=(h-th-5):fontcolor=black:", | |
"fontsize=64:box=1:boxcolor=#ffffff88:boxborderw=3" | |
) | |
ffmpeg -hide_banner -framerate 30 -start_number 1 -i frames\f%03d.png -vf $VF -fps_mode vfr -enc_time_base 1/1000 ` | |
-c:v libvpx-vp9 -crf 10 -pix_fmt yuv420p -an -y out.webm | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment