diff options
| author | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2025-12-13 12:32:15 +0500 |
|---|---|---|
| committer | Physick <96335032+DegustatorPonos@users.noreply.github.com> | 2025-12-13 12:32:15 +0500 |
| commit | ae11ed555cb350ea4c11fc1625f4a56229969ddd (patch) | |
| tree | 1d02b84131b713a2eed9a332ffac51997b0ec285 /Audio | |
| parent | 4b8ef84059e772ef7636c451356680ec5b0983c4 (diff) | |
Tracks meta
Diffstat (limited to 'Audio')
| -rw-r--r-- | Audio/List.go | 31 | ||||
| -rw-r--r-- | Audio/Meta.go | 45 |
2 files changed, 76 insertions, 0 deletions
diff --git a/Audio/List.go b/Audio/List.go new file mode 100644 index 0000000..46bdfff --- /dev/null +++ b/Audio/List.go @@ -0,0 +1,31 @@ +package audio + +import ( + "log" + "os" + + settings "physick.ru/culture_exam/Settings" +) + +type TracksList struct { + Tracks []TrackMeta +} + +func GenerateTrackList(verbouse bool) (TracksList, error) { + var files, dirErr = os.ReadDir(settings.Current.SongsLocation) + if dirErr != nil { + return TracksList{}, dirErr + } + var outp = TracksList { + Tracks: make([]TrackMeta, 0, len(files)), + } + for _, v := range files { + var meta, err = GenerateMetadata(v.Name()) + if err != nil { + log.Printf("Failed to parse the file info: %s", err.Error()) + continue + } + outp.Tracks = append(outp.Tracks, meta) + } + return outp, nil +} diff --git a/Audio/Meta.go b/Audio/Meta.go index 9e288f3..6b8343c 100644 --- a/Audio/Meta.go +++ b/Audio/Meta.go @@ -1,13 +1,44 @@ package audio import ( + "fmt" "io" "os" "time" + "github.com/dhowden/tag" "github.com/tcolgate/mp3" + settings "physick.ru/culture_exam/Settings" ) +type TrackMeta struct { + Name string + Author string + Album string + Location string + Duration time.Duration +} + +func GenerateMetadata(fileName string) (TrackMeta, error) { + var fullPath = fmt.Sprintf("%s/%s", settings.Current.SongsLocation, fileName) + var duration, error = GetSongDuration(fullPath) + // If file doesn't exist that will be caught there + if error != nil { + return TrackMeta{}, error + } + var Artist, Title, Album, metaErr = GetInfo(fullPath) + if metaErr != nil { + return TrackMeta{}, metaErr + } + return TrackMeta { + Name: Title, + Author: Artist, + Album: Album, + Location: fullPath, + Duration: duration, + }, nil +} + func GetSongDuration(path string) (time.Duration, error) { var file, fopenErr = os.Open(path) if fopenErr != nil { @@ -33,3 +64,17 @@ func GetSongDuration(path string) (time.Duration, error) { } return duration, nil } + +// Author - name - album +func GetInfo (filePath string) (string, string, string, error) { + f, err := os.Open(filePath) + if err != nil { + return "", "", "", err + } + defer f.Close() + m, err := tag.ReadFrom(f) + if err != nil { + return "", "", "", err + } + return m.Artist(), m.Title(), m.Album(), nil +} |
