-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch.go
More file actions
108 lines (88 loc) · 2.13 KB
/
Copy pathbatch.go
File metadata and controls
108 lines (88 loc) · 2.13 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
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
type Pair struct {
Video string
Subtitle string
}
func FindPairs(dir string) ([]Pair, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
// maps base name → filename
subs := map[string]string{}
videos := map[string]string{}
for _, e := range entries {
if e.IsDir() {
continue
}
name := e.Name()
ext := strings.ToLower(filepath.Ext(name))
base := strings.TrimSuffix(name, ext)
switch ext {
case ".srt":
subs[base] = filepath.Join(dir, name)
case ".mkv":
videos[base] = filepath.Join(dir, name)
}
}
var pairs []Pair
for base, video := range videos {
if sub, ok := subs[base]; ok {
pairs = append(pairs, Pair{
Video: video,
Subtitle: sub,
})
}
}
// sorting is probably not necessary, but eh whatever
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].Video < pairs[j].Video
})
return pairs, nil
}
func DeriveBatchDeckName(videoPath string) string {
// base name of the video file without extension
base := strings.TrimSuffix(filepath.Base(videoPath), filepath.Ext(videoPath))
// name of the current working directory
dir, err := os.Getwd()
if err != nil {
// fallback if something goes wrong
dir = "Show"
}
dirName := filepath.Base(dir)
// combine: <directory-name><file-base>
return dirName + base
}
func DoBatch(cli Cli) error {
pairs, err := FindPairs(".")
if err != nil {
return err
}
// derive base output dir, e.g., ~/Desktop/<current folder>
home, _ := os.UserHomeDir()
batchOutputDir := filepath.Join(home, "Desktop", filepath.Base("."))
for i, pair := range pairs {
// copy cli so original struct isn't modified
job := cli
job.videoFile = pair.Video
job.subtitleFile = pair.Subtitle
job.deckName = DeriveBatchDeckName(pair.Video)
job.outputDirectory = batchOutputDir
fmt.Printf("[%d/%d] Processing %s → deck: %s\n", i+1, len(pairs), pair.Video, job.deckName)
subtitles, err := ParseSRTFile(job.subtitleFile)
if err != nil {
return err
}
if err := CreateAnkiDeck(job, subtitles); err != nil {
return err
}
}
return nil
}