Skip to content

Commit

Permalink
v3 mp3 & v3 mp4
Browse files Browse the repository at this point in the history
  • Loading branch information
gcottom committed Jun 7, 2024
1 parent 2224597 commit a05f9ed
Show file tree
Hide file tree
Showing 17 changed files with 1,164 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (

func main() {
args := os.Args[1:]
fmt.Println(args)
if len(args) > 1 && args[0] == "." {
fmt.Println(args)
args = args[1:]
}
if len(args) >= 1 {
Expand All @@ -19,6 +21,8 @@ func main() {
file = args[1]
}
mode := strings.ToLower(args[0])
fmt.Println(mode)
fmt.Println(len(args) % 2)
if len(args)%2 == 0 && len(args) != 1 {
if mode == "p" || mode == "parse" || mode == "r" || mode == "read" || mode == "-p" || mode == "-parse" || mode == "-r" || mode == "-read" {
tag, err := audiometa.OpenTagFromPath(file)
Expand Down Expand Up @@ -185,14 +189,14 @@ func main() {
} else if args[1] == "c" || args[1] == "e" || args[1] == "clear" || args[1] == "empty" || args[1] == "-c" || args[1] == "-e" || args[1] == "-clear" || args[1] == "-empty" {
fmt.Println("mp3-mp4-tag-cmd-help: Clear(Empty) mode\nThe clear mode clears all of the tags in the file and saves the file with empty tags. You can use this mode to clear all tags of a file and then use the write mode to write all new tags to the file.\nex usage: mp3-mp4-tag clear filepath.mp3")
}
}
} else {
if len(args) == 1 && args[0] == "h" || args[0] == "-h" || args[0] == "-help" || args[0] == "help" {
fmt.Println("mp3-mp4-tag-cmd-help: The application offers 3 modes: read, write, and clear. Use the command \"help\" followed by a mode to learn more about its usage.")
} else {
if len(args) == 1 && args[0] == "h" || args[0] == "-h" || args[0] == "-help" || args[0] == "help" {
fmt.Println("mp3-mp4-tag-cmd-help: The application offers 3 modes: read, write, and clear. Use the command \"help\" followed by a mode to learn more about its usage.")
} else {
fmt.Println("Invalid number of arguments!\nmp3-mp4-tag-cmd-help: The application offers 3 modes: read, write, and clear. Use the command \"help\" followed by a mode to learn more about its usage.")
}

fmt.Println("Invalid number of arguments!\nmp3-mp4-tag-cmd-help: The application offers 3 modes: read, write, and clear. Use the command \"help\" followed by a mode to learn more about its usage.")
}

}
}
}
25 changes: 25 additions & 0 deletions v3/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package audiometa

import "errors"

var (
ErrFLACParse = errors.New("error parsing flac stream")
ErrFLACCmtParse = errors.New("error parsing flac comment")

ErrMP4AtomOutOfBounds = errors.New("mp4 atom out of bounds")
ErrMP4InvalidAtomSize = errors.New("mp4 atom has invalid size")
ErrMP4InvalidEncoding = errors.New("invalid encoding: got wrong number of bytes")
ErrMP4IlstAtomMissing = errors.New("ilst atom is missing")
ErrMP4InvalidCntntType = errors.New("invalid content type")

ErrOggInvalidSgmtTblSz = errors.New("invalid segment table size")
ErrOggInvalidHeader = errors.New("invalid ogg header")
ErrOggInvalidCRC = errors.New("invalid CRC")
ErrOggMissingCOP = errors.New("missing ogg COP packet")
ErrOggImgConfigFail = errors.New("failed to get image config")
ErrOggCodecNotSpprtd = errors.New("unsupported codec for ogg")

ErrMP3ParseFail = errors.New("error parsing mp3")

ErrNoMethodAvlble = errors.New("no method available for this filetype")
)
10 changes: 10 additions & 0 deletions v3/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/gcottom/audiometa/v3

go 1.18

require (
github.com/aler9/writerseeker v1.1.0
github.com/bogem/id3v2/v2 v2.1.4
)

require golang.org/x/text v0.14.0 // indirect
30 changes: 30 additions & 0 deletions v3/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
github.com/aler9/writerseeker v1.1.0 h1:t+Sm3tjp8scNlqyoa8obpeqwciMNOvdvsxjxEb3Sx3g=
github.com/aler9/writerseeker v1.1.0/go.mod h1:QNCcjSKnLsYoTfMmXkEEfgbz6nNXWxKSaBY+hGJGWDA=
github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI=
github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
10 changes: 10 additions & 0 deletions v3/mp3/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/gcottom/audiometa/mp3/v3

go 1.18

require (
github.com/aler9/writerseeker v1.1.0
github.com/bogem/id3v2/v2 v2.1.4
)

require golang.org/x/text v0.3.8 // indirect
29 changes: 29 additions & 0 deletions v3/mp3/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
github.com/aler9/writerseeker v1.1.0 h1:t+Sm3tjp8scNlqyoa8obpeqwciMNOvdvsxjxEb3Sx3g=
github.com/aler9/writerseeker v1.1.0/go.mod h1:QNCcjSKnLsYoTfMmXkEEfgbz6nNXWxKSaBY+hGJGWDA=
github.com/bogem/id3v2/v2 v2.1.4 h1:CEwe+lS2p6dd9UZRlPc1zbFNIha2mb2qzT1cCEoNWoI=
github.com/bogem/id3v2/v2 v2.1.4/go.mod h1:l+gR8MZ6rc9ryPTPkX77smS5Me/36gxkMgDayZ9G1vY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
204 changes: 204 additions & 0 deletions v3/mp3/mp3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package mp3

import (
"fmt"
"image"
"io"
"strconv"
)

var mp3TextFrames = map[string]string{
"Artist": "TPE1",
"Title": "TIT2",
"Album": "TALB",
"BPM": "TBPM",
"Genre": "TCON",
"Year": "TYER",
"AlbumArtist": "TPE2",
"Composer": "TCOM",
"Copyright": "TCOP",
"Date": "TDRC",
"EncodedBy": "TENC",
"Lyricist": "TEXT",
"Language": "TLAN",
"Length": "TLEN",
"DiscNumberString": "TPOS",
"Publisher": "TPUB",
"SubTitle": "TIT3",
"ISRC": "TSRC",
"TrackNumberString": "TRCK",
}

type MP3Tag struct {
Album string
AlbumArt *image.Image
AlbumArtist string
Artist string
BPM string
Composer string //multiple seperated by /
Copyright string //must begin with year yyyy and a space
Date string //limit 4 char DDMM
DiscNumber int //TPOS
DiscNumberString string
DiscTotal int
EncodedBy string
Genre string
ISRC string
Language string
Length string //length in millisecs
Lyricist string //multiple seperated by /
Title string
TrackNumber int
TrackNumberString string
TrackTotal int
Publisher string
SubTitle string
Year string

reader io.ReadSeeker
}

func (t *MP3Tag) GetAlbum() string {
return t.Album
}
func (t *MP3Tag) GetCoverArt() *image.Image {
return t.AlbumArt
}
func (t *MP3Tag) GetAlbumArtist() string {
return t.AlbumArtist
}
func (t *MP3Tag) GetArtist() string {
return t.Artist
}
func (t *MP3Tag) GetBPM() int {
bpm, err := strconv.Atoi(t.BPM)
if err != nil {
return 0
}
return bpm
}
func (t *MP3Tag) GetComposer() string {
return t.Composer
}
func (t *MP3Tag) GetCopyright() string {
return t.Copyright
}
func (t *MP3Tag) GetDate() string {
return t.Date
}
func (t *MP3Tag) GetDiscNumber() int {
return t.DiscNumber
}
func (t *MP3Tag) GetDiscTotal() int {
return t.DiscTotal
}
func (t *MP3Tag) GetEncoder() string {
return t.EncodedBy
}
func (t *MP3Tag) GetGenre() string {
return t.Genre
}
func (t *MP3Tag) GetISRC() string {
return t.ISRC
}
func (t *MP3Tag) GetLanguage() string {
return t.Language
}
func (t *MP3Tag) GetLength() string {
return t.Length
}
func (t *MP3Tag) GetLyricist() string {
return t.Lyricist
}
func (t *MP3Tag) GetTitle() string {
return t.Title
}
func (t *MP3Tag) GetTrackNumber() int {
return t.TrackNumber
}
func (t *MP3Tag) GetTrackTotal() int {
return t.TrackTotal
}
func (t *MP3Tag) GetPublisher() string {
return t.Publisher
}
func (t *MP3Tag) GetSubTitle() string {
return t.SubTitle
}
func (t *MP3Tag) GetYear() int {
year, err := strconv.Atoi(t.Year)
if err != nil {
return 0
}
return year
}

func (t *MP3Tag) SetAlbum(album string) {
t.Album = album
}
func (t *MP3Tag) SetCoverArt(coverArt *image.Image) {
t.AlbumArt = coverArt
}
func (t *MP3Tag) SetAlbumArtist(albumArtist string) {
t.AlbumArtist = albumArtist
}
func (t *MP3Tag) SetArtist(artist string) {
t.Artist = artist
}
func (t *MP3Tag) SetBPM(bpm int) {
t.BPM = fmt.Sprint(bpm)
}
func (t *MP3Tag) SetComposer(composer string) {
t.Composer = composer
}
func (t *MP3Tag) SetCopyright(copyright string) {
t.Copyright = copyright
}
func (t *MP3Tag) SetDate(date string) {
t.Date = date
}
func (t *MP3Tag) SetDiscNumber(discNumber int) {
t.DiscNumber = discNumber
}
func (t *MP3Tag) SetDiscTotal(discTotal int) {
t.DiscTotal = discTotal
}
func (t *MP3Tag) SetEncoder(encoder string) {
t.EncodedBy = encoder
}
func (t *MP3Tag) SetGenre(genre string) {
t.Genre = genre
}
func (t *MP3Tag) SetISRC(isrc string) {
t.ISRC = isrc
}
func (t *MP3Tag) SetLanguage(language string) {
t.Language = language
}
func (t *MP3Tag) SetLength(length string) {
t.Length = length
}
func (t *MP3Tag) SetLyricist(lyricist string) {
t.Lyricist = lyricist
}
func (t *MP3Tag) SetTitle(title string) {
t.Title = title
}
func (t *MP3Tag) SetTrackNumber(trackNumber int) {
t.TrackNumber = trackNumber
}
func (t *MP3Tag) SetTrackTotal(trackTotal int) {
t.TrackTotal = trackTotal
}
func (t *MP3Tag) SetPublisher(publisher string) {
t.Publisher = publisher
}
func (t *MP3Tag) SetSubTitle(subTitle string) {
t.SubTitle = subTitle
}
func (t *MP3Tag) SetYear(year int) {
t.Year = fmt.Sprint(year)
}
func (t *MP3Tag) Save(w io.Writer) error {
return SaveMP3(t, w)
}
35 changes: 35 additions & 0 deletions v3/mp3/mp3_decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package mp3

import (
"bytes"
"errors"
"image"
"io"
"reflect"

mp3TagLib "github.com/bogem/id3v2/v2"
)

func ParseMP3(r io.ReadSeeker) (*MP3Tag, error) {
resultTag := MP3Tag{}
resultTag.reader = r
tag, err := mp3TagLib.ParseReader(r, mp3TagLib.Options{Parse: true})
if err != nil {
return nil, errors.New("error parsing mp3")
}
rtPtr := reflect.ValueOf(&resultTag).Elem()
for k, v := range mp3TextFrames {
framer := tag.GetTextFrame(v)
if framer.Text == "" {
continue
}
rtPtr.FieldByName(k)
}
if pictures := tag.GetFrames("APIC"); len(pictures) > 0 {
pic := pictures[0].(mp3TagLib.PictureFrame)
if img, _, err := image.Decode(bytes.NewReader(pic.Picture)); err == nil {
resultTag.AlbumArt = &img
}
}
return &resultTag, nil
}
Loading

0 comments on commit a05f9ed

Please sign in to comment.