FLAC picture metablock manipulation for go-flac
The following example adds a jpeg image as front cover to the FLAC metadata.
package example
import (
"github.com/go-flac/flacpicture/v2"
"github.com/go-flac/go-flac/v2"
)
func addFLACCover(fileName string, imgData []byte) {
f, err := flac.ParseFile(fileName)
if err != nil {
panic(err)
}
picture, err := flacpicture.NewFromImageData(flacpicture.PictureTypeFrontCover, "Front cover", imgData, "image/jpeg")
if err != nil {
panic(err)
}
picturemeta := picture.Marshal()
f.Meta = append(f.Meta, &picturemeta)
f.Save(fileName)
}
The following example extracts an existing cover from a FLAC file
package example
import (
"github.com/go-flac/flacpicture/v2"
"github.com/go-flac/go-flac/v2"
)
func extractFLACCover(fileName string) *flacpicure.MetadataBlockPicture {
f, err := flac.ParseFile(fileName)
if err != nil {
panic(err)
}
var pic *flacpicure.MetadataBlockPicture
for _, meta := range f.Meta {
if meta.Type == flac.Picture {
pic, err = flacpicure.ParseFromMetaDataBlock(*meta)
if err != nil {
panic(err)
}
}
}
return pic
}