-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVifpics.ps1
1829 lines (1474 loc) · 60.1 KB
/
Vifpics.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Set Vifpics global variables.
$Version = "0.1.0"
$TempPath = "$Env:TEMP\Vifpics"
$SupportedAnimations = @("gif", "webp", "png", "apng", "avif") | Sort-Object
$SupportedImages = @("gif", "webp", "png", "apng", "avif", "jpg", "jpeg") | Sort-Object
$SupportedVideos = @("mp4", "mkv", "webm") | Sort-Object
$SupportedInputTypes = @("file", "dir") | Sort-Object
$TimeCodePattern = '^([0-5]\d)[:\.]([0-5]\d)$'
$TimeCodePattern2 = '^([0-5]\d)[:\.]([0-5]\d)[:\.]([0-5]\d)$'
$TimeCodePattern3 = '^(?:(\d+):)?([0-5]?\d)(?:\.(\d{1,2}))?$'
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) } # For sorting files by natual order.
# Setup executable locations.
$Commands = @("ffmpeg", "ffprobe", "gifski", "apngasm")
foreach ($Command in $Commands) {
if (Test-Path ("$PSScriptRoot\$Command.exe")) {
New-Variable -Name "$Command" -Value "$PSScriptRoot\$Command.exe"
} else {
New-Variable -Name "$Command" -Value "$Command.exe"
}
}
function New-VifpicsOptions
{
$NewOptions = [PSCustomObject]@{
InputPath = ""
OutputPath = ""
Format = "gif"
Animate = $True
MergeFormat = $null
Frames = $False
Start = "0:00"
Duration = 1
Timestamps = $null
NoTimeLimit = $False
NoFilter = $False
Preset = $null
Size = $null
Width = -1
Height = -1
NoAutoScale = $False
FPS = $null
NoLoop = $False
Encoder = "ffmpeg"
Dither = "sierra2_4a"
GifskiQuality = 100
WebPCompression = 4
WebPQuality = 75
PNGCompressMethod = "-z0"
LibSVT = $False
LibAOM = $False
AVIFQ = 8
CRF = 21
FilmGrain = 8
Resolution = $null
ForceOriginalAspectRatio = $null
LoopOption = $null
}
Return $NewOptions
}
function Main
{
begin {
$OptionsData = New-VifpicsOptions
# Get user input from interactive mode.
Start-Interactive -OptionsData $OptionsData
# Check and set input file data.
$InputData = Test-Input $OptionsData.InputPath
# Set timestams
$TimestampsData = Set-TimeStamps -InputData $InputData -OptionsData $OptionsData
# Apply encoder preset.
Set-EncoderPreset -OptionsData $OptionsData
# Apply size preset.
Set-SizePreset -OptionsData $OptionsData
# Apply resolution.
Set-Resolution -OptionsData $OptionsData
}
process {
if ($OptionsData.Animate) {
New-Animation -InputData $InputData -OptionsData $OptionsData -TimestampsData $TimestampsData
} elseif ($OptionsData.Frames) {
New-Frames -InputData $InputData -OptionsData $OptionsData -TimestampsData $TimestampsData
} else {
Show-ErrorMessage "Invalid task."
}
}
end {
Write-Host "Done." -ForegroundColor Cyan
Pause
Main
}
}
<#
INTERACTIVE MENU
________________________________________________________________________________
#>
function Show-MenuHeader
{
[console]::CursorVisible = $False
Write-Host "Vifpics Video To Animation by DeAndre Queary" -ForegroundColor Green
Write-Host "Current Location: $PWD"
Write-Host "UP - go up / DOWN - go down / ENTER - select / enter [\]/ESC - go home"
Write-Host "-----------------------------------------------------------------------------------"
}
function Start-Interactive
{
param (
[int32] $CurrentSelection = 0,
[object] $OptionsData
)
Clear-Host
Show-MenuHeader
$Key = 0
Write-Host "CHOOSE A TASK:" -ForegroundColor Magenta
$Options = 0..5
$Options[0] = "📹 Create Animation"
$Options[1] = "✂️ Generate Frames"
$Options[2] = "▶️ Show Formats"
$Options[3] = "⬇️ Download Encoders"
$Options[4] = "❔ About"
$Options[5] = "🚪 LEAVE!"
Draw-MenuOptions -Options $Options -POS $CurrentSelection
while ($Key -ne 13 -and $Key -ne 27) {
$Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
if ($Key -eq 13 -and $CurrentSelection -eq 0)
{
Start-CreateAnimation -OptionsData $OptionsData
}
if ($Key -eq 13 -and $CurrentSelection -eq 1)
{
Start-CreateFrames -OptionsData $OptionsData
}
if ($Key -eq 13 -and $CurrentSelection -eq 2)
{
Show-Formats
}
if ($Key -eq 13 -and $CurrentSelection -eq 3)
{
Start-DownloadEncoders
}
if ($Key -eq 13 -and $CurrentSelection -eq 4)
{
Show-About
}
if ($Key -eq 13 -and $CurrentSelection -eq 5)
{
$Exit = $True
}
$Key, $Options, $CurrentSelection = Get-UpDownControls -Key $Key -Options $Options -CurrentSelection $CurrentSelection
# ESC KEY
if ($Key -eq 27 -or $Key -eq 220 -or ($Key -eq 13 -and $Exit))
{
Write-Host "Goodbye.🙋🏾♂️" -ForegroundColor Green
Start-Sleep 0.5
Clear-Host
EXIT
}
}
}
function Start-CreateAnimation
{
param (
[int32] $CurrentSelection = 0,
[object] $OptionsData
)
Clear-Host
Show-MenuHeader
$Key = 0
[console]::CursorVisible = $True
Write-Host "CREATE ANIMATION:" -ForegroundColor Magenta
Write-Host "Enter video, image, or folder path." -ForegroundColor Cyan
$InteractiveInput = Start-GetInput
$InputPath = $InteractiveInput.InputPath
Write-Host "SOURCE:"`"$InputPath`" -ForegroundColor Yellow
Write-Host "-----------------------------------"
[console]::CursorVisible = $False
Write-Host "Select animation format." -ForegroundColor Cyan
$Options = 0..10
$Options[0] = "Standard Animated GIF (fast encode)"
$Options[1] = " High Quality Animated GIF (slow encode)"
$Options[2] = " Best Quality Animated GIF (requires gifski)"
$Options[3] = "Animated WebP"
$Options[4] = "Animated PNG"
$Options[5] = " Optimized Animated PNG (requires apngasm)"
$Options[6] = "Animated AVIF (fast encode)"
$Options[7] = " Best Quality Animated AVIF (slow encode)"
$Options[8] = "MP4 Video"
$Options[9] = "MKV Video"
$Options[10] = "WebM Video"
Draw-MenuOptions -Options $Options -POS $CurrentSelection
while ($Key -ne 13 -and $Key -ne 27) {
$Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
if ($Key -eq 13 -and $CurrentSelection -eq 0) {$Preset = "standard"}
if ($Key -eq 13 -and $CurrentSelection -eq 1) {$Preset = "hq"}
if ($Key -eq 13 -and $CurrentSelection -eq 2) {$Preset = "best"}
if ($Key -eq 13 -and $CurrentSelection -eq 3) {$Preset = "webp"}
if ($Key -eq 13 -and $CurrentSelection -eq 4) {$Preset = "png"}
if ($Key -eq 13 -and $CurrentSelection -eq 5) {$Preset = "pngopt"}
if ($Key -eq 13 -and $CurrentSelection -eq 6) {$Preset = "avif"}
if ($Key -eq 13 -and $CurrentSelection -eq 7) {$Preset = "hq-avif"}
if ($Key -eq 13 -and $CurrentSelection -eq 8) {$Preset = "mp4"}
if ($Key -eq 13 -and $CurrentSelection -eq 9) {$Preset = "mkv"}
if ($Key -eq 13 -and $CurrentSelection -eq 10) {$Preset = "webm"}
$Key, $Options, $CurrentSelection = Get-UpDownControls -Key $Key -Options $Options -CurrentSelection $CurrentSelection
# ESC KEY
Invoke-InteractiveEsc
}
if (($Preset -eq "best") -and -not (Test-Command "gifski")) {
Write-Host "Gifski must be installed in order to create higher quality GIFs." -ForegroundColor DarkRed
Write-Host "Visit: https://github.com/ImageOptim/gifski or `nselect Download Encoders from main menu." -ForegroundColor Cyan
Pause
Start-CreateAnimation -OptionsData $OptionsData
Return
}
if ($Preset -eq "pngopt" -and -not (Test-Command "apngasm")) {
Write-Host "apngasm must be installed in order to create optimized PNGs." -ForegroundColor DarkRed
Write-Host "Visit: https://apngasm.sourceforge.net or `nselect Download Encoders from main menu." -ForegroundColor Cyan
Pause
Start-CreateAnimation -OptionsData $OptionsData
Return
}
Write-Host "ANIMATION PRESET:"$Options[$CurrentSelection] -ForegroundColor Yellow
Write-Host "-----------------------------------"
$Key = 0
$CurrentSelection = 0
Write-Host "Select size preset. Will autoscale based on height of video." -ForegroundColor Cyan
$Options = 0..15
$Options[0] = "Original Resolution"
$Options[1] = "Tiny (16x16)"
$Options[2] = "Icon (32x32)"
$Options[3] = "Big Icon (64x64)"
$Options[4] = "Small (128x128)"
$Options[5] = "Twitch Emote (128x128/no autoscale)"
$Options[6] = "Twitch Emote WIde (336x128/no autoscale)"
$Options[7] = "Medium (256x256)"
$Options[8] = "Large (512x512)"
$Options[9] = "Web (640x360)"
$Options[10] = "SD (640x480)"
$Options[11] = "HD (1280x720)"
$Options[12] = "Full HD (1920x1080)"
$Options[13] = "2K (2560x1440)"
$Options[14] = "4K (3840x2160)"
$Options[15] = "8K (7680x4320/experimental)"
Draw-MenuOptions -Options $Options -POS $CurrentSelection
while ($Key -ne 13 -and $Key -ne 27) {
$Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
if ($Key -eq 13 -and $CurrentSelection -eq 0) {$Size = "original"}
if ($Key -eq 13 -and $CurrentSelection -eq 1) {$Size = "tiny"}
if ($Key -eq 13 -and $CurrentSelection -eq 2) {$Size = "icon"}
if ($Key -eq 13 -and $CurrentSelection -eq 3) {$Size = "big-icon"}
if ($Key -eq 13 -and $CurrentSelection -eq 4) {$Size = "small"}
if ($Key -eq 13 -and $CurrentSelection -eq 5) {$Size = "emote"}
if ($Key -eq 13 -and $CurrentSelection -eq 6) {$Size = "wide-emote"}
if ($Key -eq 13 -and $CurrentSelection -eq 7) {$Size = "medium"}
if ($Key -eq 13 -and $CurrentSelection -eq 8) {$Size = "large"}
if ($Key -eq 13 -and $CurrentSelection -eq 9) {$Size = "web"}
if ($Key -eq 13 -and $CurrentSelection -eq 10) {$Size = "sd"}
if ($Key -eq 13 -and $CurrentSelection -eq 11) {$Size = "hd"}
if ($Key -eq 13 -and $CurrentSelection -eq 12) {$Size = "fhd"}
if ($Key -eq 13 -and $CurrentSelection -eq 13) {$Size = "2k"}
if ($Key -eq 13 -and $CurrentSelection -eq 14) {$Size = "4k"}
if ($Key -eq 13 -and $CurrentSelection -eq 15) {$Size = "8k"}
$Key, $Options, $CurrentSelection = Get-UpDownControls -Key $Key -Options $Options -CurrentSelection $CurrentSelection
# ESC KEY
Invoke-InteractiveEsc
}
Write-Host "SIZE PRESET:"$Size -ForegroundColor Yellow
Write-Host "-----------------------------------"
[console]::CursorVisible = $False
$Key = 0
$CurrentSelection = 0
Write-Host "Set FPS." -ForegroundColor Cyan
$Options = 0..9
$Options[0] = "30fps"
$Options[1] = "60fps"
$Options[2] = "50fps"
$Options[3] = "40fps"
$Options[4] = "25fps"
$Options[5] = "20fps"
$Options[6] = "15fps"
$Options[7] = "10fps"
$Options[8] = "5fps"
$Options[9] = "2fps"
Draw-MenuOptions -Options $Options -POS $CurrentSelection
while ($Key -ne 13 -and $Key -ne 27) {
$Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
$FPS = $Options[$CurrentSelection]
$Key, $Options, $CurrentSelection = Get-UpDownControls -Key $Key -Options $Options -CurrentSelection $CurrentSelection
# ESC KEY
Invoke-InteractiveEsc
}
Write-Host "FPS:"$FPS -ForegroundColor Yellow
$FPS = $FPS -replace "[a-zA-Z]", ""
Write-Host "-----------------------------------"
if ($InteractiveInput.InputType -ne "dir" -and -not ($InteractiveInput.InputFormat -in ($SupportedImages))) {
$Start = Start-GetStartTimecode
Write-Host "START AT:"$Start -ForegroundColor Yellow
Write-Host "-----------------------------------"
$Duration = Start-GetEndTimeCode
if ($Duration -lt 1 -and (Test-Integer $Duration)) {
$NoTimeLimit = $True
Write-Host "DURATION: No Time Limit." -ForegroundColor Yellow
} else {
Write-Host "DURATION:"$Duration -ForegroundColor Yellow
}
Write-Host "-----------------------------------"
} else {
$Start = "0:00"
$Duration = $null
$NoTimeLimit = $null
}
if ($Preset -ne "mp4" -and $Preset -ne "mkv" -and $Preset -ne "webm") {
[console]::CursorVisible = $False
$Key = 0
$CurrentSelection = 0
$Options = 0..1
$Options[0] = "Yes"
$Options[1] = "No"
Write-Host "Loop animation?" -ForegroundColor Cyan
Draw-MenuOptions -Options $Options -POS $CurrentSelection
while ($Key -ne 13 -and $Key -ne 27) {
$Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
if ($Key -eq 13 -and $CurrentSelection -eq 0)
{
$NoLoop = $False
}
if ($Key -eq 13 -and $CurrentSelection -eq 1)
{
$NoLoop = $True
}
$Key, $Options, $CurrentSelection = Get-UpDownControls -Key $Key -Options $Options -CurrentSelection $CurrentSelection
# ESC KEY
Invoke-InteractiveEsc
}
if ($NoLoop -eq $False) {
Write-Host "LOOP: Yes" -ForegroundColor Yellow
$NoLoop = $False
} else {
Write-Host "LOOP? No" -ForegroundColor Yellow
$NoLoop = $True
}
Write-Host "-----------------------------------"
} else {
$NoLoop = $null
}
[console]::CursorVisible = $True
Write-Host "Enter output filename path without extension." -ForegroundColor Cyan
Write-Host "If left blank, a filename will be generated." -ForegroundColor Cyan
$OutputPath = Read-Host "[Enter output path]"
Cancel-Read -Option $OutputPath
if (-not $OutputPath) {
Write-Host "FILENAME: Autogenerated" -ForegroundColor Yellow
} else {
Write-Host "FILENAME:"$OutputPath -ForegroundColor Yellow
}
Write-Host "-----------------------------------"
[console]::CursorVisible = $True
Write-Host "Confirm. Create animation?" -ForegroundColor Cyan
do {
$Confirm = Read-Host "[yes (y) / no (n)]"
} while ($Confirm -ne "y" -and $Confirm -ne "yes" -and $Confirm -ne "n" -and $Confirm -ne "no" -and $Confirm -ne "\")
if ($Confirm -eq "y" -or $Confirm -eq "yes") {
$OptionsData.Animate = $True
$OptionsData.Frames = $False
$OptionsData.InputPath = $InputPath
$OptionsData.Start = $Start
$OptionsData.Duration = $Duration
$OptionsData.NoTimeLimit = $NoTimeLimit
$OptionsData.Preset = $Preset
$OptionsData.Size = $Size
$OptionsData.FPS = $FPS
$OptionsData.NoLoop = $NoLoop
$OptionsData.OutputPath = $OutputPath
} else {
Start-CreateAnimation -OptionsData $OptionsData
Return
}
}
function Start-CreateFrames
{
param (
[int32] $CurrentSelection = 0,
[object] $OptionsData
)
Clear-Host
Show-MenuHeader
$Key = 0
[console]::CursorVisible = $True
Write-Host "GENERATE FRAMES:" -ForegroundColor Magenta
Write-Host "Enter video or animated image path." -ForegroundColor Cyan
$InteractiveInput = Start-GetInput
$InputPath = $InteractiveInput.InputPath
Write-Host "SOURCE:"`"$InputPath`" -ForegroundColor Yellow
Write-Host "-----------------------------------"
[console]::CursorVisible = $False
Write-Host "Select frame format." -ForegroundColor Cyan
$Options = 0..3
$Options[0] = "PNG"
$Options[1] = "JPG"
$Options[2] = "WebP"
$Options[3] = "AVIF"
Draw-MenuOptions -Options $Options -POS $CurrentSelection
while ($Key -ne 13 -and $Key -ne 27) {
$Key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").VirtualKeyCode
if ($Key -eq 13 -and $CurrentSelection -eq 0) {$Format = "png"}
if ($Key -eq 13 -and $CurrentSelection -eq 1) {$Format = "jpg"}
if ($Key -eq 13 -and $CurrentSelection -eq 2) {$Format = "webp"}
if ($Key -eq 13 -and $CurrentSelection -eq 3) {$Format = "avif"}
$Key, $Options, $CurrentSelection = Get-UpDownControls -Key $Key -Options $Options -CurrentSelection $CurrentSelection
# ESC KEY
Invoke-InteractiveEsc
}
Write-Host "OUTPUT FORMAT:"$Format -ForegroundColor Yellow
Write-Host "-----------------------------------"
[console]::CursorVisible = $True
$Start = Start-GetStartTimecode
Write-Host "START AT:"$Start -ForegroundColor Yellow
Write-Host "-----------------------------------"
$Duration = Start-GetEndTimeCode
if ($Duration -eq "") {
Write-Host "DURATION: No Time Limit." -ForegroundColor Yellow
} else {
Write-Host "DURATION:"$Duration -ForegroundColor Yellow
}
Write-Host "-----------------------------------"
Write-Host "Confirm. Create frames?" -ForegroundColor Cyan
do {
$Confirm = Read-Host "[yes (y) / no (n)]"
} while ($Confirm -ne "y" -and $Confirm -ne "yes" -and $Confirm -ne "n" -and $Confirm -ne "no" -and $Confirm -ne "\")
if ($Confirm -eq "y" -or $Confirm -eq "yes") {
$OptionsData.Frames = $True
$OptionsData.Animate = $False
$OptionsData.InputPath = $InputPath
$OptionsData.Format = $Format
$OptionsData.Start = $Start
$OptionsData.Duration = $Duration
$OptionsData.NoTimeLimit = $NoTimeLimit
} else {
Main Start-CreateFrames -OptionsData $OptionsData
Return
}
}
function Start-GetInput
{
do {
$InputPath = Read-Host "[Enter path]"
Cancel-Read -Option $InputPath
} while ($InputPath -eq "")
$InputPath = Trim-String -String $InputPath
$InputPath = $InputPath.Split(",")
$InputPath = $InputPath.Trim()
$InteractiveInput = Test-Input $InputPath
if (($InteractiveInput.InputType -eq "file" -or $InteractiveInput.InputType -eq "dir") -and -not (Test-Path "$InputPath")) {
Show-ErrorMessage -Message "File path `"$InputPath`" doesn't exist. Try again."
}
# End script if input file or folder doesn't exist.
if ($InteractiveInput.InputType -ne "file" -and $InteractiveInput.InputType -ne "dir") {
Show-ErrorMessage -Message "Invalid source. Try again."
}
Return $InteractiveInput
}
function Start-GetStartTimecode
{
[console]::CursorVisible = $True
Write-Host "Enter start timecode. Like 1:30 or 1.30. Default: 0:00." -ForegroundColor Cyan
do {
$Start = Read-Host "[Enter start timecode]"
Cancel-Read -Option $Start
} while ($Start -ne "" -and (-not ($Start -match $TimeCodePattern) -and -not ($Start -match $TimeCodePattern2) -and -not ($Start -match $TimeCodePattern3)))
$Start = Trim-String -String $Start
if ($Start -eq "") {
$Start = "0:00"
}
Return $Start
}
function Start-GetEndTimeCode
{
[console]::CursorVisible = $True
Write-Host "Enter animation duration in seconds or 0 for no duration limit. Default: 1." -ForegroundColor Cyan
$Duration = Read-Host "[Enter duration]"
Cancel-Read -Option $Duration
if ($Duration -lt 1 -and -not (Test-Integer $Duration)) {
$Duration = 1
}
if (-not (Test-Integer $Duration)) {
$Duration = 1
}
$Duration = Trim-String -String $Duration
Return $Duration
}
function Get-UpDownControls
{
param (
[int32] $Key,
[array] $Options,
[int32] $CurrentSelection
)
# UP ARROW
if ($Key -eq 38)
{
$CurrentSelection--
}
# DOWN ARROW
if ($Key -eq 40)
{
$CurrentSelection++
}
# DOWN ARROW ON LAST ITEM GOES BACK TO FIRST ITEM
if ($CurrentSelection -eq $Options.count)
{
$CurrentSelection = 0
}
# UP ARROW ON FIRST ITEM GOES DOWN TO LAST ITEM
if ($CurrentSelection -lt 0)
{
$CurrentSelection = $Options.count - 1
}
if ($Key -ne 27 -and $Key -ne 13)
{
try {
$NewPOS = [System.Console]::CursorTop - $Options.Length
[System.Console]::SetCursorPosition(0, $NewPOS)
} catch {
Clear-Host
Show-MenuHeader
}
Draw-MenuOptions -Options $Options -POS $CurrentSelection
}
Return $Key, $Options, $CurrentSelection
}
function Start-DownloadEncoders {
Clear-Host
Show-MenuHeader
[console]::CursorVisible = $True
$FilePath = "$PSScriptRoot"
$WithErrors = $False
Write-Host "Would you like to download encoders?" -ForegroundColor Cyan
Write-Host "This will download ffmpeg, ffprobe, gifski, and `napngasm from DeAndre Queary's CDN." -ForegroundColor Cyan
do {
$Confirm = Read-Host "[no (n) / yes (y)]"
} while ($Confirm -ne "y" -and $Confirm -ne "yes" -and $Confirm -ne "n" -and $Confirm -ne "no")
if ($Confirm -eq "y" -or $Confirm -eq "yes") {
$EncodersToGet = @("ffmpeg", "ffprobe", "gifski", "apngasm")
foreach($Encoder in $EncodersToGet) {
Write-Host "`nDownloading $Encoder..." -ForegroundColor Cyan
Write-Host "FROM: https://cdn.deandrequeary.com/vifpics/public/encoders/$Encoder.exe" -ForegroundColor Blue
Start-Sleep 0.5
try {
Write-Host "Checking connection..." -ForegroundColor Blue
$Response = Invoke-WebRequest -Uri "https://cdn.deandrequeary.com/vifpics/public/encoders/$Encoder.exe" -ErrorAction SilentlyContinue
} catch {
Write-Host "Connection error. Could not download $Encoder.exe. Skipping...`n" -ForegroundColor DarkRed
Start-Sleep 1.5
$WithErrors = $True
Continue
}
Write-Host "Connection successful..." -ForegroundColor Green
Start-Sleep 0.5
curl -o "$FilePath\$Encoder.exe" "https://cdn.deandrequeary.com/vifpics/public/encoders/$Encoder.exe"
}
if ($WithErrors -eq $True) {
Write-Host "Finished with errors." -ForegroundColor DarkRed
Pause
Main
} else {
Write-Host "Done." -ForegroundColor Green
Write-Host "Please restart Vifpics." -ForegroundColor Cyan
Pause
EXIT
}
}
Main
}
function Show-Formats
{
Clear-Host
Show-MenuHeader
Write-Host "SUPPORTED ANIMATION INPUT/OUTPUT FORMATS:" -ForegroundColor Cyan
foreach ($f in $SupportedAnimations) {
if($f -eq "webp") {
Write-Host $f.ToUpper()"*"
} else {
Write-Host $f.ToUpper()
}
}
Write-Host "`nSUPPORTED IMAGE INPUT FORMATS (for merging):" -ForegroundColor Cyan
foreach ($f in $SupportedImages) {
if ($f -ne "gif") {
Write-Host $f.ToUpper()
}
}
Write-Host "`nSUPPORTED IMAGE OUTPUT FORMATS (for frames):" -ForegroundColor Cyan
Write-Host "PNG"
Write-Host "JPG"
Write-Host "WEBP"
Write-Host "AVIF"
Write-Host "`nSUPPORTED VIDEO INPUT FORMATS:" -ForegroundColor Cyan
foreach ($f in $SupportedVideos) {
Write-Host $f.ToUpper()
}
Write-Host "`nNOTE: Converting FROM WebP is not supported in this version.`n" -ForegroundColor Yellow
Pause
Main
}
function Show-About
{
Clear-Host
Show-MenuHeader
Write-Host "❔ ABOUT" -ForegroundColor Cyan
Write-Host " Version: $Version (light) - 2023" -ForegroundColor Green
Write-Host " GitHub Repo - https://github.com/drequeary/vifpics-light"
Write-Host "------------------------------------------------------------------------------"
Write-Host "🔗 LINKS" -ForegroundColor Cyan
Write-Host " ffmpeg https://ffmpeg.org/"
Write-Host " ffmpeg (windows builds) https://www.gyan.dev/ffmpeg/builds/"
Write-Host " ffprobe https://ffmpeg.org/ffprobe.html"
Write-Host " gifski https://github.com/ImageOptim/gifski/"
Write-Host " apngasm https://apngasm.sourceforge.net/"
Write-Host "------------------------------------------------------------------------------"
Write-Host "🥷 CREDITS" -ForegroundColor Cyan
Write-Host " DeAndre Queary"
Write-Host " GitHub: https://github.com/drequeary"
Write-Host " Contact: contact@deandrequeary.com"
Write-Host "😭 SPECIAL THANKS" -ForegroundColor Magenta
Write-Host " NabiKAZ on GitHub for making video2gif.bat."
Write-Host " https://github.com/NabiKAZ/video2gif"
Write-Host " -------------"
Write-Host " kornelski for developing Gifski."
Write-Host " https://github.com/kornelski/"
Write-Host " -------------"
Write-Host " The following articles on creating high quality GIFs."
Write-Host " https://www.bannerbear.com/blog/how-to-make-a-gif-from-a-video-using-ffmpeg/"
Write-Host " https://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html/"
Write-Host " -------------"
Write-Host " All the developers behind ffmpeg and the optional encoders. This script does nothing without them." -ForegroundColor Yellow
Pause
Main
}
function Draw-MenuOptions
{
param (
[array] $Options,
[int32] $POS
)
for($i = 0; $i -le $Options.Count; $i++) {
if ($null -ne $Options[$i]) {
if ($i -eq $POS) {
Write-Host "➡️"$Options[$i] -ForegroundColor Cyan
} else {
Write-Host " "$Options[$i]
}
}
}
}
function Invoke-InteractiveEsc
{
if ($Key -eq 27 -or $Key -eq 220)
{
Main
}
}
<#
TASK FUNCTIONS
________________________________________________________________________________
#>
function New-Animation
{
param (
[object] $InputData,
[object] $OptionsData,
[object] $TimestampsData
)
begin {
Require-Command -Command "ffmpeg"
Write-Host "CREATING ANIMATION" -ForegroundColor Magenta
Start-Sleep 1
# Check input
if (-not ($InputData.InputFormat -in $SupportedAnimations) -and -not ($InputData.InputFormat -in $SupportedVideos) -and $InputData.InputType -ne "dir") {
Show-ErrorMessage -Message "Input must be an animated image or video file."
}
$InputPath = $InputData.InputPath
$InputType = $InputData.InputType
$InputFormat = $InputData.InputFormat
$Timerange = $TimestampsData.TimeRange
$OutputData = Get-OutputFile -InputData $InputData -OptionsData $OptionsData
$OutputPath = $OutputData.OutputPath
$OutputFormat = $OutputData.Format
$ConcatFlag = "-f concat -safe 0".Split(" ")
# Check output format.
if (-not ($OutputFormat -in $SupportedAnimations) -and -not ($OutputFormat -in $SupportedVideos)) {
Show-ErrorMessage -Message "'$OutputFormat' is not image/video output format."
}
Start-RemoveExistingOutput
New-TempFolder
}
process {
# If input is a folder of frames and encoder is set to ffmpeg, use ffmpeg to
# turn frames into a video first. Then use that video as source.
# Gifski and apngasm encoders already do this in their respective Invoke functions.
if ($InputType -eq "dir" -and $OptionsData.Encoder -eq "ffmpeg") {
Write-Host "Turning frames into video..." -ForegroundColor Cyan
Start-Sleep 1
New-MergeList -InputPath $InputPath -InputFromat $InputFormat -InputType $InputType -OptionsData $OptionsData
& $ffmpeg $ConcatFlag -i "$TempPath\files.txt" -c:v libx264 -c:a copy -preset slow -crf 18 -y "$TempPath\temp.mp4"
$InputData = Test-Input -InputPath "$TempPath\temp.mp4"
$InputPath = "$TempPath\temp.mp4"
}
$Filters = Set-Filter -InputData $InputData -OptionsData $OptionsData -TimestampsData $TimestampsData -OutputFormat $OutputFormat
if ($OptionsData.Encoder -eq "ffmpeg") {
$LoopOption = Set-LoopOption -OptionsData $OptionsData -OutputFormat $OutputFormat -Encoder $OutputData.Encoder
if ($OutputFormat -eq "webp") {
Write-Host "Creating animated WebP (w/ ffmpeg using libwebp)..." -ForegroundColor Cyan
Start-Sleep 1
& $ffmpeg $TimeRange -i "$InputPath" $Filters $LoopOption -c libwebp -compression_level $OptionsData.WebpCompression -qscale $OptionsData.WebpQuality -y "$TempPath\animated.webp"
} elseif ($OutputFormat -eq "png" -or $OutputFormat -eq "apng") {
Write-Host "Creating animated PNG (w/ ffmpeg)..." -ForegroundColor Cyan
Start-Sleep 1
& $ffmpeg $TimeRange -i "$InputPath" $Filters $LoopOption -y "$TempPath\animated.apng"
try {
Copy-Item -Path "$TempPath\animated.apng" -Destination "$TempPath\animated.png" -ErrorAction Stop
} catch {
Write-Host "Could not copy final PNG file. Check temp folder." -ForegroundColor DarkRed
Pause
}
} elseif ($OutputFormat -eq "avif" -and $OptionsData.LibAOM) {
Write-Host "Creating animated AVIF (w/ ffmpeg using libaom)..." -ForegroundColor Cyan
Start-Sleep 1
if (-not $AVIFQ) { $AVIFQ = 8 }
& $ffmpeg $TimeRange -i "$InputPath" $Filters $LoopOption -c libaom-av1 -cpu-used $OptionsData.AVIFQ -row-mt 1 -g 24 -pix_fmt yuv420p10le -svtav1-params tune=0:film-grain=$OptionsData.FilmGrain -crf $OptionsData.CRF -y "$TempPath\animated.avif"
} elseif ($OutputFormat -eq "avif") {
Write-Host "Creating animated AVIF (w/ ffmpeg using libsvtav1)..." -ForegroundColor Cyan
Start-Sleep 1
if (-not $AVIFQ) { $AVIFQ = 13 }
& $ffmpeg $TimeRange -i "$InputPath" $Filters $LoopOption -c libsvtav1 -preset $OptionsData.AVIFQ -g 24 -pix_fmt yuv420p -svtav1-params tune=0:film-grain=$OptionsData.FilmGrain -crf $OptionsData.CRF -y "$TempPath\animated.avif"
} elseif ($OutputFormat -eq "gif") {
Write-Host "Creating animated GIF (w/ ffmpeg)..." -ForegroundColor Cyan
Start-Sleep 1
& $ffmpeg $TimeRange -i "$InputPath" $Filters $LoopOption -y "$TempPath\animated.gif"
} elseif ($OutputFormat -eq "mp4" -or $OutputFormat -eq "mkv") {
$CodecName = $OutputFormat.ToUpper()
Write-Host "Creating $CodecName video (w/ ffmpeg)..." -ForegroundColor Cyan
Start-Sleep 1
& $ffmpeg -i "$InputPath" $Filters $LoopOption -c:v libx264 -c:a copy -preset slow -crf $OptionsData.CRF -y "$TempPath\animated.$OutputFormat"
} elseif ($OutputFormat -eq "webm") {
Write-Host "Creating WebM video (w/ ffmpeg)..." -ForegroundColor Cyan
Start-Sleep 1
& $ffmpeg -i "$InputPath" $Filters $LoopOption -c:v libvpx-vp9 -c:a libopus -crf $OptionsData.CRF -y "$TempPath\animated.webm"
}
Vifpics-CheckError -Message "There was a problem creating animation. See ffmpeg output above for details."
}
if ($OptionsData.Encoder -eq "gifski") {
Invoke-gifski -InputData $InputData -OptionsData $OptionsData -TimestampsData $TimestampsData -OutputPath "$TempPath\animated.gif"
}
if ($OptionsData.Encoder -eq "apngasm") {
Invoke-apngasm -InputPath $InputPath -OptionsData $OptionsData -TimestampsData $TimestampsData -OutputPath "$TempPath\animated.png"
try {
Copy-Item -Path "$TempPath\animated.png" -Destination "$TempPath\animated.apng" -ErrorAction Stop
} catch {
Write-Host "Could not copy final PNG file. Check temp folder." -ForegroundColor DarkRed
Pause
}
}
Vifpics-CheckError -Message "There was a problem creating animation. See output above for details."
try {
Copy-Item -Path "$TempPath\animated.$OutputFormat" -Destination "$OutputPath" -ErrorAction Stop
Start-RenameAPNG -OutputPath $OutputPath -OutputFormat $OutputFormat
} catch {
Show-ErrorMessage -Message "Could not save $OutputPath. Check Vifpics temp folder in `"$TempPath`"."
}
}
end {
Return
}
}
function New-Frames
{
param (
[object] $InputData,
[object] $OptionsData,
[object] $TimestampsData
)
begin {
Require-Command -Command "ffmpeg"
Write-Host
Write-Host "GENERATING FRAMES" -ForegroundColor Magenta
Start-Sleep 1
# Check input.
if (-not ($InputData.InputFormat -in $SupportedAnimations) -and -not ($InputData.InputFormat -in $SupportedVideos)) {
Show-ErrorMessage -Message "Input file needs to be an animated image or video file."
}
if ($InputData.InputFormat -eq "webp") {
Show-ErrorMessage "This version of Vifpics cannot convert animated WebP to other formats."
}
$OutputFormat = $OptionsData.Format