audio.go 572 B

12345678910111213141516171819202122232425262728293031
  1. package read
  2. import (
  3. "os"
  4. tag "github.com/dhowden/tag"
  5. duration "github.com/felamaslen/go-music-player/pkg/read/duration"
  6. )
  7. func ReadFile(fileName string) (song *Song, err error) {
  8. file, errFile := os.Open(fileName)
  9. if errFile != nil {
  10. return &Song{}, errFile
  11. }
  12. defer file.Close()
  13. tags, errTags := tag.ReadFrom(file)
  14. if errTags != nil {
  15. return &Song{}, errTags
  16. }
  17. result := Song{
  18. title: tags.Title(),
  19. artist: tags.Artist(),
  20. album: tags.Album(),
  21. length: duration.GetSongDuration(file, tags),
  22. }
  23. return &result, nil
  24. }