blog/go/mp3/main.go

90 lines
2.1 KiB
Go

package main
import (
"fmt"
"github.com/bogem/id3v2/v2"
"github.com/spf13/cobra"
)
func show(file string)error{
mp3File, err := id3v2.Open(file, id3v2.Options{Parse: true})
if err != nil {
return err
}
fmt.Println("File: ", file)
fmt.Println("Title: ", mp3File.Title())
fmt.Println("Artist: ", mp3File.Artist())
fmt.Println("Genre: ", mp3File.Genre())
fmt.Println("Album: ", mp3File.Album())
fmt.Println("Version:", mp3File.Version())
fmt.Println("Year: ", mp3File.Year())
return nil
}
func set()*cobra.Command{
var artist, title, genre, year, version, album, file string
cmd := &cobra.Command{
Use: "set",
Short: "set [options] set mp3 file info",
Run: func(cmd *cobra.Command, args []string) {
mp3File, err := id3v2.Open(file, id3v2.Options{Parse: true})
if err != nil {
panic(err)
}
mp3File.SetDefaultEncoding(id3v2.EncodingUTF16)
if artist != "" {
mp3File.SetArtist(artist)
}
if title != "" {
mp3File.SetTitle(title)
}
if album != ""{
mp3File.SetAlbum(album)
}
if genre != "" {
mp3File.SetGenre(genre)
}
if year != "" {
mp3File.SetYear(year)
}
if version != "" {
mp3File.SetVersion(byte(version[0]))
}
err = mp3File.Save()
fmt.Println("save err ", err, artist, title)
if err != nil {
panic(err)
}
},
}
cmd.Flags().StringVarP(&file, "file", "f", "", "mp3 file to set")
cmd.Flags().StringVarP(&artist, "artist", "a", "", "mp3 file artist")
cmd.Flags().StringVarP(&title, "title", "t", "", "mp3 file title")
cmd.Flags().StringVarP(&genre, "genre", "g", "", "mp3 file genre")
cmd.Flags().StringVarP(&year, "year", "y", "", "mp3 file year")
cmd.Flags().StringVarP(&version, "version", "v", "", "mp3 file version")
cmd.Flags().StringVarP(&album, "album", "b", "", "mp3 file album")
return cmd
}
func main(){
cmd := &cobra.Command{
Short: "",
}
showCmd := &cobra.Command{
Use: "show",
Short: "show [file]",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := show(args[0]); err != nil {
panic(err)
}
},
}
cmd.AddCommand(showCmd, set())
cmd.Execute()
}