-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
232 lines (205 loc) · 4.41 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
package main
import (
"io/ioutil"
"log"
"os"
"strings"
"github.com/closetool/NCMconverter/converter"
"github.com/closetool/NCMconverter/ncm"
"github.com/closetool/NCMconverter/path"
"github.com/closetool/NCMconverter/tag"
"github.com/urfave/cli/v2"
"github.com/xxjwxc/gowp/workpool"
)
var version = "0.1.0"
var cmd = struct {
output string
tag bool
deepth int
thread int
}{}
var pool *workpool.WorkPool
func main() {
app := &cli.App{
Name: "NCM Parser",
Usage: "convert ncm file to mp3 or flac format",
Authors: []*cli.Author{
{Name: "closetool", Email: "[email protected]"},
},
Version: version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Value: ``,
Usage: "output dir",
Destination: &cmd.output,
},
&cli.BoolFlag{
Name: "tag",
Aliases: []string{"t"},
Value: true,
Usage: "if adding tag to music file",
Destination: &cmd.tag,
},
&cli.IntFlag{
Name: "deepth",
Aliases: []string{"d"},
Value: 0,
Usage: "max deepth the program will find",
Destination: &cmd.deepth,
},
&cli.IntFlag{
Name: "thread",
Aliases: []string{"n"},
Value: 10,
Usage: "max thread count",
Destination: &cmd.thread,
},
},
Commands: []*cli.Command{
{
Name: "version",
Action: func(c *cli.Context) error {
cli.ShowVersion(c)
return nil
},
},
},
Action: action,
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func action(c *cli.Context) error {
if c.NArg() < 1 {
cli.ShowAppHelpAndExit(c, 1)
}
args := c.Args().Slice()
//if runtime.GOOS == "windows" {
// outputDir = filepath.Clean(outputDir)
//} else {
// outputDir = path.Clean(outputDir)
//}
cmd.output = path.Clean(cmd.output)
pool = workpool.New(cmd.thread)
//files := make([]strings, 0)
res, err := resolvePath(args)
if err != nil {
log.Printf("resolving file path failed: %v", err)
}
for _, pt := range res {
p := pt
pool.Do(func() error {
err := convert(p, cmd.output)
if err != nil {
log.Printf("Convert %v failed: %v", p, err)
}
return nil
})
}
pool.Wait()
return nil
}
func resolvePath(args []string) ([]string, error) {
var res []string
for _, arg := range args {
info, err := os.Stat(arg)
if err != nil {
log.Printf("Can not find file: %s", arg)
continue
}
if info.IsDir() {
res, err = findNCMInDir(arg, cmd.deepth)
if err != nil {
log.Printf("find ncm file in %s failed: %v", arg, err)
}
} else {
res = append(res, arg)
}
}
return res, nil
}
func convert(filePath, dir string) error {
nf, err := ncm.NewNcmFile(filePath)
if err != nil {
return err
}
defer nf.Close()
err = nf.Parse()
if err != nil {
return err
}
cv := converter.NewConverter(nf)
err = cv.HandleAll()
if err != nil {
return err
}
if dir == "" {
dir = nf.FileDir
}
fileName := strings.Split(cv.FileName, cv.Ext)
//if runtime.GOOS == "windows" {
// dir = filepath.Join(dir, fileName[0]+"."+cv.MetaData.Format)
//} else {
// dir = path.Join(dir, fileName[0]+"."+cv.MetaData.Format)
//}
dir = path.Join(dir, fileName[0]+"."+cv.MetaData.Format)
err = writeToFile(dir, cv.MusicData)
if err != nil {
return err
}
if cmd.tag {
Tag(dir, cv.Cover.Detail, cv.MetaData)
}
return nil
}
func writeToFile(dst string, data []byte) error {
info, err := os.Stat(path.Dir(dst))
if err != nil && info == nil {
os.MkdirAll(path.Dir(dst), 0644)
}
fd, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer fd.Close()
_, err = fd.Write(data)
if err != nil {
return err
}
return nil
}
func Tag(path string, imageData []byte, meta *converter.Meta) error {
tg, err := tag.NewTagger(path, meta.Format)
if err != nil {
return err
}
err = tag.TagAudioFileFromMeta(tg, imageData, meta)
return err
}
func findNCMInDir(dir string, deepth int) ([]string, error) {
if deepth <= 0 {
return nil, nil
}
infos, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
res := make([]string, 0)
for _, info := range infos {
if info.IsDir() {
tmp, _ := findNCMInDir(path.Join(dir, info.Name()), deepth-1)
if tmp != nil {
res = append(res, tmp...)
}
} else {
if strings.HasSuffix(info.Name(), ".ncm") {
res = append(res, path.Join(dir, info.Name()))
}
}
}
return res, nil
}