Browse Source

Added new helper function

main
Unbewohnte 3 years ago
parent
commit
b1ae1927f8
  1. 35
      README.md
  2. 16
      id3v10.go
  3. 16
      id3v11.go
  4. 10
      util.go

35
README.md

@ -1,5 +1,40 @@
# ID3ED (ID3 - Encoder - Decoder) # ID3ED (ID3 - Encoder - Decoder)
## Library for encoding/decoding ID3 tags ## Library for encoding/decoding ID3 tags
# Installation
`go get https://github.com/Unbewohnte/id3ed`
# Usage
```
package main
import(
"fmt"
"github.com/Unbewohnte/id3ed"
)
func main() {
mp3file, err := os.Open("/path/to/mp3/myMP3.mp3")
if err != nil {
panic(err)
}
mp3tags, err := GetID3v11Tags(mp3file)
if err != nil {
panic(err)
}
// printing all tags
fmt.Printf("%+v",mp3tags)
// getting certain tag
songname := mp3tags.GetSongName()
// etc.
}
```
# Under construction ! # Under construction !
## Bugs are a possibility rn in this state ## Bugs are a possibility rn in this state

16
id3v10.go

@ -38,17 +38,17 @@ func GetID3v1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) {
return nil, fmt.Errorf("does not use ID3v1") return nil, fmt.Errorf("does not use ID3v1")
} }
songname, err := read(rs, 30) songname, err := readToString(rs, 30)
if err != nil { if err != nil {
return nil, err return nil, err
} }
artist, err := read(rs, 30) artist, err := readToString(rs, 30)
if err != nil { if err != nil {
return nil, err return nil, err
} }
album, err := read(rs, 30) album, err := readToString(rs, 30)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -62,7 +62,7 @@ func GetID3v1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) {
return nil, fmt.Errorf("could not convert yearbytes into int: %s", err) return nil, fmt.Errorf("could not convert yearbytes into int: %s", err)
} }
comment, err := read(rs, 30) comment, err := readToString(rs, 30)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -78,11 +78,11 @@ func GetID3v1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) {
} }
return &ID3v1Tags{ return &ID3v1Tags{
SongName: string(songname), SongName: songname,
Artist: string(artist), Artist: artist,
Album: string(album), Album: album,
Year: year, Year: year,
Comment: string(comment), Comment: comment,
Genre: genre, Genre: genre,
}, nil }, nil
} }

16
id3v11.go

@ -35,17 +35,17 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
return nil, fmt.Errorf("does not use ID3v1") return nil, fmt.Errorf("does not use ID3v1")
} }
songname, err := read(rs, 30) songname, err := readToString(rs, 30)
if err != nil { if err != nil {
return nil, err return nil, err
} }
artist, err := read(rs, 30) artist, err := readToString(rs, 30)
if err != nil { if err != nil {
return nil, err return nil, err
} }
album, err := read(rs, 30) album, err := readToString(rs, 30)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -59,7 +59,7 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
return nil, fmt.Errorf("could not convert yearbytes into int: %s", err) return nil, fmt.Errorf("could not convert yearbytes into int: %s", err)
} }
comment, err := read(rs, 28) comment, err := readToString(rs, 28)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -89,11 +89,11 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
} }
return &ID3v11Tags{ return &ID3v11Tags{
SongName: string(songname), SongName: songname,
Artist: string(artist), Artist: artist,
Album: string(album), Album: album,
Year: year, Year: year,
Comment: string(comment), Comment: comment,
Track: track, Track: track,
Genre: genre, Genre: genre,
}, nil }, nil

10
util.go

@ -15,3 +15,13 @@ func read(rs io.Reader, n int) ([]byte, error) {
return read, nil return read, nil
} }
// Shortcut function to read n bytes and convert them into string
func readToString(rs io.Reader, n int) (string, error) {
read := make([]byte, n)
_, err := rs.Read(read)
if err != nil {
return "", fmt.Errorf("could not read from reader: %s", err)
}
return string(read), nil
}

Loading…
Cancel
Save