read_files.go 841 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. type Song struct {
  8. title, artist, album string
  9. length int
  10. }
  11. func getSongDuration(file *os.File, tags tag.Metadata) int {
  12. switch tags.Format() {
  13. case "VORBIS":
  14. result, _ := duration.GetSongDurationVorbis(file.Name())
  15. return result
  16. default:
  17. return 0
  18. }
  19. }
  20. func ReadFile(fileName string) (song *Song, err error) {
  21. file, errFile := os.Open(fileName)
  22. if errFile != nil {
  23. return &Song{}, errFile
  24. }
  25. defer file.Close()
  26. tags, errTags := tag.ReadFrom(file)
  27. if errTags != nil {
  28. return &Song{}, errTags
  29. }
  30. result := Song{
  31. title: tags.Title(),
  32. artist: tags.Artist(),
  33. album: tags.Album(),
  34. length: getSongDuration(file, tags),
  35. }
  36. return &result, nil
  37. }