Documentation ¶
Overview ¶
Package id3v2 is the ID3 parsing and writing library for Go.
Example ¶
package main import ( "fmt" "log" "github.com/bogem/id3v2" ) func main() { // Open file and parse tag in it. tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if err != nil { log.Fatal("Error while opening mp3 file: ", err) } defer tag.Close() // Read frames. fmt.Println(tag.Artist()) fmt.Println(tag.Title()) // Set simple text frames. tag.SetArtist("Artist") tag.SetTitle("Title") // Set comment frame. comment := id3v2.CommentFrame{ Encoding: id3v2.EncodingUTF8, Language: "eng", Description: "My opinion", Text: "Very good song", } tag.AddCommentFrame(comment) // Write tag to file. if err = tag.Save(); err != nil { log.Fatal("Error while saving a tag: ", err) } }
Output:
Example (Concurrent) ¶
package main import ( "fmt" "log" "os" "sync" "github.com/bogem/id3v2" ) func main() { tagPool := sync.Pool{New: func() interface{} { return id3v2.NewEmptyTag() }} var wg sync.WaitGroup wg.Add(100) for i := 0; i < 100; i++ { go func() { defer wg.Done() tag := tagPool.Get().(*id3v2.Tag) defer tagPool.Put(tag) file, err := os.Open("file.mp3") if err != nil { log.Fatal("Error while opening file:", err) } defer file.Close() if err := tag.Reset(file, id3v2.Options{Parse: true}); err != nil { log.Fatal("Error while reseting tag to file:", err) } fmt.Println(tag.Artist() + " - " + tag.Title()) }() } wg.Wait() }
Output:
Index ¶
- Constants
- Variables
- type CommentFrame
- type Encoding
- type Framer
- type Options
- type PictureFrame
- type Tag
- func (tag *Tag) AddAttachedPicture(pf PictureFrame)
- func (tag *Tag) AddCommentFrame(cf CommentFrame)
- func (tag *Tag) AddFrame(id string, f Framer)
- func (tag *Tag) AddTextFrame(id string, encoding Encoding, text string)
- func (tag *Tag) AddUFIDFrame(ufid UFIDFrame)
- func (tag *Tag) AddUnsynchronisedLyricsFrame(uslf UnsynchronisedLyricsFrame)
- func (tag *Tag) AddUserDefinedTextFrame(udtf UserDefinedTextFrame)
- func (tag *Tag) Album() string
- func (tag *Tag) AllFrames() map[string][]Framer
- func (tag *Tag) Artist() string
- func (tag *Tag) Close() error
- func (tag *Tag) CommonID(description string) string
- func (tag *Tag) Count() int
- func (tag *Tag) DefaultEncoding() Encoding
- func (tag *Tag) DeleteAllFrames()
- func (tag *Tag) DeleteFrames(id string)
- func (tag *Tag) Genre() string
- func (tag *Tag) GetFrames(id string) []Framer
- func (tag *Tag) GetLastFrame(id string) Framer
- func (tag *Tag) GetTextFrame(id string) TextFrame
- func (tag *Tag) HasFrames() bool
- func (tag *Tag) Reset(rd io.Reader, opts Options) error
- func (tag *Tag) Save() error
- func (tag *Tag) SetAlbum(album string)
- func (tag *Tag) SetArtist(artist string)
- func (tag *Tag) SetDefaultEncoding(encoding Encoding)
- func (tag *Tag) SetGenre(genre string)
- func (tag *Tag) SetTitle(title string)
- func (tag *Tag) SetVersion(version byte)
- func (tag *Tag) SetYear(year string)
- func (tag *Tag) Size() int
- func (tag *Tag) Title() string
- func (tag *Tag) Version() byte
- func (tag *Tag) WriteTo(w io.Writer) (n int64, err error)
- func (tag *Tag) Year() string
- type TextFrame
- type UFIDFrame
- type UnknownFrame
- type UnsynchronisedLyricsFrame
- type UserDefinedTextFrame
Examples ¶
- Package
- Package (Concurrent)
- CommentFrame (Add)
- CommentFrame (Get)
- PictureFrame (Add)
- PictureFrame (Get)
- Tag.AddAttachedPicture
- Tag.AddCommentFrame
- Tag.AddUnsynchronisedLyricsFrame
- Tag.GetFrames
- Tag.GetLastFrame
- TextFrame (Add)
- TextFrame (Get)
- UnsynchronisedLyricsFrame (Add)
- UnsynchronisedLyricsFrame (Get)
Constants ¶
const ( PTOther = iota PTFileIcon PTOtherFileIcon PTFrontCover PTBackCover PTLeafletPage PTMedia PTLeadArtistSoloist PTArtistPerformer PTConductor PTBandOrchestra PTComposer PTLyricistTextWriter PTRecordingLocation PTDuringRecording PTDuringPerformance PTMovieScreenCapture PTBrightColouredFish PTIllustration PTBandArtistLogotype PTPublisherStudioLogotype )
Available picture types for picture frame.
Variables ¶
var ( V23CommonIDs = map[string]string{ "Attached picture": "APIC", "Comments": "COMM", "Album/Movie/Show title": "TALB", "BPM": "TBPM", "Composer": "TCOM", "Content type": "TCON", "Copyright message": "TCOP", "Date": "TDAT", "Playlist delay": "TDLY", "Encoded by": "TENC", "Lyricist/Text writer": "TEXT", "File type": "TFLT", "Time": "TIME", "Content group description": "TIT1", "Title/Songname/Content description": "TIT2", "Subtitle/Description refinement": "TIT3", "Initial key": "TKEY", "Language": "TLAN", "Length": "TLEN", "Media type": "TMED", "Original album/movie/show title": "TOAL", "Original filename": "TOFN", "Original lyricist/text writer": "TOLY", "Original artist/performer": "TOPE", "Original release year": "TORY", "File owner/licensee": "TOWN", "Lead artist/Lead performer/Soloist/Performing group": "TPE1", "Band/Orchestra/Accompaniment": "TPE2", "Conductor/performer refinement": "TPE3", "Interpreted, remixed, or otherwise modified by": "TPE4", "Part of a set": "TPOS", "Publisher": "TPUB", "Track number/Position in set": "TRCK", "Recording dates": "TRDA", "Internet radio station name": "TRSN", "Internet radio station owner": "TRSO", "Size": "TSIZ", "ISRC": "TSRC", "Software/Hardware and settings used for encoding": "TSSE", "Year": "TYER", "User defined text information frame": "TXXX", "Unique file identifier": "UFID", "Unsynchronised lyrics/text transcription": "USLT", "Artist": "TPE1", "Title": "TIT2", "Genre": "TCON", } V24CommonIDs = map[string]string{ "Attached picture": "APIC", "Comments": "COMM", "Album/Movie/Show title": "TALB", "BPM": "TBPM", "Composer": "TCOM", "Content type": "TCON", "Copyright message": "TCOP", "Encoding time": "TDEN", "Playlist delay": "TDLY", "Original release time": "TDOR", "Recording time": "TDRC", "Release time": "TDRL", "Tagging time": "TDTG", "Encoded by": "TENC", "Lyricist/Text writer": "TEXT", "File type": "TFLT", "Involved people list": "TIPL", "Content group description": "TIT1", "Title/Songname/Content description": "TIT2", "Subtitle/Description refinement": "TIT3", "Initial key": "TKEY", "Language": "TLAN", "Length": "TLEN", "Musician credits list": "TMCL", "Media type": "TMED", "Mood": "TMOO", "Original album/movie/show title": "TOAL", "Original filename": "TOFN", "Original lyricist/text writer": "TOLY", "Original artist/performer": "TOPE", "File owner/licensee": "TOWN", "Lead artist/Lead performer/Soloist/Performing group": "TPE1", "Band/Orchestra/Accompaniment": "TPE2", "Conductor/performer refinement": "TPE3", "Interpreted, remixed, or otherwise modified by": "TPE4", "Part of a set": "TPOS", "Produced notice": "TPRO", "Publisher": "TPUB", "Track number/Position in set": "TRCK", "Internet radio station name": "TRSN", "Internet radio station owner": "TRSO", "Album sort order": "TSOA", "Performer sort order": "TSOP", "Title sort order": "TSOT", "ISRC": "TSRC", "Software/Hardware and settings used for encoding": "TSSE", "Set subtitle": "TSST", "User defined text information frame": "TXXX", "Unique file identifier": "UFID", "Unsynchronised lyrics/text transcription": "USLT", "Date": "TDRC", "Time": "TDRC", "Original release year": "TDOR", "Recording dates": "TDRC", "Size": "", "Year": "TDRC", "Artist": "TPE1", "Title": "TIT2", "Genre": "TCON", } )
Common IDs for ID3v2.3 and ID3v2.4.
var ( // EncodingISO is ISO-8859-1 encoding. EncodingISO = Encoding{ Name: "ISO-8859-1", Key: 0, TerminationBytes: []byte{0}, } // EncodingUTF16 is UTF-16 encoded Unicode with BOM. EncodingUTF16 = Encoding{ Name: "UTF-16 encoded Unicode with BOM", Key: 1, TerminationBytes: []byte{0, 0}, } // EncodingUTF16BE is UTF-16BE encoded Unicode without BOM. EncodingUTF16BE = Encoding{ Name: "UTF-16BE encoded Unicode without BOM", Key: 2, TerminationBytes: []byte{0, 0}, } // EncodingUTF8 is UTF-8 encoded Unicode. EncodingUTF8 = Encoding{ Name: "UTF-8 encoded Unicode", Key: 3, TerminationBytes: []byte{0}, } )
Available encodings.
var ErrInvalidLanguageLength = errors.New("language code must consist of three letters according to ISO 639-2")
var ErrInvalidSizeFormat = errors.New("invalid format of tag's/frame's size")
var ErrNoFile = errors.New("tag was not initialized with file")
var ErrSizeOverflow = errors.New("size of tag/frame is greater than allowed in id3 tag")
var ErrSmallHeaderSize = errors.New("size of tag header is less than expected")
var ErrUnsupportedVersion = errors.New("unsupported version of ID3 tag")
Functions ¶
This section is empty.
Types ¶
type CommentFrame ¶ added in v0.6.1
CommentFrame is used to work with COMM frames. The information about how to add comment frame to tag you can see in the docs to tag.AddCommentFrame function.
You must choose a three-letter language code from ISO 639-2 code list: https://www.loc.gov/standards/iso639-2/php/code_list.php
Example (Add) ¶
package main import ( "github.com/bogem/id3v2" ) func main() { tag := id3v2.NewEmptyTag() comment := id3v2.CommentFrame{ Encoding: id3v2.EncodingUTF8, Language: "eng", Description: "My opinion", Text: "Very good song", } tag.AddCommentFrame(comment) }
Output:
Example (Get) ¶
package main import ( "fmt" "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } comments := tag.GetFrames(tag.CommonID("Comments")) for _, f := range comments { comment, ok := f.(id3v2.CommentFrame) if !ok { log.Fatal("Couldn't assert comment frame") } // Do something with comment frame. // For example, print the text: fmt.Println(comment.Text) } }
Output:
func (CommentFrame) Size ¶ added in v0.8.6
func (cf CommentFrame) Size() int
type Framer ¶ added in v0.6.1
type Framer interface { // Size returns the size of frame body. Size() int // WriteTo writes body slice into w. WriteTo(w io.Writer) (n int64, err error) }
Framer provides a generic interface for frames. You can create your own frames. They must implement only this interface.
type Options ¶ added in v0.12.1
type Options struct { // Parse defines, if tag will be parsed. Parse bool // ParseFrames defines, that frames do you only want to parse. For example, // `ParseFrames: []string{"Artist", "Title"}` will only parse artist // and title frames. You can specify IDs ("TPE1", "TIT2") as well as // descriptions ("Artist", "Title"). If ParseFrame is blank or nil, // id3v2 will parse all frames in tag. It works only if Parse is true. // // It's very useful for performance, so for example // if you want to get only some text frames, // id3v2 will not parse huge picture or unknown frames. ParseFrames []string }
Options influence on processing the tag.
type PictureFrame ¶ added in v0.6.1
type PictureFrame struct { Encoding Encoding MimeType string PictureType byte Description string Picture []byte }
PictureFrame structure is used for picture frames (APIC). The information about how to add picture frame to tag you can see in the docs to tag.AddAttachedPicture function.
Available picture types you can see in constants.
Example (Add) ¶
package main import ( "io/ioutil" "log" "github.com/bogem/id3v2" ) func main() { tag := id3v2.NewEmptyTag() artwork, err := ioutil.ReadFile("artwork.jpg") if err != nil { log.Fatal("Error while reading artwork file", err) } pic := id3v2.PictureFrame{ Encoding: id3v2.EncodingUTF8, MimeType: "image/jpeg", PictureType: id3v2.PTFrontCover, Description: "Front cover", Picture: artwork, } tag.AddAttachedPicture(pic) }
Output:
Example (Get) ¶
package main import ( "fmt" "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } pictures := tag.GetFrames(tag.CommonID("Attached picture")) for _, f := range pictures { pic, ok := f.(id3v2.PictureFrame) if !ok { log.Fatal("Couldn't assert picture frame") } // Do something with picture frame. // For example, print the description: fmt.Println(pic.Description) } }
Output:
func (PictureFrame) Size ¶ added in v0.8.6
func (pf PictureFrame) Size() int
type Tag ¶
type Tag struct {
// contains filtered or unexported fields
}
Tag stores all information about opened tag.
func NewEmptyTag ¶ added in v0.12.1
func NewEmptyTag() *Tag
NewEmptyTag returns an empty ID3v2.4 tag without any frames and reader.
func Open ¶
Open opens file with name and passes it to OpenFile. If there is no tag in file, it will create new one with version ID3v2.4.
func ParseReader ¶ added in v0.12.1
ParseReader parses rd and finds tag in it considering opts. If there is no tag in rd, it will create new one with version ID3v2.4.
func (*Tag) AddAttachedPicture ¶ added in v0.5.1
func (tag *Tag) AddAttachedPicture(pf PictureFrame)
AddAttachedPicture adds the picture frame to tag.
Example ¶
package main import ( "io/ioutil" "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } artwork, err := ioutil.ReadFile("artwork.jpg") if err != nil { log.Fatal("Error while reading artwork file", err) } pic := id3v2.PictureFrame{ Encoding: id3v2.EncodingUTF8, MimeType: "image/jpeg", PictureType: id3v2.PTFrontCover, Description: "Front cover", Picture: artwork, } tag.AddAttachedPicture(pic) }
Output:
func (*Tag) AddCommentFrame ¶ added in v0.5.1
func (tag *Tag) AddCommentFrame(cf CommentFrame)
AddCommentFrame adds the comment frame to tag.
Example ¶
package main import ( "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } comment := id3v2.CommentFrame{ Encoding: id3v2.EncodingUTF8, Language: "eng", Description: "My opinion", Text: "Very good song", } tag.AddCommentFrame(comment) }
Output:
func (*Tag) AddFrame ¶ added in v0.5.1
AddFrame adds f to tag with appropriate id. If id is "" or f is nil, AddFrame will not add it to tag.
If you want to add attached picture, comment or unsynchronised lyrics/text transcription frames, better use AddAttachedPicture, AddCommentFrame or AddUnsynchronisedLyricsFrame methods respectively.
func (*Tag) AddTextFrame ¶ added in v1.1.1
AddTextFrame creates the text frame with provided encoding and text and adds to tag.
func (*Tag) AddUFIDFrame ¶ added in v1.1.0
AddUFIDFrame adds the unique file identifier frame (UFID) to tag.
func (*Tag) AddUnsynchronisedLyricsFrame ¶ added in v0.5.1
func (tag *Tag) AddUnsynchronisedLyricsFrame(uslf UnsynchronisedLyricsFrame)
AddUnsynchronisedLyricsFrame adds the unsynchronised lyrics/text frame to tag.
Example ¶
package main import ( "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } uslt := id3v2.UnsynchronisedLyricsFrame{ Encoding: id3v2.EncodingUTF8, Language: "ger", ContentDescriptor: "Deutsche Nationalhymne", Lyrics: "Einigkeit und Recht und Freiheit...", } tag.AddUnsynchronisedLyricsFrame(uslt) }
Output:
func (*Tag) AddUserDefinedTextFrame ¶ added in v1.1.0
func (tag *Tag) AddUserDefinedTextFrame(udtf UserDefinedTextFrame)
AddUserDefinedTextFrame adds the custom frame (TXXX) to tag.
func (*Tag) AllFrames ¶ added in v0.7.1
AllFrames returns map, that contains all frames in tag, that could be parsed. The key of this map is an ID of frame and value is an array of frames.
func (*Tag) Close ¶
Close closes tag's file, if tag was opened with a file. If tag was initiliazed not with file, it returns ErrNoFile.
func (*Tag) CommonID ¶ added in v0.8.3
CommonID returns ID3v2.3 or ID3v2.4 (in appropriate to version of Tag) frame ID from given description. For example, CommonID("Language") will return "TLAN". If it can't find the ID with given description, it returns the description.
All descriptions you can find in file common_ids.go or in id3 documentation (for fourth version: http://id3.org/id3v2.4.0-frames; for third version: http://id3.org/id3v2.3.0#Declared_ID3v2_frames).
func (*Tag) DefaultEncoding ¶ added in v1.1.1
DefaultEncoding returns default encoding of tag. Default encoding is used in methods (e.g. SetArtist, SetAlbum ...) for setting text frames without the explicit providing of encoding.
func (*Tag) DeleteAllFrames ¶ added in v0.8.1
func (tag *Tag) DeleteAllFrames()
DeleteAllFrames deletes all frames in tag.
func (*Tag) DeleteFrames ¶ added in v0.8.1
DeleteFrames deletes frames in tag with given id.
func (*Tag) GetFrames ¶ added in v0.7.1
GetFrames returns frames with corresponding id. It returns nil if there is no frames with given id.
Example ¶
package main import ( "fmt" "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } pictures := tag.GetFrames(tag.CommonID("Attached picture")) for _, f := range pictures { pic, ok := f.(id3v2.PictureFrame) if !ok { log.Fatal("Couldn't assert picture frame") } // Do something with picture frame. // For example, print description of picture frame: fmt.Println(pic.Description) } }
Output:
func (*Tag) GetLastFrame ¶ added in v0.7.1
GetLastFrame returns last frame from slice, that is returned from GetFrames function. GetLastFrame is suitable for frames, that can be only one in whole tag. For example, for text frames.
Example ¶
package main import ( "fmt" "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } bpmFramer := tag.GetLastFrame(tag.CommonID("BPM")) if bpmFramer != nil { bpm, ok := bpmFramer.(id3v2.TextFrame) if !ok { log.Fatal("Couldn't assert bpm frame") } fmt.Println(bpm.Text) } }
Output:
func (*Tag) GetTextFrame ¶ added in v0.7.1
GetTextFrame returns text frame with corresponding id.
func (*Tag) HasFrames ¶ added in v0.12.1
HasFrames checks if there is at least one frame in tag. It's much faster than tag.Count() > 0.
func (*Tag) Reset ¶ added in v0.12.1
Reset deletes all frames in tag and parses rd considering opts.
func (*Tag) Save ¶ added in v0.7.1
Save writes tag to the file, if tag was opened with a file. If there are no frames in tag, Save will write only music part without any ID3v2 information. If tag was initiliazed not with file, it returns ErrNoFile.
func (*Tag) SetDefaultEncoding ¶ added in v1.1.1
SetDefaultEncoding sets default encoding for tag. Default encoding is used in methods (e.g. SetArtist, SetAlbum ...) for setting text frames without explicit providing encoding.
func (*Tag) SetVersion ¶ added in v0.8.7
SetVersion sets given ID3v2 version to tag. If version is less than 3 or greater than 4, then this method will do nothing. If tag has some frames, which are deprecated or changed in given version, then to your notice you can delete, change or just stay them.
func (*Tag) Size ¶ added in v0.8.8
Size returns the size of tag (tag header + size of all frames) in bytes.
type TextFrame ¶ added in v0.6.1
TextFrame is used to work with all text frames (all T*** frames like TIT2 (title), TALB (album) and so on).
Example (Add) ¶
package main import ( "github.com/bogem/id3v2" ) func main() { tag := id3v2.NewEmptyTag() textFrame := id3v2.TextFrame{ Encoding: id3v2.EncodingUTF8, Text: "Happy", } tag.AddFrame(tag.CommonID("Mood"), textFrame) }
Output:
Example (Get) ¶
package main import ( "fmt" "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } tf := tag.GetTextFrame(tag.CommonID("Mood")) fmt.Println(tf.Text) }
Output:
type UnknownFrame ¶ added in v0.8.1
type UnknownFrame struct {
Body []byte
}
UnknownFrame is used for frames, which id3v2 so far doesn't know how to parse and write it. It just contains an unparsed byte body of the frame.
func (UnknownFrame) Size ¶ added in v0.8.6
func (uf UnknownFrame) Size() int
type UnsynchronisedLyricsFrame ¶ added in v0.6.1
type UnsynchronisedLyricsFrame struct { Encoding Encoding Language string ContentDescriptor string Lyrics string }
UnsynchronisedLyricsFrame is used to work with USLT frames. The information about how to add unsynchronised lyrics/text frame to tag you can see in the docs to tag.AddUnsynchronisedLyricsFrame function.
You must choose a three-letter language code from ISO 639-2 code list: https://www.loc.gov/standards/iso639-2/php/code_list.php
Example (Add) ¶
package main import ( "github.com/bogem/id3v2" ) func main() { tag := id3v2.NewEmptyTag() uslt := id3v2.UnsynchronisedLyricsFrame{ Encoding: id3v2.EncodingUTF8, Language: "ger", ContentDescriptor: "Deutsche Nationalhymne", Lyrics: "Einigkeit und Recht und Freiheit...", } tag.AddUnsynchronisedLyricsFrame(uslt) }
Output:
Example (Get) ¶
package main import ( "fmt" "log" "github.com/bogem/id3v2" ) func main() { tag, err := id3v2.Open("file.mp3", id3v2.Options{Parse: true}) if tag == nil || err != nil { log.Fatal("Error while opening mp3 file: ", err) } uslfs := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription")) for _, f := range uslfs { uslf, ok := f.(id3v2.UnsynchronisedLyricsFrame) if !ok { log.Fatal("Couldn't assert USLT frame") } // Do something with USLT frame. // For example, print the lyrics: fmt.Println(uslf.Lyrics) } }
Output:
func (UnsynchronisedLyricsFrame) Size ¶ added in v0.8.6
func (uslf UnsynchronisedLyricsFrame) Size() int
type UserDefinedTextFrame ¶ added in v1.1.0
UserDefinedTextFrame is used to work with TXXX frames. There can be many UserDefinedTextFrames but the Desciption fields need to be unique.
func (UserDefinedTextFrame) Size ¶ added in v1.1.0
func (udtf UserDefinedTextFrame) Size() int