audio.go 885 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package read
  2. import (
  3. "os"
  4. "path/filepath"
  5. tag "github.com/dhowden/tag"
  6. duration "github.com/felamaslen/gmus-backend/pkg/read/duration"
  7. )
  8. func ReadFile(basePath string, scannedFile *File) (song *Song, err error) {
  9. fullPath := filepath.Join(basePath, scannedFile.RelativePath)
  10. file, errFile := os.Open(fullPath)
  11. if errFile != nil {
  12. return &Song{}, errFile
  13. }
  14. defer file.Close()
  15. tags, errTags := tag.ReadFrom(file)
  16. if errTags != nil {
  17. return &Song{}, errTags
  18. }
  19. durationSeconds := duration.GetSongDurationSeconds(file, tags)
  20. trackNumber, _ := tags.Track()
  21. result := Song{
  22. TrackNumber: trackNumber,
  23. Title: tags.Title(),
  24. Artist: tags.Artist(),
  25. Album: tags.Album(),
  26. Duration: durationSeconds,
  27. BasePath: basePath,
  28. RelativePath: scannedFile.RelativePath,
  29. ModifiedDate: scannedFile.ModifiedDate,
  30. }
  31. return &result, nil
  32. }