audio.go 919 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package read
  2. import (
  3. "os"
  4. "path/filepath"
  5. tag "github.com/dhowden/tag"
  6. duration "github.com/felamaslen/go-music-player/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. durationTime, durationOk := duration.GetSongDuration(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: durationTime,
  27. DurationOk: durationOk,
  28. BasePath: basePath,
  29. RelativePath: scannedFile.RelativePath,
  30. ModifiedDate: scannedFile.ModifiedDate,
  31. }
  32. return &result, nil
  33. }