forked from dhowden/tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (80 loc) · 2.05 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
// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
The tag tool reads metadata from media files (as supported by the tag library).
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"github.com/icanhazbroccoli/tag"
"github.com/icanhazbroccoli/tag/mbz"
)
var usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [optional flags] filename\n", os.Args[0])
flag.PrintDefaults()
}
var (
raw = flag.Bool("raw", false, "show raw tag data")
extractMBZ = flag.Bool("mbz", false, "extract MusicBrainz tag data (if available)")
)
func main() {
flag.Usage = usage
flag.Parse()
if flag.NArg() != 1 {
usage()
return
}
f, err := os.Open(flag.Arg(0))
if err != nil {
fmt.Printf("error loading file: %v", err)
return
}
defer f.Close()
m, err := tag.ReadFrom(f)
if err != nil {
fmt.Printf("error reading file: %v\n", err)
return
}
printMetadata(m)
if *raw {
fmt.Println()
fmt.Println()
tags := m.Raw()
for k, v := range tags {
if _, ok := v.(*tag.Picture); ok {
fmt.Printf("%#v: %v\n", k, v)
continue
}
fmt.Printf("%#v: %#v\n", k, v)
}
}
if *extractMBZ {
b, err := json.MarshalIndent(mbz.Extract(m), "", " ")
if err != nil {
fmt.Printf("error marshalling MusicBrainz info: %v\n", err)
return
}
fmt.Printf("\nMusicBrainz Info:\n%v\n", string(b))
}
}
func printMetadata(m tag.Metadata) {
fmt.Printf("Metadata Format: %v\n", m.Format())
fmt.Printf("File Type: %v\n", m.FileType())
fmt.Printf(" Title: %v\n", m.Title())
fmt.Printf(" Album: %v\n", m.Album())
fmt.Printf(" Artist: %v\n", m.Artist())
fmt.Printf(" Composer: %v\n", m.Composer())
fmt.Printf(" Genre: %v\n", m.Genre())
fmt.Printf(" Year: %v\n", m.Year())
track, trackCount := m.Track()
fmt.Printf(" Track: %v of %v\n", track, trackCount)
disc, discCount := m.Disc()
fmt.Printf(" Disc: %v of %v\n", disc, discCount)
fmt.Printf(" Picture: %v\n", m.Picture())
fmt.Printf(" Lyrics: %v\n", m.Lyrics())
fmt.Printf(" Comment: %v\n", m.Comment())
}