-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
1128 lines (986 loc) · 37.6 KB
/
main.go
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
/*
Quality Muncher Go (QM:GO) - a program to worsen the quality of media files
Copyright (C) 2022 Quality Muncher Organization
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"qm-go/ffprobe"
"qm-go/utils"
"github.com/flopp/go-findfont"
"github.com/spf13/pflag"
"golang.org/x/term"
)
var (
// flags
output string
inputs []string
debug bool
overwrite bool
progbarLength int
imagePasses int
loglevel string
updateSpeed float64
noVideo, noAudio bool
replaceAudio string
preset int
start, end, outDuration float64
volume int
earrape bool
outScale float64
videoBrDiv, audioBrDiv int
stretch string
outFPS int
speed float64
zoom float64
fadein, fadeout float64
stutter int
vignette float64
corrupt int
fry int
interlace bool
lagfun bool
resample bool
text, textFont, textColor string
textposx, textposy int
fontSize float64
// other variables
unspecifiedProgbarSize bool
strFmt formats
)
type formats struct {
success, successHL string
warning, warningHL string
error, errorHL string
info, infoHL string
debug, debugHL string
working, workingHL string
reset string
}
func init() {
// colors
strFmt = formats{
success: "\033[32m",
successHL: "\033[92m",
warning: "\033[38;2;250;169;30m",
warningHL: "\033[38;2;250;182;37m",
error: "\033[31m",
errorHL: "\033[91m",
info: "\033[94m",
infoHL: "\033[36m",
debug: "\033[36m",
debugHL: "\033[96m",
working: "\033[94m",
workingHL: "\033[36m",
reset: "\033[0m",
}
pflag.CommandLine.SortFlags = false
pflag.StringSliceVarP(&inputs, "input", "i", []string{""}, "Specify the input file(s)")
pflag.StringVarP(&output, "output", "o", "", "Specify the output file")
pflag.BoolVarP(&debug, "debug", "d", false, "Print out debug information")
pflag.BoolVarP(&overwrite, "overwrite", "y", false, "Overwrite the output file if it exists instead of prompting for confirmation")
pflag.IntVar(&progbarLength, "progress-bar", -1, "Length of progress bar, defaults based on terminal width")
pflag.IntVar(&imagePasses, "loop", 1, "Number of time to compress the input. ONLY USED FOR IMAGES.")
pflag.StringVar(&loglevel, "loglevel", "error", "Specify the log level for ffmpeg")
pflag.Float64Var(&updateSpeed, "update-speed", 0.0167, "Specify the speed at which stats will be updated")
pflag.BoolVar(&noVideo, "no-video", false, "Produces an output with no video")
pflag.BoolVar(&noAudio, "no-audio", false, "Produces an output with no audio")
pflag.StringVar(&replaceAudio, "replace-audio", "", "Replace the audio with the specified file")
pflag.IntVarP(&preset, "preset", "p", 4, "Specify the quality preset (1-7, higher = worse)")
pflag.Float64Var(&start, "start", 0, "Specify the start time of the output")
pflag.Float64Var(&end, "end", -1, "Specify the end time of the output, cannot be used when duration is specified")
pflag.Float64Var(&outDuration, "duration", -1, "Specify the duration of the output, cannot be used when end is specified")
pflag.IntVarP(&volume, "volume", "v", 0, "Specify the amount to increase or decrease the volume by, in dB")
pflag.BoolVar(&earrape, "earrape", false, "Heavily and extremely distort the audio (aka earrape). BE WARNED: VOLUME WILL BE SUBSTANTIALLY INCREASED.")
pflag.Float64VarP(&outScale, "scale", "s", -1, "Specify the output scale")
pflag.IntVar(&videoBrDiv, "video-bitrate", -1, "Specify the video bitrate divisor (higher = worse)")
pflag.IntVar(&videoBrDiv, "vb", videoBrDiv, "Shorthand for --video-bitrate")
pflag.IntVar(&audioBrDiv, "audio-bitrate", -1, "Specify the audio bitrate divisor (higher = worse)")
pflag.IntVar(&audioBrDiv, "ab", audioBrDiv, "Shorthand for --audio-bitrate")
pflag.StringVar(&stretch, "stretch", "1:1", "Modify the existing aspect ratio")
pflag.IntVar(&outFPS, "fps", -1, "Specify the output fps (lower = worse)")
pflag.Float64Var(&speed, "speed", 1.0, "Specify the video and audio speed")
pflag.Float64VarP(&zoom, "zoom", "z", 1, "Specify the amount to zoom in or out")
pflag.Float64Var(&fadein, "fade-in", 0, "Fade in duration")
pflag.Float64Var(&fadeout, "fade-out", 0, "Fade out duration")
pflag.IntVar(&stutter, "stutter", 0, "Randomize the order of a frames (higher = more stutter)")
pflag.Float64Var(&vignette, "vignette", 0, "Specify the amount of vignette")
pflag.IntVar(&corrupt, "corrupt", 0, "Corrupt the output (1-10, higher = worse)")
pflag.IntVar(&fry, "deep-fry", 0, "Deep-fry the output (1-10, higher = worse)")
pflag.BoolVar(&interlace, "interlace", false, "Interlace the output")
pflag.BoolVar(&lagfun, "lagfun", false, "Force darker pixels to update slower")
pflag.BoolVar(&resample, "resample", false, "Blend frames together instead of dropping them")
pflag.StringVarP(&text, "text", "t", "", "Text to add (if empty, no text)")
pflag.StringVar(&textFont, "text-font", "arial", "Text to add (if empty, no text)")
pflag.StringVar(&textColor, "text-color", "white", "Text color")
pflag.IntVar(&textposx, "text-pos-x", 50, "horizontal position of text (0 is far left, 100 is far right)")
pflag.IntVar(&textposy, "text-pos-y", 90, "vertical position of text (0 is top, 100 is bottom)")
pflag.Float64Var(&fontSize, "font-size", 12, "Font size (scales with output width)")
pflag.Parse()
// check for invalid input
if inputs[0] == "" {
log.Fatal("No input was specified")
}
// negative start time would give an output with no video so throw an error
if start < 0 {
log.Fatal("Start time cannot be negative")
}
// if start time is greater than or equal to end time, output length would be 0 so throw an error
if start >= end && end != -1 {
log.Fatal("Start time cannot be greater than or equal to end time")
}
if outDuration != -1 && end != -1 {
log.Fatal("Cannot specify both duration and end time")
}
// make sure that we know when the progress bar length is unspecified so we can set it automatically
if progbarLength == -1 {
unspecifiedProgbarSize = true
} else {
unspecifiedProgbarSize = false
}
}
func main() {
// throw out all flags if debug is enabled
if debug {
log.Println("throwing all flags out")
log.Println(
inputs,
output,
debug,
progbarLength,
imagePasses,
loglevel,
updateSpeed,
noVideo,
noAudio,
preset,
start,
end,
outDuration,
volume,
outScale,
videoBrDiv,
audioBrDiv,
stretch,
outFPS,
speed,
zoom,
fadein,
fadeout,
stutter,
vignette,
corrupt,
interlace,
lagfun,
resample,
text,
textFont,
textColor,
textposx,
textposy,
fontSize,
)
}
// loop for each provided input because we support queueing multiple inputs
for i, input := range inputs {
// check if input file exists
_, err := os.Stat(input)
// if it doesn't exist, skip this input and go to the next one
if err != nil {
if os.IsNotExist(err) {
log.Println(strFmt.error+"Error: input file", strFmt.errorHL+input+strFmt.error, "does not exist"+strFmt.reset)
continue
} else {
// the input file exists but can't be accessed for some reason
fmt.Println(strFmt.warning+"Warning: Input file", strFmt.warningHL+input+strFmt.warning, "might not be accessible."+strFmt.reset)
}
}
// set the progressbar length to 0 if it isn't explicitly set, preventing it from spilling over to the next line when called for the first time
if unspecifiedProgbarSize {
progbarLength = 0
}
if debug {
log.Println("input: " + input)
log.Println("input #: " + strconv.Itoa(i))
}
// i have no clue how this works but i remember writing it and it works so i'm not touching it
// maybe i should stop using triple negatives in my variable names/code
renderVideo := !noVideo
renderAudio := !noAudio
if !noVideo {
renderVideo = Stream(input, "v:0")
}
if len(string(replaceAudio)) == 0 {
if !noAudio {
renderAudio = Stream(input, "a:0")
}
}
// get input data: width, height, duration, and framerate
inputData, _ := ffprobe.ProbeData(input)
// assume that the input is a video unless proven otherwise
isImage := false
outExt := ".mp4"
if renderAudio && !renderVideo {
outExt = ".mp3"
}
if !renderVideo && !renderAudio {
log.Println(strFmt.error + "Error: Cannot encode video without audio or video streams" + strFmt.reset)
continue
}
// if the duration is less than 1.0 seconds, check the frame count\
// only check the frame count when needed because it's slow
if inputData.Duration < 1.0 && renderVideo {
if ffprobe.FrameCount(input) == 1 { // if there's only one frame, it's an image
log.Print("duration: ", inputData.Duration)
isImage = true
outExt = ".jpg"
}
}
// set the output file name if it isn't explicitly set or if there are multiple inputs
if len(inputs) > 1 {
output = strings.TrimSuffix(input, filepath.Ext(input)) + " (Quality Munched)" + outExt
}
if output == "" {
output = strings.TrimSuffix(input, filepath.Ext(input)) + " (Quality Munched)" + outExt
if debug {
log.Println("No output was specified, using input name plus (Quality Munched)")
log.Println("output: " + output)
}
} else {
if !strings.Contains(output, ":") {
output = filepath.Dir(input) + "/" + output
}
}
// check if output file already exists
_, outExistErr := os.Stat(output)
if outExistErr == nil {
if debug {
log.Print("output file already exists")
}
var confirm string
if !overwrite {
fmt.Println(strFmt.warning+"Warning: The output file", strFmt.warningHL+output+strFmt.warning, "already exists! Overwrite? [Y/N]"+strFmt.reset)
fmt.Scanln(&confirm) // get user input, confirming that they want to overwrite the output file
if confirm != "Y" && confirm != "y" {
log.Println("Aborted by user - output file already exists")
continue
}
}
}
startTime := time.Now()
if isImage {
if debug {
log.Println("input is an image")
}
imageMunch(input, inputData, i+1, len(inputs)) // encode the image
} else {
if start >= inputData.Duration {
log.Fatal("Start time cannot be greater than or equal to input duration")
}
videoMunch(input, inputData, i+1, len(inputs), renderVideo, renderAudio) // encode the video
}
// check if output file exists
_, outErr := os.Stat(output)
if outErr != nil {
if os.IsNotExist(outErr) {
log.Fatal(strFmt.error + "Fatal Error: something went wrong when making the output file!" + strFmt.reset)
log.Fatal(strFmt.error + "Fatal Error: something went wrong when making the output file!" + strFmt.reset)
} else {
log.Fatal(err)
}
} else {
fmt.Println(strFmt.success+"Finished encoding"+strFmt.successHL, output+strFmt.success, "in", utils.TrimTime(utils.FormatTime(time.Since(startTime).Seconds()))+strFmt.reset)
}
}
}
func videoMunch(input string, inputData ffprobe.MediaData, inNum int, totalNum int, renderVideo bool, renderAudio bool) {
if !renderVideo {
inputData.Width = 1
inputData.Height = 1
inputData.Framerate = 1.0
}
// get input resolution
if debug {
log.Print("resolution is ", inputData.Width, " by ", inputData.Height)
}
// fps and tmix (frame resampling) filters/calculations
if outFPS == -1 {
outFPS = 24 - (3 * preset)
}
if debug {
log.Print("Output FPS is ", outFPS)
}
var fpsFilter string = "fps=" + strconv.Itoa(outFPS)
var tmixFrames int = 0
if resample {
if outFPS <= int(inputData.Framerate) {
tmixFrames = int(inputData.Framerate) / outFPS
fpsFilter = "tmix=frames=" + strconv.Itoa(tmixFrames) + ":weights=1,fps=" + strconv.Itoa(outFPS)
if debug {
log.Print("resampling with tmix, tmix frames ", tmixFrames, " and output fps is "+strconv.Itoa(outFPS))
}
} else {
log.Fatal("Cannot resample from a lower framerate to a higher framerate (output fps exceeds input fps)")
}
}
// if the output scale isn't explicitly set, calculate it using the given preset
if outScale == -1 {
outScale = 1.0 / float64(preset)
}
if debug {
log.Print("Output scale is ", outScale)
}
// calculate the output resolution
outputWidth, outputHeight := newResolution(inputData.Width, inputData.Height)
var bitrate int
// calculate the video bitrate
if videoBrDiv != -1 {
bitrate = outputHeight * outputWidth * int(math.Sqrt(float64(outFPS))) / videoBrDiv
} else {
bitrate = outputHeight * outputWidth * int(math.Sqrt(float64(outFPS))) / preset
}
var audioBitrate int
// calculate the audio bitrate
if audioBrDiv != -1 {
audioBitrate = 80000 / audioBrDiv
} else {
audioBitrate = 80000 / preset
}
if debug {
log.Print("bitrate is ", bitrate, " which i got by doing ", outputHeight, "*", outputWidth, "*", int(math.Sqrt(float64(outFPS))), "/", preset)
}
// set up the ffmpeg filter for -filter_complex
var filter strings.Builder
// if NOT using --no-video, set add the specified video filters to filter
if renderVideo {
if speed != 1 {
filter.WriteString("setpts=(1/" + strconv.FormatFloat(speed, 'f', -1, 64) + ")*PTS,")
if debug {
log.Print("speed is ", speed)
}
}
filter.WriteString(fpsFilter + ",scale=" + strconv.Itoa(outputWidth) + ":" + strconv.Itoa(outputHeight) + ",setsar=1:1")
if fadein != 0 {
filter.WriteString(",fade=t=in:d=" + strconv.FormatFloat(fadein, 'f', -1, 64))
if debug {
log.Print("fade in is ", fadein)
}
}
if fadeout != 0 {
filter.WriteString(",fade=t=out:d=" + strconv.FormatFloat(fadeout, 'f', -1, 64) + ":st=" + strconv.FormatFloat((inputData.Duration-fadeout), 'f', -1, 64))
if debug {
log.Print("fade out duration is ", fadeout, " start time is ", (inputData.Duration - fadeout))
}
}
if zoom != 1 {
filter.WriteString(",zoompan=d=1:zoom=" + strconv.FormatFloat(zoom, 'f', -1, 64) + ":fps=" + strconv.Itoa(outFPS) + ":x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'")
if debug {
log.Print("zoom amount is ", zoom)
}
}
if vignette != 0 {
filter.WriteString(",vignette=PI/(5/(" + strconv.FormatFloat(vignette, 'f', -1, 64) + "/2))")
if debug {
log.Print("vignette amount is ", vignette, " or PI/(5/("+strconv.FormatFloat(vignette, 'f', -1, 64)+"/2))")
}
}
if text != "" {
filter.WriteString(makeTextFilter(outputWidth, text, textFont, fontSize, textColor, textposx, textposy))
}
if interlace {
filter.WriteString(",interlace")
}
if lagfun {
filter.WriteString(",lagfun")
}
if stutter != 0 {
filter.WriteString(",random=frames=" + strconv.Itoa(stutter))
if debug {
log.Print("stutter is ", stutter)
}
}
if fry != 0 {
filter.WriteString("," + "eq=saturation=" + strconv.FormatFloat(float64(fry)*0.15+0.85, 'f', -1, 64) + ":contrast=" + strconv.Itoa(fry) + ",unsharp=5:5:1.25:5:5:" + strconv.FormatFloat(float64(fry)/6.66, 'f', -1, 64) + ",noise=alls=" + strconv.Itoa(fry*5) + ":allf=t")
if debug {
log.Print("fry is ", ","+"eq=saturation="+strconv.FormatFloat(float64(fry)*0.15+0.85, 'f', -1, 64)+":contrast="+strconv.Itoa(fry)+",unsharp=5:5:1.25:5:5:"+strconv.FormatFloat(float64(fry)/6.66, 'f', -1, 64)+",noise=alls="+strconv.Itoa(fry*5)+":allf=t")
}
}
} else {
log.Print("no video, ignoring all video filters")
}
// find what the duration of the output should be for the progress bar, % completion, and ETA in stats
var realOutputDuration float64
if outDuration >= inputData.Duration || outDuration == -1 {
realOutputDuration = (inputData.Duration - start) / speed // if the output duration is longer than the input duration, set the output duration to the input duration times speed
} else {
realOutputDuration = outDuration / speed // if the output duration is shorter than the input duration, set the output duration to the output duration times speed
}
// if not using --no-audio, set add the specified audio filters to filter
if renderAudio {
if earrape {
filter.WriteString(";aeval=sgn(val(5)):c=same")
if debug {
log.Print("earrape is true")
}
}
if volume != 0 {
if earrape {
filter.WriteString(",volume=" + strconv.Itoa(volume) + "dB")
} else {
filter.WriteString(";volume=" + strconv.Itoa(volume) + "dB")
}
if debug {
log.Print("volume is ", volume)
}
}
// is speed is not 1, set the audio speed to the specified speed
if speed != 1 {
if replaceAudio != "" {
filter.WriteString(";[1]atempo=" + strconv.FormatFloat(speed, 'f', -1, 64)) // if the audio is being replaced, use audio from second input
} else {
filter.WriteString(";[0]atempo=" + strconv.FormatFloat(speed, 'f', -1, 64)) // first input (video)
}
if debug {
log.Print("audio speed is ", speed)
}
}
} else {
log.Print("no audio, ignoring all audio filters")
}
var corruptAmount int
var corruptFilter string
// corruption calculations based on width and height
if corrupt != 0 {
// amount of corruption is based on the bitrate of the video, the amount of corruption, and the size of the video
corruptAmount = int(float64(outputHeight*outputWidth) / float64(bitrate) * 100000.0 / float64(corrupt*3))
corruptFilter = "noise=" + strconv.Itoa(corruptAmount)
if debug {
log.Print("corrupt amount is", corruptAmount)
log.Print("(", outputHeight, " * ", outputWidth, ")", " / 2073600 * 1000000", " / ", "(", corrupt, "* 10)")
log.Print("corrupt filter is -bsf ", corruptFilter)
}
}
// staring ffmpeg args
args := []string{
"-y", // forces overwrite of existing file, if one does exist
"-loglevel", loglevel,
"-hide_banner",
"-progress", "-",
"-stats_period", strconv.FormatFloat(updateSpeed, 'f', -1, 64),
}
if start != 0 { // if start is specified
args = append(args, "-ss", strconv.FormatFloat(start, 'f', -1, 64)) // -ss is the start time
}
if end != -1 { // if end is specified
outDuration = end - start
}
if outDuration != -1 { // if the duration is specified
args = append(args, "-t", strconv.FormatFloat(outDuration, 'f', -1, 64)) // -t sets the duration
}
// remove video if the user wants no video
if !renderVideo {
args = append(args, "-vn")
if debug {
log.Print("no video")
}
}
// remove audio if noAudio is true
if !renderAudio {
args = append(args, "-an") // removes audio
if debug {
log.Print("no audio")
}
}
// add the input to the ffmpeg args
args = append(args,
"-i", input,
)
// if replaceAudio is specified, add the second input to the ffmpeg args to replace the audio of the output
if replaceAudio != "" {
args = append(args, "-i", replaceAudio)
args = append(args, "-map", "0:v:0")
args = append(args, "-map", "1:a:0")
if debug {
log.Print("replacing audio")
}
}
// more always-used args
if renderVideo {
args = append(args,
"-preset", "ultrafast",
"-shortest",
"-c:v", "libx264",
"-b:v", strconv.Itoa(int(bitrate)),
"-c:a", "aac",
"-b:a", strconv.Itoa(int(audioBitrate)),
)
} else {
args = append(args,
"-shortest",
"-b:v", strconv.Itoa(int(bitrate)),
"-c:a", "libmp3lame",
"-b:a", strconv.Itoa(int(audioBitrate)),
)
}
// if any filters are being used, add them
if len(filter.String()) != 0 {
args = append(args, "-filter_complex", filter.String())
}
// if corruption is specified, add the corrupt filter to the ffmpeg args
if corrupt != 0 {
args = append(args, "-bsf", corruptFilter)
}
args = append(args, output) // add the output file to the ffmpeg args
if debug {
log.Print(args)
}
// the [x/y] thing when encoding multiple files
var encodingFileOutOf string
if totalNum != 1 {
encodingFileOutOf = "[" + strconv.Itoa(inNum) + "/" + strconv.Itoa(totalNum) + "] "
}
fmt.Println(strFmt.working+encodingFileOutOf+"Encoding "+strFmt.workingHL+filepath.Base(input), strFmt.working+"to", strFmt.workingHL+filepath.Base(output)+strFmt.reset) // print the input and output file
// start ffmpeg for encoding
cmd := exec.Command("ffmpeg", args...)
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
cmd.Start()
// variables for progress bar and stats
scannerTextAccum := " "
scannerrorTextAccum := " "
eta := 0.0
currentFrame := 0
fullTime := "" // time as a string
oldFrame := 0 // the frame number from the last time that the average fps over the last second was calculated
avgFramerate := " " // the average framerate over the entire video
lastSecAvgFramerate := " " // the average framerate over the last second
startTime := time.Now() // the time that the video started encoding
changeStartTime := time.Now() // the time that the last change in the one second framerate was made
var currentTotalTime float64
// if the progress bar length is not set, set it to the length of the longest possible progress bar
if unspecifiedProgbarSize {
progbarLength = utils.ProgbarSize(len(" " + strconv.FormatFloat((currentTotalTime*100/realOutputDuration), 'f', 1, 64) + "%" + " time: " + utils.TrimTime(fullTime) + " ETA: " + utils.TrimTime(utils.FormatTime(eta)) + " fps: " + avgFramerate + " fp1s: " + lastSecAvgFramerate))
}
if debug {
log.Print("progbarLength is", progbarLength)
}
// print the progress bar, completely unfilled
// fmt.Println(utils.ProgressBar(0.0, realOutputDuration, progbarLength))
// removed above due to it not working now
// start the progress bar updater until the video is done encoding
scanner := bufio.NewScanner(stdout)
scannerror := bufio.NewScanner(stderr)
scanner.Split(bufio.ScanRunes)
scannerror.Split(bufio.ScanRunes)
for scanner.Scan() {
// accumulate the text from the scanner
scannerTextAccum += scanner.Text()
// if the scanner text is a newline or carriage return, process the accumulated text
if scanner.Text() == "\r" || scanner.Text() == "\n" {
// if the accumulated text contains the time, process it and print the progress bar and % completion
if strings.Contains(scannerTextAccum, "time=") {
if !strings.Contains(scannerTextAccum, "time=N/A") { // needed due to ffmpeg 6.1 adding a thing where time is N/A at the start of the encoding process
// time variables
fullTime = strings.Split(strings.Split(scannerTextAccum, "\n")[0], "=")[1]
hour, _ := strconv.Atoi(strings.Split(fullTime, ":")[0])
min, _ := strconv.Atoi(strings.Split(fullTime, ":")[1])
sec, _ := strconv.Atoi(strings.Split(strings.Split(fullTime, ":")[2], ".")[0])
milisec, _ := strconv.ParseFloat("."+strings.Split(fullTime, ".")[1], 64)
fullTime = strings.Split(fullTime, ".")[0] + strconv.FormatFloat(milisec, 'f', 1, 64)[1:] + "s"
// calculate estimated time remaining
eta = getETA(startTime, currentTotalTime, realOutputDuration)
// if the progress bar length is not set, set it to the length of the longest possible progress bar
if unspecifiedProgbarSize {
var terminalWidth int
lastTerminalWidth := terminalWidth
terminalWidth, _, _ = term.GetSize(int(os.Stdout.Fd()))
lastProgBar := progbarLength
progbarLength = utils.ProgbarSize(len(" " + strconv.FormatFloat((currentTotalTime*100/realOutputDuration), 'f', 1, 64) + "%" + " time: " + utils.TrimTime(fullTime) + " ETA: " + utils.TrimTime(utils.FormatTime(eta)) + " fps: " + avgFramerate + " fp1s: " + lastSecAvgFramerate))
if (lastProgBar+1 == progbarLength || lastProgBar-1 == progbarLength) && terminalWidth == lastTerminalWidth {
progbarLength = lastProgBar
}
}
currentTotalTime = float64(hour*3600+min*60+sec) + milisec
// if the progress bar length is greater than 0, print the progress bar
if progbarLength > 0 {
fmt.Print("\033[1A", utils.ProgressBar(currentTotalTime, realOutputDuration, progbarLength))
} else {
fmt.Print("\033[1A\033[0J")
}
// print the percentage complete
fmt.Print(" ", strconv.FormatFloat((currentTotalTime*100/realOutputDuration), 'f', 1, 64), "%")
}
}
// if the accumulated text contains the frame, process it
if strings.Contains(scannerTextAccum, "frame=") {
currentFrame, _ = strconv.Atoi(strings.Split(strings.Split(scannerTextAccum, "\n")[0], "=")[1])
avgFramerate = strconv.FormatFloat(float64(currentFrame)/time.Since(startTime).Seconds(), 'f', 1, 64)
// if it's been one second since the last fps over one second update, update it again
if time.Since(changeStartTime).Seconds() >= 1 {
lastSecAvgFramerate = strconv.FormatFloat(float64(currentFrame-oldFrame)/time.Since(changeStartTime).Seconds(), 'f', 1, 64)
oldFrame = currentFrame
changeStartTime = time.Now()
}
}
// if the accumulated text contains the speed, print the time, eta, fps, and fps over the last second
if strings.Contains(scannerTextAccum, "speed=") {
fmt.Print(" time: ", utils.TrimTime(fullTime))
fmt.Print(" ETA: ", utils.TrimTime(utils.FormatTime(eta)))
fmt.Print(" fps: ", avgFramerate)
fmt.Print("\033[0J") // this overwrites any previous text that was printed, even if unlikely
fmt.Print(" fp1s: ", lastSecAvgFramerate)
fmt.Print("\n")
}
// reset the accumulated text
scannerTextAccum = ""
}
}
for scannerror.Scan() {
scannerrorTextAccum += scannerror.Text()
}
cmd.Wait()
// print the percentage complete (100% by now), time, ETA (hopfully 0s), fps, and fps over the last second
if len(scannerrorTextAccum) > 1 {
fmt.Println("\n\n"+strFmt.error+"Possible FFmpeg Error:", scannerrorTextAccum+strFmt.reset)
} else {
if progbarLength > 0 {
fmt.Print("\033[1A\033[0J", utils.ProgressBar(realOutputDuration, realOutputDuration, progbarLength))
} else {
fmt.Print("\033[1A\033[0J")
}
fmt.Print(
" 100.0%",
" time: ", utils.TrimTime(fullTime),
" ETA: ", utils.TrimTime(utils.FormatTime(eta)),
" fps: ", avgFramerate,
" fp1s: ", lastSecAvgFramerate,
"\n",
)
}
}
func imageMunch(input string, inputData ffprobe.MediaData, inNum int, totalNum int) {
if debug {
log.Print("resolution is ", inputData.Width, " by ", inputData.Height)
}
if outScale == -1 {
outScale = 1.0 / float64(preset)
}
if debug {
log.Print("Output scale is ", outScale)
}
outputWidth, outputHeight := newResolution(inputData.Width, inputData.Height)
// set up the ffmpeg filter for -filter_complex
var filter strings.Builder
filter.WriteString("scale=" + strconv.Itoa(outputWidth) + ":" + strconv.Itoa(outputHeight) + ",setsar=1:1")
if zoom != 1 {
filter.WriteString(",zoompan=d=1:zoom=" + strconv.FormatFloat(zoom, 'f', -1, 64) + ":fps=" + strconv.Itoa(outFPS) + ":x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'")
if debug {
log.Print("zoom amount is ", zoom)
}
}
if vignette != 0 {
filter.WriteString(",vignette=PI/(5/(" + strconv.FormatFloat(vignette, 'f', -1, 64) + "/2))")
if debug {
log.Print("vignette amount is ", vignette, " or PI/(5/("+strconv.FormatFloat(vignette, 'f', -1, 64)+"/2))")
}
}
if text != "" {
filter.WriteString(makeTextFilter(outputWidth, text, textFont, fontSize, textColor, textposx, textposy))
}
if fry != 0 {
filter.WriteString("," + "eq=saturation=" + strconv.FormatFloat(float64(fry)*0.15+0.85, 'f', -1, 64) + ":contrast=" + strconv.Itoa(fry) + ",unsharp=5:5:1.25:5:5:" + strconv.FormatFloat(float64(fry)/6.66, 'f', -1, 64) + ",noise=alls=" + strconv.Itoa(fry*5) + ":allf=t")
if debug {
log.Print("fry is ", ","+"eq=saturation="+strconv.FormatFloat(float64(fry)*0.15+0.85, 'f', -1, 64)+":contrast="+strconv.Itoa(fry)+",unsharp=5:5:1.25:5:5:"+strconv.FormatFloat(float64(fry)/6.66, 'f', -1, 64)+",noise=alls="+strconv.Itoa(fry*5)+":allf=t")
}
}
// staring ffmpeg args
args := []string{
"-y", // forces overwrite of existing file, if one does exist
"-loglevel", loglevel,
"-hide_banner",
"-progress", "-",
"-stats_period", strconv.FormatFloat(updateSpeed, 'f', -1, 64),
"-i", input,
"-c:v", "mjpeg",
"-q:v", "31",
"-frames:v", "1",
}
// if any filters are being used, add them
if len(filter.String()) != 0 {
args = append(args, "-filter_complex", filter.String())
}
args = append(args, output) // add the output file to the ffmpeg args
if debug {
log.Print(args)
}
// the [x/y] thing when encoding multiple files
var encodingFileOutOf string
if totalNum != 1 {
encodingFileOutOf = "[" + strconv.Itoa(inNum) + "/" + strconv.Itoa(totalNum) + "] "
}
fmt.Println(strFmt.working+encodingFileOutOf+"Encoding "+strFmt.workingHL+filepath.Base(input), strFmt.working+"to", strFmt.workingHL+filepath.Base(output)+strFmt.reset) // print the input and output file
// start ffmpeg for encoding
cmd := exec.Command("ffmpeg", args...)
cmd.Start()
cmd.Wait()
var newOutput string
startTime := time.Now()
eta := 0.0
if imagePasses > 1 {
if err := os.MkdirAll("temp", os.ModePerm); err != nil {
log.Fatal(err)
}
newOutput = "temp/loop1.jpg"
cmd = exec.Command(
"ffmpeg",
"-y", // forces overwrite of existing file, if one does exist
"-loglevel", loglevel,
"-hide_banner",
"-progress", "-",
"-stats_period", strconv.FormatFloat(updateSpeed, 'f', -1, 64),
"-i", output,
"-c:v", "mjpeg",
"-q:v", "31",
"-frames:v", "1",
newOutput,
)
cmd.Start()
cmd.Wait()
eta = getETA(startTime, 1, 1)
// if the progress bar length is not set, set it to the length of the longest possible progress bar
if unspecifiedProgbarSize {
progbarLength = utils.ProgbarSize(len(" " + (strconv.FormatFloat((1*100/float64(imagePasses)), 'f', 1, 64) + "%" + " ETA: " + utils.TrimTime(utils.FormatTime(eta)))))
}
// if the progress bar length is greater than 0, print the progress bar
if progbarLength > 0 {
fmt.Print(utils.ProgressBar(1, float64(imagePasses), progbarLength))
} else {
fmt.Print("\033[0J")
}
// print the percentage complete
fmt.Println(" ", strconv.FormatFloat((1*100/float64(imagePasses)), 'f', 1, 64)+"%"+" ETA: "+utils.TrimTime(utils.FormatTime(eta))+"\033[0J")
if debug {
log.Print("libwebp:")
log.Print("compression level: " + strconv.Itoa(int(float64(1/float64(preset))*7.0)-1))
log.Print("quality: " + strconv.Itoa(((preset)*12)+16))
log.Print("libx264:")
log.Print("crf: " + strconv.Itoa(int(float64(preset)*(51.0/7.0))))
log.Print("mjpeg:")
log.Print("q:v: " + strconv.Itoa(int(float64(preset)*3.0)+10))
}
var oldOutput string
for i := 2; i < imagePasses-1; i++ {
oldOutput = newOutput
newOutput = "temp/loop" + strconv.Itoa(i) + ".png"
cmd = exec.Command(
"ffmpeg",
"-y", // forces overwrite of existing file, if one does exist
"-loglevel", loglevel,
"-hide_banner",
"-progress", "-",
"-stats_period", strconv.FormatFloat(updateSpeed, 'f', -1, 64),
"-i", oldOutput,
"-c:v", "libwebp",
"-compression_level", strconv.Itoa(int(float64(1/float64(preset))*7.0)-1),
"-quality", strconv.Itoa(((preset)*12)+16),
"-frames:v", "1",
newOutput,
)
cmd.Start()
cmd.Wait()
os.Remove(oldOutput)
eta = getETA(startTime, float64(i), float64(imagePasses))
if unspecifiedProgbarSize {
progbarLength = utils.ProgbarSize(len(" " + (strconv.FormatFloat((float64(i)*100/float64(imagePasses)), 'f', 1, 64) + "%" + " ETA: " + utils.TrimTime(utils.FormatTime(eta)))))
}
// if the progress bar length is greater than 0, print the progress bar
if progbarLength > 0 {
fmt.Print("\033[1A", utils.ProgressBar(float64(i), float64(imagePasses), progbarLength))
} else {
fmt.Print("\033[1A\033[0J")
}
// print the percentage complete
fmt.Println(" ", strconv.FormatFloat((float64(i)*100/float64(imagePasses)), 'f', 1, 64)+"%"+" ETA: "+utils.TrimTime(utils.FormatTime(eta))+"\033[0J")
i++
oldOutput = newOutput
newOutput = "temp/loop" + strconv.Itoa(i) + ".png"
cmd = exec.Command(
"ffmpeg",
"-y", // forces overwrite of existing file, if one does exist
"-loglevel", loglevel,
"-hide_banner",
"-progress", "-",
"-stats_period", strconv.FormatFloat(updateSpeed, 'f', -1, 64),
"-i", oldOutput,
"-c:v", "libx264",
"-crf", strconv.Itoa(int(float64(preset)*(51.0/7.0))),
"-frames:v", "1",
newOutput,
)
cmd.Start()
cmd.Wait()
os.Remove(oldOutput)
eta = getETA(startTime, float64(i), float64(imagePasses))
if unspecifiedProgbarSize {
progbarLength = utils.ProgbarSize(len(" " + (strconv.FormatFloat((float64(i)*100/float64(imagePasses)), 'f', 1, 64) + "%" + " ETA: " + utils.TrimTime(utils.FormatTime(eta)))))
}
// if the progress bar length is greater than 0, print the progress bar
if progbarLength > 0 {
fmt.Print("\033[1A", utils.ProgressBar(float64(i), float64(imagePasses), progbarLength))
} else {
fmt.Print("\033[1A\033[0J")
}
// print the percentage complete
fmt.Println(" ", strconv.FormatFloat((float64(i)*100/float64(imagePasses)), 'f', 1, 64)+"%"+" ETA: "+utils.TrimTime(utils.FormatTime(eta))+"\033[0J")
i++
oldOutput = newOutput
newOutput = "temp/loop" + strconv.Itoa(i) + ".jpg"
cmd = exec.Command(
"ffmpeg",
"-y", // forces overwrite of existing file, if one does exist
"-loglevel", loglevel,
"-hide_banner",
"-progress", "-",
"-stats_period", strconv.FormatFloat(updateSpeed, 'f', -1, 64),
"-i", oldOutput,
"-c:v", "mjpeg",
"-q:v", strconv.Itoa(int(float64(preset)*3.0)+10),
"-frames:v", "1",
newOutput,
)
cmd.Start()
cmd.Wait()
os.Remove(oldOutput)
eta = getETA(startTime, float64(i), float64(imagePasses))
if unspecifiedProgbarSize {
progbarLength = utils.ProgbarSize(len(" " + (strconv.FormatFloat((float64(i)*100/float64(imagePasses)), 'f', 1, 64) + "%" + " ETA: " + utils.TrimTime(utils.FormatTime(eta)))))
}