-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
54 lines (42 loc) · 1.25 KB
/
Copy pathmain.go
File metadata and controls
54 lines (42 loc) · 1.25 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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"path"
"slices"
"go.senan.xyz/taglib"
)
func main() {
files, err := os.ReadDir(".")
if err != nil {
log.Fatalf("can't read dir: %s", err.Error())
}
fmt.Printf("Found %d total files in the folder\n", len(files))
// Remove non-mp3 files from folder
files = slices.DeleteFunc(files, func(s os.DirEntry) bool {
return path.Ext(s.Name()) != ".mp3"
})
fmt.Printf("Found %d .mp3 files in the folder\n", len(files))
// Shuffle "files" slice
rand.Shuffle(len(files), func(i, j int) {
files[i], files[j] = files[j], files[i]
})
for index, file := range files {
// Retrieve current track number
tags, err := taglib.ReadTags(file.Name())
if err != nil {
log.Fatalf("failed while reading tags of file '%s' with error: '%s'", file.Name(), err.Error())
}
trackNumberBefore := tags[taglib.TrackNumber]
// Set track number to current index
err = taglib.WriteTags(file.Name(), map[string][]string{
taglib.TrackNumber: {fmt.Sprintf("%d", index+1)},
}, 0)
if err != nil {
log.Fatalf("failed while setting tags of file '%s' with error: '%s'", file.Name(), err.Error())
}
fmt.Printf("Updated trackNumber of file '%s' from '%s' to '%d'\n", file.Name(), trackNumberBefore, index+1)
}
}