tag/cmd/tag/main.go

100 lines
2.0 KiB
Go
Raw Normal View History

2015-03-19 13:21:53 +01:00
// 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"
2015-03-19 13:21:53 +01:00
"flag"
"fmt"
"os"
"github.com/dhowden/tag"
"github.com/dhowden/tag/mbz"
2015-03-19 13:21:53 +01:00
)
var usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [optional flags] filename\n", os.Args[0])
flag.PrintDefaults()
}
2018-04-02 02:54:32 +02:00
var (
raw = flag.Bool("raw", false, "show raw tag data")
extractMBZ = flag.Bool("mbz", false, "extract MusicBrainz tag data (if available)")
)
2015-03-19 13:21:53 +01:00
func main() {
2018-04-02 02:54:32 +02:00
flag.Usage = usage
2015-03-19 13:21:53 +01:00
flag.Parse()
if flag.NArg() != 1 {
usage()
2015-03-19 13:21:53 +01:00
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)
2018-04-02 02:54:32 +02:00
if *raw {
2015-03-19 13:21:53 +01:00
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)
}
}
2018-04-02 02:54:32 +02:00
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))
}
2015-03-19 13:21:53 +01:00
}
func printMetadata(m tag.Metadata) {
fmt.Printf("Metadata Format: %v\n", m.Format())
fmt.Printf("File Type: %v\n", m.FileType())
2015-03-19 13:21:53 +01:00
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())
2015-05-24 14:25:04 +02:00
fmt.Printf(" Genre: %v\n", m.Genre())
2015-03-19 13:21:53 +01:00
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())
2015-05-19 14:10:04 +02:00
fmt.Printf(" Lyrics: %v\n", m.Lyrics())
2018-11-04 21:56:00 +01:00
fmt.Printf(" Comment: %v\n", m.Comment())
2015-03-19 13:21:53 +01:00
}