initial commit
This commit is contained in:
65
internal/metadata.go
Normal file
65
internal/metadata.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package ripsort
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/dhowden/tag"
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
Format tag.Format
|
||||
FileType tag.FileType
|
||||
Title string
|
||||
Artist []string
|
||||
Album string
|
||||
AlbumArtist []string
|
||||
Composer []string
|
||||
Genre string
|
||||
Year int
|
||||
Track int
|
||||
TotalTracks int
|
||||
Disc int
|
||||
TotalDiscs int
|
||||
Lyrics string
|
||||
Comment string
|
||||
HasPicture bool
|
||||
}
|
||||
|
||||
func ReadAudioTags(filePath string) (*metadata, error) {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
m, err := tag.ReadFrom(f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read tags: %w", err)
|
||||
}
|
||||
|
||||
track, totalTracks := m.Track()
|
||||
disc, totalDiscs := m.Disc()
|
||||
|
||||
info := &metadata{
|
||||
Format: m.Format(),
|
||||
FileType: m.FileType(),
|
||||
Title: m.Title(),
|
||||
Artist: strings.Split(m.Artist(), ";"),
|
||||
Album: m.Album(),
|
||||
AlbumArtist: strings.Split(m.AlbumArtist(), ";"),
|
||||
Composer: strings.Split(m.Composer(), ";"),
|
||||
Genre: m.Genre(),
|
||||
Year: m.Year(),
|
||||
Track: track,
|
||||
TotalTracks: totalTracks,
|
||||
Disc: disc,
|
||||
TotalDiscs: totalDiscs,
|
||||
Lyrics: m.Lyrics(),
|
||||
Comment: m.Comment(),
|
||||
HasPicture: m.Picture() != nil,
|
||||
}
|
||||
|
||||
return info, nil
|
||||
}
|
||||
Reference in New Issue
Block a user