From ae11ed555cb350ea4c11fc1625f4a56229969ddd Mon Sep 17 00:00:00 2001 From: Physick <96335032+DegustatorPonos@users.noreply.github.com> Date: Sat, 13 Dec 2025 12:32:15 +0500 Subject: Tracks meta --- Audio/Meta.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'Audio/Meta.go') 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 +} -- cgit v1.3