-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
144 lines (122 loc) · 3.2 KB
/
main.go
File metadata and controls
144 lines (122 loc) · 3.2 KB
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
package main
import (
"fmt"
"normalize_video/config"
"normalize_video/files"
"normalize_video/service"
"normalize_video/service/mkvmetadata"
"normalize_video/types"
"path/filepath"
"strings"
"sync"
"github.com/k0kubun/pp"
)
func main() {
stats := processWithStreaming()
printStats(stats)
}
func processWithStreaming() ProcessStats {
videoChan := make(chan string, 100)
var scanErr error
var scanWg sync.WaitGroup
scanWg.Add(1)
go func() {
defer scanWg.Done()
scanErr = service.ScanVideoFilesStream(
config.ORIGIN_PATH,
config.RECURSIVE_SCAN,
config.Extensions,
videoChan,
)
}()
stats := ProcessStats{}
var processWg sync.WaitGroup
var mu sync.Mutex
workerJobs := make(chan *types.Video, config.MAX_WORKERS)
for i := 0; i < config.MAX_WORKERS; i++ {
processWg.Add(1)
go func() {
defer processWg.Done()
for video := range workerJobs {
err := processVideo(video)
mu.Lock()
if err != nil {
stats.Errors = append(stats.Errors, err)
} else {
if video.Type == "Serie" {
stats.SeriesCount++
} else {
stats.MoviesCount++
}
}
mu.Unlock()
}
}()
}
go func() {
for path := range videoChan {
filename := strings.ToLower(filepath.Base(path))
filenameParts := service.SplitFilename(filename)
extension := filenameParts[len(filenameParts)-1]
video := files.NewVideo(filename, filenameParts, path, extension)
workerJobs <- video
}
close(workerJobs)
}()
scanWg.Wait()
if scanErr != nil {
pp.Printf("Scan error: %v\n", scanErr)
}
processWg.Wait()
return stats
}
type ProcessStats struct {
MoviesCount int
SeriesCount int
Errors []error
}
func processVideo(video *types.Video) error {
if video.Type == "Serie" {
serie := files.NewSerie(video)
return processMedia(serie, serie.OriginPath, serie.Normalizer.NewPath, serie.Video.Extension)
}
movie := files.NewMovie(video)
return processMedia(movie, movie.OriginPath, movie.Normalizer.NewPath, movie.Video.Extension)
}
func processMedia(media any, originPath, newPath, extension string) error {
if err := service.MoveFile(originPath, newPath); err != nil {
return fmt.Errorf("move file error for %s: %w", filepath.Base(originPath), err)
}
if strings.ToLower(extension) == "mkv" {
result, err := mkvmetadata.UpdateMkvMetadata(media)
if err != nil {
pp.Printf("Warning: MKV metadata update failed for %s: %v\n", filepath.Base(newPath), err)
} else {
switch v := media.(type) {
case *types.Serie:
v.MkvMetadata = result
case *types.Movie:
v.MkvMetadata = result
}
}
}
service.PrintStructTable(media)
pp.Println("________________________________________________________________________________")
return nil
}
func printStats(stats ProcessStats) {
if len(stats.Errors) > 0 {
pp.Println("\nErrors encountered:")
for _, err := range stats.Errors {
pp.Printf(" - %v\n", err)
}
}
totalCount := stats.MoviesCount + stats.SeriesCount
if totalCount > 0 {
pp.Println("")
pp.Println("Movies processed:", stats.MoviesCount)
pp.Println("Series processed:", stats.SeriesCount)
pp.Println("Total videos processed:", totalCount)
pp.Println("________________________________________________________________________________")
}
}