Skip to content

Commit e3395f7

Browse files
committed
Refactoring and cleanup
1 parent 78904ca commit e3395f7

File tree

6 files changed

+158
-134
lines changed

6 files changed

+158
-134
lines changed

data.go

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ type Anime struct {
1919
Status AnimeStatus
2020
}
2121

22+
type Gui struct {
23+
CurrentSpeed string
24+
Eta string
25+
Progress float32
26+
ProgressLabel string
27+
TotalProgress string
28+
ButtonLabel string
29+
Logs string
30+
}
31+
2232
type Shader struct {
2333
Name string
2434
Path string

ffmpeg.go

+33-97
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
g "github.com/AllenDang/giu"
12-
"github.com/jaypipes/ghw"
1312
)
1413

1514
func handleUpscalingLogs(stderr io.ReadCloser, anime Anime) string {
@@ -28,58 +27,56 @@ func handleUpscalingLogs(stderr io.ReadCloser, anime Anime) string {
2827
}
2928

3029
if !strings.HasPrefix(line, "frame=") {
31-
trim := strings.Replace(line, "\r", "", -1)
32-
trim = strings.Replace(trim, "\n", "", -1)
30+
trim := strings.ReplaceAll(line, "\r", "")
31+
trim = strings.ReplaceAll(trim, "\n", "")
3332
logDebug(trim, false)
3433
ffmpegLogs += line
3534
line = ""
3635
continue
3736
}
3837

3938
// It's line with speed and time
40-
speedRaw := strings.Split(strings.Split(line, "speed=")[1], " ")[0]
41-
time := strings.Split(strings.Split(strings.Split(line, "time=")[1], " ")[0], ".")[0]
39+
time := strings.Split(readOutputParameter(line, "time", "bitrate"), ".")[0]
4240
millis := durationToMillis(time)
43-
progress = float32(millis) / float32(anime.Length)
41+
gui.Progress = float32(millis) / float32(anime.Length)
4442

45-
if time == "N/A" { // FFMPEG does not report time, we must estimate it manually
46-
split := strings.Split(line, "frame=")
47-
if len(split) > 1 { // Just for safety
48-
frame, _ := strconv.ParseFloat(strings.ReplaceAll(strings.Split(split[1], "f")[0], " ", ""), 32)
49-
progress = float32(frame) / float32(anime.TotalFrames)
43+
// FFMPEG may not report time, we must calculate it manually
44+
if time == "N/A" && len(strings.Split(line, "frame=")) > 1 {
45+
frame, _ := strconv.ParseFloat(readOutputParameter(line, "frame", "fps"), 32)
46+
gui.Progress = float32(frame) / float32(anime.TotalFrames)
5047

51-
fps, _ := strconv.ParseFloat(strings.ReplaceAll(strings.Split(strings.Split(line, "fps=")[1], "q")[0], " ", ""), 32)
52-
currentSpeed = fmt.Sprintf("Speed: %.2fx", fps/anime.FrameRate)
48+
fps, _ := strconv.ParseFloat(readOutputParameter(line, "fps", "q"), 32)
49+
gui.CurrentSpeed = fmt.Sprintf("Speed: %.2fx", fps/anime.FrameRate)
5350

54-
leftFrames := anime.TotalFrames - int(frame)
55-
leftMillis := int64((float32(leftFrames) / float32(fps)) * 1000)
56-
eta = fmt.Sprintf("ETA: %s", formatMillis(leftMillis))
57-
}
58-
}
59-
60-
rounded := int(progress * 100)
61-
if rounded == 99 {
62-
progress = 1
63-
progressLabel = "100%"
64-
} else {
65-
progressLabel = fmt.Sprintf("%d%%", rounded)
51+
leftFrames := anime.TotalFrames - int(frame)
52+
leftMillis := int64((float32(leftFrames) / float32(fps)) * 1000)
53+
gui.Eta = fmt.Sprintf("ETA: %s", formatMillis(leftMillis))
6654
}
6755

68-
// Workaround for disappearing speed
56+
// Speed
57+
speedRaw := strings.ReplaceAll(readOutputParameter(line, "speed", ""), "x", "")
6958
if strings.Contains(speedRaw, ".") {
70-
speedValue, _ := strconv.ParseFloat(strings.Replace(speedRaw, "x", "", -1), 32)
71-
72-
currentSpeed = fmt.Sprintf("Speed: %s", speedRaw)
59+
gui.CurrentSpeed = fmt.Sprintf("Speed: %s", speedRaw)
60+
speedValue, _ := strconv.ParseFloat(speedRaw, 32)
7361

7462
// Just for safety
7563
if speedValue != 0 {
7664
etaMillis := float64(anime.Length-millis) / speedValue
77-
eta = fmt.Sprintf("ETA: %s", formatMillis(int64(etaMillis)))
65+
gui.Eta = fmt.Sprintf("ETA: %s", formatMillis(int64(etaMillis)))
7866
}
7967
}
8068

81-
ffmpegLogs = strings.Replace(ffmpegLogs, progressLine, line, -1)
82-
logs = strings.Replace(logs, progressLine, line, -1)
69+
// Progress bar
70+
rounded := int(gui.Progress * 100)
71+
if rounded == 99 {
72+
gui.Progress = 1
73+
gui.ProgressLabel = "100%"
74+
} else {
75+
gui.ProgressLabel = fmt.Sprintf("%d%%", rounded)
76+
}
77+
78+
ffmpegLogs = strings.ReplaceAll(ffmpegLogs, progressLine, line)
79+
gui.Logs = strings.ReplaceAll(gui.Logs, progressLine, line)
8380
progressLine = line
8481

8582
line = ""
@@ -136,71 +133,10 @@ func buildUpscalingParams(anime Anime, resolution Resolution, shader Shader, out
136133
return params
137134
}
138135

139-
func searchHardwareAcceleration() {
140-
nvidia := false
141-
amd := false
142-
intel := false
143-
144-
gpus, err := ghw.GPU()
145-
if err != nil {
146-
handleSoftError("Getting GPU info error", err.Error())
147-
return
148-
}
149-
150-
logMessage(fmt.Sprintf("Detected GPUs (%d): ", len(gpus.GraphicsCards)), false)
151-
152-
for index, gpu := range gpus.GraphicsCards {
153-
vendor := strings.ToLower(gpu.DeviceInfo.Vendor.Name)
154-
155-
logDebug(fmt.Sprintf("GPU ID: %d, Vendor: %s", index, vendor), false)
156-
157-
if strings.Contains(vendor, "nvidia") {
158-
nvidia = true
159-
} else if strings.Contains(vendor, "amd") || strings.Contains(vendor, "advanced micro devices") {
160-
amd = true
161-
} else if strings.Contains(vendor, "intel") {
162-
intel = true
163-
}
164-
165-
logMessage(fmt.Sprintf(" %d. %s", index+1, gpu.DeviceInfo.Product.Name), false)
166-
}
167-
168-
if (nvidia && intel) || (amd && intel) {
169-
intel = false
170-
logDebug("Ignoring Intel iGPU, detected NVIDIA/AMD dGPU)", false)
171-
}
172-
173-
if nvidia && amd { // AMD is iGPU
174-
amd = false
175-
logDebug("Ignoring AMD iGPU, detected NVIDIA dGPU", false)
136+
func readOutputParameter(line string, parameter string, nextParameter string) string {
137+
if nextParameter == "" {
138+
return strings.ReplaceAll(strings.Split(line, parameter+"=")[1], " ", "")
176139
}
177140

178-
if nvidia {
179-
hwaccelParams = append(hwaccelParams, "-hwaccel_device", "cuda", "-hwaccel_output_format", "cuda")
180-
addEncoders("nvidia")
181-
182-
logMessage("Available GPU acceleration: CUDA + NVENC", false)
183-
} else if amd {
184-
hwaccelParams = append(hwaccelParams, "-hwaccel_device", "opencl")
185-
addEncoders("advanced micro devices")
186-
187-
logMessage("Available GPU acceleration: AMF", false)
188-
} else if intel {
189-
settings.CompatibilityMode = true
190-
addEncoders("cpu")
191-
192-
logMessage("Intel GPUs are not supported - application may not work correctly", false)
193-
} else {
194-
settings.CompatibilityMode = true
195-
addEncoders("cpu")
196-
197-
logMessage("There's no available GPU acceleration, application may not work correctly! Please verify your GPU drivers or report bug on GitHub", false)
198-
}
199-
200-
for index, encoder := range availableEncoders {
201-
if encoder.Vendor != "cpu" {
202-
settings.Encoder = int32(index)
203-
break
204-
}
205-
}
141+
return strings.ReplaceAll(strings.Split(strings.Split(line, parameter+"=")[1], nextParameter)[0], " ", "")
206142
}

gui.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,20 @@ func loop(window *g.MasterWindow) {
9393

9494
g.Label(""),
9595

96-
g.Button(buttonLabel).OnClick(handleButton).Size(360, 30),
96+
g.Button(gui.ButtonLabel).OnClick(handleButton).Size(360, 30),
9797
},
9898
),
9999
g.Layout{
100100
g.Label("Logs"),
101-
g.InputTextMultiline(&logs).Flags(g.InputTextFlagsReadOnly).Size(1600, 270),
101+
g.InputTextMultiline(&gui.Logs).Flags(g.InputTextFlagsReadOnly).Size(1600, 270),
102102
g.SplitLayout(g.DirectionVertical, &bottomBarPos,
103103
g.SplitLayout(g.DirectionVertical, &bottomProgressPos,
104-
g.Label("Progress: "+totalProgress),
105-
g.ProgressBar(progress).Overlay(progressLabel).Size(1170, 20),
104+
g.Label("Progress: "+gui.TotalProgress),
105+
g.ProgressBar(gui.Progress).Overlay(gui.ProgressLabel).Size(1170, 20),
106106
),
107107
g.SplitLayout(g.DirectionVertical, &bottomSpeedPos,
108-
g.Label(currentSpeed),
109-
g.Label(eta),
108+
g.Label(gui.CurrentSpeed),
109+
g.Label(gui.Eta),
110110
),
111111
),
112112
},
@@ -173,7 +173,7 @@ LOOP:
173173
Status: NotStarted,
174174
}
175175
animeList = append(animeList, anime)
176-
totalProgress = fmt.Sprintf("%d / %d", calcFinished(), len(animeList))
176+
gui.TotalProgress = fmt.Sprintf("%d / %d", calcFinished(), len(animeList))
177177
logMessage("Added file "+path, false)
178178
}
179179
}
@@ -187,9 +187,9 @@ func handleButton() {
187187
}
188188

