-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (174 loc) · 5.11 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
package main
import (
"fmt"
"os"
"syscall"
"os/signal"
"flag"
"path/filepath"
"github.com/schollz/progressbar/v3"
)
var (
// input parameters
squareSize int
inputVideo string
outputDir string
inputVideos []string
realTime bool
drawUI bool
playOnlyMode bool
bar *progressbar.ProgressBar
)
func parseFlags() {
// Define flags
playOnlyModeF := flag.Bool("play", false, "Player only mode, do not write output. Just displays a preview of the result. (default false)")
drawUIF := flag.Bool("ui", false, "Whether to draw UI (default false)")
realTimeF := flag.Bool("rt", false, "Whether to run in real-time (default false)")
squareSizeF := flag.Int("s", 256, "Size of square output video")
var outputDirF string
flag.StringVar(&outputDirF, "o", "", "Output directory (default current directory)")
var inputVideosF string
flag.StringVar(&inputVideosF, "i", "", "Input video or directory. Required.")
// Parse flags()
flag.Parse()
// Set default output directory to current working directory if not provided
if outputDirF == "" {
var err error
outputDirF, err = os.Getwd()
if err != nil {
fmt.Println("Error getting current working directory: "+err.Error())
selfExit()
return
}
} else {
// make output folder if it doesn't exist
if !*playOnlyModeF {
_, err := os.Stat(outputDirF)
if os.IsNotExist(err) {
// Create the directory and its parents if they don't exist
err := os.MkdirAll(outputDirF, os.ModePerm)
if err != nil {
fmt.Println("Error creating output directory: "+err.Error())
selfExit()
return
}
}
}
}
if !*playOnlyModeF {
var err error
outputDirF, err = convertToAbsolutePath(outputDirF)
if err != nil {
fmt.Println("Error getting absolute path of output folder: "+err.Error())
selfExit()
return
}
}
// Process folder input instead of single video
if inputVideosF == "" {
fmt.Println("Error: no input video/directory specified with -i.")
selfExit()
return
}
files, isFolder, err := getFilesInPath(inputVideosF)
if err != nil {
fmt.Println("Error:", err)
selfExit()
return
}
if !isFolder {
files = []string{}
files = append(files, inputVideosF)
}
for i, file := range files {
if isFolder {
files[i] = inputVideosF+string(filepath.Separator)+file
}
}
// Override Logic
if !*drawUIF {
// if not drawing UI, cannot be real-time proccesed
*realTimeF = false
}
if *playOnlyModeF {
// if player only mode, must draw ui and real-time
*realTimeF = true
*drawUIF = true
}
// Print parsed flags
fmt.Println("1:1 Content-Aware Video Cropper V1.0")
fmt.Println("Parameters:")
if *playOnlyModeF {
fmt.Println("\tMode: Player-Only")
fmt.Printf("\tInput Video(s): %v\n", files)
fmt.Printf("\tSquare Size: %d\n", *squareSizeF)
} else {
if *drawUIF {
fmt.Println("\tMode: Show Algorithm")
fmt.Printf("\tReal-time: %v\n", *realTimeF)
} else {
fmt.Println("\tMode: Proccess")
}
// fmt.Printf("\tDraw UI: %v\n", *drawUIF)
fmt.Printf("\tInput Video(s): %v\n", files)
fmt.Printf("\tOutput Directory: %s\n", outputDirF)
fmt.Printf("\tSquare Size: %d\n", *squareSizeF)
}
playOnlyMode = *playOnlyModeF
drawUI = *drawUIF
realTime = *realTimeF
squareSize = *squareSizeF
outputDir = outputDirF
for _, video := range files {
video, err := convertToAbsolutePath(video)
if err != nil {
fmt.Println("Error getting absolute path of input video: "+err.Error())
selfExit()
return
}
inputVideos = append(inputVideos, video)
}
}
func main() {
catchExit()
parseFlags()
screenWidth, screenHeight = CalculateWidthHeight(squareSize)
if drawUI {
setupUI()
}
cleanPathsAndTempDir()
defer rmTempDirs()
if drawUI {
go runUI()
}
var i int
for i, inputVideo = range inputVideos {
establishPipesAndImgs()
fmt.Printf("\nProcessing %d/%d: %s\n", i+1, len(inputVideos), getBasenameWithoutExt(inputVideo))
initalResize()
startPipeline()
if drawUI {
tickPipeline()
if i == len(inputVideos)-1 {
//last one
termUI = true
}
} else {
tickPipeline()
}
}
}
func catchExit() {
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
go func() {
<- gracefulShutdown
fmt.Println("\n\nCaught EXIT")
termUI = true
rmTempDirs()
os.Exit(1)
}()
}
func selfExit() {
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}