-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
scaler.ps1
executable file
·289 lines (250 loc) · 7 KB
/
scaler.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/env pwsh
$ScalerScriptName = $MyInvocation.MyCommand.Name
class Program {
[string]$binary;
[string]$program;
[string]$link;
[string]$message = "If you need to build from source, see:";
}
function failproc([Program]$program) {
fail $program.program $program.link $program.message
}
function fail([string]$program, [string]$link, [string]$message) {
Write-Warning "Please install ``$program``. $message $link"
Write-Warning "If you already have, put the path to the binary's folder in your ```$env:PATH`` variable."
Write-Warning ""
Write-Warning "Example: ```$env:PATH = `"/path/to/bin$( [IO.Path]::PathSeparator )`$env:PATH`"``"
Write-Warning ""
Write-Warning "If you're calling from a POSIX shell, you can stack on the ``$program`` environment."
Write-Warning ""
Write-Warning "Example: ``$program=/path/to/bin pwsh $ScalerScriptName``"
if ( (Read-Host "Would you like to open ``$link``?").StartsWith('y')) {
Start-Process "$link"
}
exit 1
}
function waitForJobs() {
Get-Job | Foreach-Object {
Write-Output "`n=== $( $_.command ) ===`n";
Wait-Job $_ > $null;
Receive-Job $_;
Remove-Job $_;
}
Write-Output "`n=== End of Task List ===`n"
}
(Get-Item -Path @("env:ffmpeg", "env:jpegli", "env:webp", "env:libavif") 2> $null).Value |
ForEach-Object { $env:PATH = "$_$( [IO.Path]::PathSeparator )$env:PATH" }
@(
[Program]@{ binary = "ffmpeg"; program = "ffmpeg"; link = "https://ffmpeg.org/"; message = "If you need to download FFmpeg, see:" },
[Program]@{ binary = "cjpegli"; program = "jpegli"; link = "https://github.com/google/jpegli" },
[Program]@{ binary = "cwebp"; program = "webp"; link = "https://developers.google.com/speed/webp/docs/precompiled"; message = "If you need to download WebP, see:" },
[Program]@{ binary = "avifenc"; program = "avif"; link = "https://github.com/AOMediaCodec/libavif" }
) | ForEach-Object { if (!(Get-Command $_.binary 2> $null)) {
failproc $_
} }
# Arguments:
$argType = @{
sizes = "sizes";
s = "sizes";
jpeg = "jpeg";
webp = "webp";
avif = "avif";
j = "jpeg";
w = "webp";
a = "avif";
}
$argShot = @{
force = "force";
move = "move";
f = "force";
m = "move";
}
$argRem = @();
$argMap = @{
# 720p and 1440p was deemed unlikely to be hit often to be worth supporting.
# sizes = "360,720,1080,1440,2160";
sizes = "360,1080,2160";
# Previews should ideally have a high quality output, but realistically,
# it's not going to be seen in full screen in isolation.
jpeg = "d=2,yuv=444";
avif = "s=0,q=60,yuv=444,ignoreExif,ignoreXmp";
# webp = "preset=photo,m=6,z=9,q=90,noalpha";
# jxl = "brotliEffort=11,e=10"
};
for($i = 0; $i -lt $args.length; $i++) {
$arg = $args[$i];
if (!$arg.startsWith('-')) {
$argRem += $arg;
continue;
}
if ($arg -eq '--') {
$argRem += $args[++$i..$args.length];
break;
}
$arg = $arg.substring(1);
if ($null -ne $argShot[$arg]) {
$argMap[$argShot[$arg]] = 1;
continue;
}
if ($null -eq $argType[$arg]) {
throw [Exception]::new("Unknown argument: $arg");
}
$arg = $argType[$arg];
$currentArg = $args[++$i];
if ($currentArg -eq '-') {
$argMap.remove($arg);
} else {
$argMap[$arg] = $currentArg;
}
}
$sizes = $argMap['sizes'].split(',')
$ffmpegArgs = @();
$map = 0;
$argRem = $argRem | % { $_.startsWith('file://') ? $_.substring(7) : $_ }
foreach ($arg in $argRem) {
$paths = $arg.split([IO.Path]::PathSeparator);
$image = $paths[0];
if (!(Get-Item $image 2> $null)) {
Write-Warning "Unknown file: $image";
continue;
}
$output = $null -ne $paths[1] ? $paths[1] : [IO.Path]::GetFileNameWithoutExtension($image);
if (!($argMap["force"]) -and !($sizes | % { !(Get-Item "$output-${_}p.png") 2> $null } | ? { $_ -eq $true })) {
Write-Warning "Skipping $output for $image";
continue;
}
$ffmpegArgs += @("-i"; $image);
$outputMap = $map++;
foreach ($size in $sizes) {
if (!($argMap["force"]) -and (Get-Item "$output-${size}p.png" 2> $null)) {
Write-Warning "Skipping $output-${size}p.png for $image";
continue;
}
$ffmpegArgs += @("-map"; [string]($outputMap); "-frames:v"; "1"; "-update"; "1"; "-vf"; "scale=-1:$size"; "-sws_flags"; "lanczos"; "$output-${size}p.png");
}
}
if($ffmpegArgs) {
if($argMap["force"]) {
$ffmpegArgs += "-y"
}
Write-Warning "ffmpeg $ffmpegArgs"
ffmpeg $ffmpegArgs
}
if (Get-Command "oxipng" 2> $null) {
[Array]$params = $argRem |
% {
$_ = $_.split([IO.Path]::PathSeparator);
$null -ne $_[1] ? $_[1] : [IO.Path]::GetFileNameWithoutExtension($_[0]);
} |
% { $o=$_; $sizes | % {"$o-${_}p.png" } }
oxipng -o max -a -s $params &
}
$jpegliArgMap = @{
yuv = "--chroma_subsampling=";
xyb = "--xyb";
d = "-d";
q = "-q";
distance = "-d";
quality = "-q";
}
$webpArgMap = @{
preset = "-preset";
m = "-m";
z = "-z";
q = "-q";
quality = "-q";
near_lossess = "-near_lossless";
lossless = "-lossless";
noalpha = "-noalpha";
}
$jpegxlArgMap = @{
brotliEffort = "--brotli_effort=";
d = "-d";
q = "-q";
e = "-e";
distance = "-d";
quality = "-q";
effort = "-e";
}
$avifArgMap = @{
y = "-y";
s = "-s";
q = "-q";
yuv = "-y";
speed = "-s";
quality = "-q";
ignoreExif = "--ignore-exif";
ignoreXmp = "--ignore-xmp";
}
function toArgs([String]$argInput, [HashTable]$argMap) {
$output = @();
foreach ($argRaw in $argInput.split(',')) {
$arg = $argRaw.split('=');
$zeroth = $arg[0]
$k = $argMap[$zeroth]
if ($null -eq $k) {
Write-Error "Unknown argument $argRaw";
continue;
}
if ( $k.endsWith('=')) {
$output += "$k$( $arg[1] )";
} else {
$output += @($k; $arg[1]);
}
}
return $output;
}
$runners = @();
foreach ($item in $argMap.getEnumerator()) {
switch ($item.key) {
"jpeg" {
$jpegArgs = $( toArgs $item.value $jpegliArgMap );
$runners += { cjpegli $args[0] "$( $args[1] )-$( $args[2] )p.jpeg" $jpegArgs & }
}
"jxl" {
$jxlArgs = $( toArgs $item.value $jpegxlArgMap );
$runners += { cjxl $args[0] "$( $args[1] )-$( $args[2] )p.jxl" $jxlArgs & }
}
"webp" {
$webpArgs = $( toArgs $item.value $webpArgMap );
$runners += { cwebp $webpArgs $args[0] -o "$( $args[1] )-$( $args[2] )p.webp" & }
}
"avif" {
$avifArgs = $( toArgs $item.value $avifArgMap );
$runners += { avifenc $avifArgs $( $args[0] ) "$( $args[1] )-$( $args[2] )p.avif" & }
}
}
}
foreach ($arg in $argRem) {
$paths = $arg.split([IO.Path]::PathSeparator);
$image = $paths[0];
$output = $null -ne $paths[1] ? $paths[1] : [IO.Path]::GetFileNameWithoutExtension($image)
if (!(Get-Item $image 2> $null)) {
Write-Warning "Unknown file $image (derived from $arg)"
continue;
}
foreach ($size in $sizes) {
$sizedOutput = "$output-${size}p.png";
if (!(Get-Item $sizedOutput 2> $null)) {
Write-Warning "Unknown file $sizedOutput (derived from $arg)"
continue;
}
foreach ($runner in $runners) {
& $runner $sizedOutput $output $size
}
}
}
waitForJobs
if($argMap['move']) {
$params = $argRem | % {
$_ = $_.split([IO.Path]::PathSeparator);
if($_[1]) {
$output = "$($_[1])$([IO.Path]::GetExtension($_[0]))";
Move-Item -Path $_[0] -Destination $output;
Write-Output $output;
} else {
Write-Output $_[0];
}
}
oxipng -o max -a -s $params
}