189189
func resetUI() {
190-
currentSpeed = "Speed:"
191-
eta = "ETA:"
192-
totalProgress = fmt.Sprintf("%d / %d", calcFinished(), len(animeList))
190+
gui.CurrentSpeed = "Speed:"
191+
gui.Eta = "ETA:"
192+
gui.TotalProgress = fmt.Sprintf("%d / %d", calcFinished(), len(animeList))
193193
g.Update()
194194
}
195195

hardware.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/jaypipes/ghw"
6+
"strings"
7+
)
8+
9+
func searchHardwareAcceleration() {
10+
nvidia := false
11+
amd := false
12+
intel := false
13+
14+
gpus, err := ghw.GPU()
15+
if err != nil {
16+
handleSoftError("Getting GPU info error", err.Error())
17+
return
18+
}
19+
20+
logMessage(fmt.Sprintf("Detected GPUs (%d): ", len(gpus.GraphicsCards)), false)
21+
22+
for index, gpu := range gpus.GraphicsCards {
23+
vendor := strings.ToLower(gpu.DeviceInfo.Vendor.Name)
24+
25+
logDebug(fmt.Sprintf("GPU ID: %d, Vendor: %s", index, vendor), false)
26+
27+
if strings.Contains(vendor, "nvidia") {
28+
nvidia = true
29+
} else if strings.Contains(vendor, "amd") || strings.Contains(vendor, "advanced micro devices") {
30+
amd = true
31+
} else if strings.Contains(vendor, "intel") {
32+
intel = true
33+
}
34+
35+
logMessage(fmt.Sprintf(" %d. %s", index+1, gpu.DeviceInfo.Product.Name), false)
36+
}
37+
38+
if (nvidia && intel) || (amd && intel) {
39+
intel = false
40+
logDebug("Ignoring Intel iGPU, detected NVIDIA/AMD dGPU)", false)
41+
}
42+
43+
if nvidia && amd { // AMD is iGPU
44+
amd = false
45+
logDebug("Ignoring AMD iGPU, detected NVIDIA dGPU", false)
46+
}
47+
48+
if nvidia {
49+
hwaccelParams = append(hwaccelParams, "-hwaccel_device", "cuda", "-hwaccel_output_format", "cuda")
50+
addEncoders("nvidia")
51+
52+
logMessage("Available GPU acceleration: CUDA + NVENC", false)
53+
} else if amd {
54+
hwaccelParams = append(hwaccelParams, "-hwaccel_device", "opencl")
55+
addEncoders("advanced micro devices")
56+
57+
logMessage("Available GPU acceleration: AMF", false)
58+
} else if intel {
59+
settings.CompatibilityMode = true
60+
addEncoders("cpu")
61+
62+
logMessage("Intel GPUs are not supported - application may not work correctly", false)
63+
} else {
64+
settings.CompatibilityMode = true
65+
addEncoders("cpu")
66+
67+
logMessage("There's no available GPU acceleration, application may not work correctly! Please verify your GPU drivers or report bug on GitHub", false)
68+
}
69+
70+
for index, encoder := range availableEncoders {
71+
if encoder.Vendor != "cpu" {
72+
settings.Encoder = int32(index)
73+
break
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)