audio.go 781 B

1234567891011121314151617181920212223242526272829303132333435363738
  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, fileName string) (song *Song, err error) {
  9. fullPath := filepath.Join(basePath, fileName)
  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. result := Song{
  21. Title: tags.Title(),
  22. Artist: tags.Artist(),
  23. Album: tags.Album(),
  24. Duration: durationTime,
  25. DurationOk: durationOk,
  26. BasePath: basePath,
  27. RelativePath: fileName,
  28. }
  29. return &result, nil
  30. }