move to audiotags

This commit is contained in:
sentriz
2019-06-11 13:42:42 +01:00
parent 435b6c1f8b
commit e5998dca02
7 changed files with 64 additions and 35 deletions

View File

@@ -15,7 +15,6 @@ import (
"github.com/sentriz/gonic/mime"
"github.com/sentriz/gonic/model"
"github.com/sentriz/taggolib"
)
var (
@@ -37,19 +36,6 @@ var coverFilenames = map[string]struct{}{
"front.jpeg": {},
}
func readTags(path string) (taggolib.Parser, error) {
track, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "reading disk")
}
defer track.Close()
parser, err := taggolib.New(track)
if err != nil {
return nil, errors.Wrap(err, "parsing")
}
return parser, nil
}
type Scanner struct {
db, tx *gorm.DB
musicPath string
@@ -276,8 +262,8 @@ func (s *Scanner) handleTrack(it *item) error {
track.TagTrackArtist = tags.Artist()
track.TagTrackNumber = tags.TrackNumber()
track.TagDiscNumber = tags.DiscNumber()
track.Duration = tags.Duration() // these two should be calculated
track.Bitrate = tags.Bitrate() // from the file instead of tags
track.Length = tags.Length() // these two should be calculated
track.Bitrate = tags.Bitrate() // from the file instead of tags
//
// set album artist basics
artist := &model.Artist{}

56
scanner/tags.go Normal file
View File

@@ -0,0 +1,56 @@
package scanner
import (
"strconv"
"strings"
"github.com/nicksellen/audiotags"
"github.com/pkg/errors"
)
func intSep(in, sep string) int {
if in == "" {
return 0
}
start := strings.Split(in, sep)[0]
out, err := strconv.Atoi(start)
if err != nil {
return 0
}
return out
}
type tags struct {
map_ map[string]string
props *audiotags.AudioProperties
}
func (t *tags) firstTag(keys ...string) string {
for _, key := range keys {
if val, ok := t.map_[key]; ok {
return val
}
}
return ""
}
func (t *tags) Title() string { return t.firstTag("title") }
func (t *tags) Artist() string { return t.firstTag("artist") }
func (t *tags) Album() string { return t.firstTag("album") }
func (t *tags) AlbumArtist() string { return t.firstTag("albumartist", "album artist") }
func (t *tags) Year() int { return intSep(t.firstTag("date", "year"), "-") }
func (t *tags) TrackNumber() int { return intSep(t.firstTag("tracknumber"), "/") }
func (t *tags) DiscNumber() int { return intSep(t.firstTag("discnumber"), "/") }
func (t *tags) Length() int { return t.props.Length }
func (t *tags) Bitrate() int { return t.props.Bitrate }
func readTags(path string) (*tags, error) {
map_, props, err := audiotags.Read(path)
if err != nil {
return nil, errors.Wrap(err, "audiotags module")
}
return &tags{
map_: map_,
props: props,
}, nil
}