From 3fb875f3a394c64644546bc033911938acb82ea0 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Tue, 22 Jun 2021 11:57:09 +0300 Subject: [PATCH] Dirty and minimal ID3v1 reading support --- LICENSE | 9 +++++ README.md | 4 ++ go.mod | 3 ++ id3v1.go | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ util.go | 17 +++++++++ 5 files changed, 142 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 id3v1.go create mode 100644 util.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a08fc5c --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright © 2021 Unbewohne | Nikolay Kasyanov + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..810ab49 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# ID3ED (ID3- Encoder - Decoder) +## Library for encoding/decoding ID3 tags + +# Under construction ! \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e689e10 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/Unbewohnte/id3ed + +go 1.16 diff --git a/id3v1.go b/id3v1.go new file mode 100644 index 0000000..d95b9f1 --- /dev/null +++ b/id3v1.go @@ -0,0 +1,109 @@ +package id3ed + +import ( + "bytes" + "fmt" + "io" + "strconv" +) + +// https://id3.org/ID3v1 - documentation + +const ID3V1SIZE int = 128 + +type ID3v1Tags struct { + SongName string + Artist string + Album string + Year int + Comment string + Genre int +} + +// Retrieves ID3v1 field values of provided io.ReadSeeker (usually a file) +func GetID3v1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) { + // set reader to the last 128 bytes + _, err := rs.Seek(-int64(ID3V1SIZE), io.SeekEnd) + if err != nil { + return nil, fmt.Errorf("could not seek: %s", err) + } + + tag, err := read(rs, 3) + if err != nil { + return nil, err + } + + if !bytes.Equal(tag, []byte("TAG")) { + // no TAG, given file does not use ID3v1 + return nil, fmt.Errorf("does not use ID3v1") + } + + songname, err := read(rs, 30) + if err != nil { + return nil, err + } + + artist, err := read(rs, 30) + if err != nil { + return nil, err + } + + album, err := read(rs, 30) + if err != nil { + return nil, err + } + + yearBytes, err := read(rs, 4) + if err != nil { + return nil, err + } + year, err := strconv.Atoi(string(yearBytes)) + if err != nil { + return nil, fmt.Errorf("could not convert yearbytes into int: %s", err) + } + + comment, err := read(rs, 30) + if err != nil { + return nil, err + } + + genreByte, err := read(rs, 1) + if err != nil { + return nil, err + } + // genre is one byte by specification + genre := int(genreByte[0]) + + return &ID3v1Tags{ + SongName: string(songname), + Artist: string(artist), + Album: string(album), + Year: year, + Comment: string(comment), + Genre: genre, + }, nil +} + +func (t *ID3v1Tags) GetSongName() string { + return t.SongName +} + +func (t *ID3v1Tags) GetArtist() string { + return t.Artist +} + +func (t *ID3v1Tags) GetAlbum() string { + return t.Album +} + +func (t *ID3v1Tags) GetYear() int { + return t.Year +} + +func (t *ID3v1Tags) GetComment() string { + return t.Comment +} + +func (t *ID3v1Tags) GetGenre() int { + return t.Genre +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..691838c --- /dev/null +++ b/util.go @@ -0,0 +1,17 @@ +package id3ed + +import ( + "fmt" + "io" +) + +// Shortcut function to read n bytes from reader. Peeked from here: https://github.com/dhowden/tag/blob/master/util.go +func read(rs io.Reader, n int) ([]byte, error) { + read := make([]byte, n) + _, err := rs.Read(read) + if err != nil { + return nil, fmt.Errorf("could not read from reader: %s", err) + } + + return read, nil +}