Skip to content

FLAC vorbis comment metablock manipulation for go-flac

License

Notifications You must be signed in to change notification settings

go-flac/flacvorbis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flacvorbis

Documentation Build Status Coverage Status

FLAC vorbis comment metablock manipulation for go-flac

Examples

The following example extracts existing tags from a FLAC file. It returns the last vorbis comment block and also the corresponding index of the metadata, which could be used for updating later on.

package example

import (
    "github.com/go-flac/flacvorbis"
    "github.com/go-flac/go-flac"
)

func extractFLACComment(fileName string) (*flacvorbis.MetadataBlockVorbisComment, int) {
	f, err := flac.ParseFile(fileName)
	if err != nil {
		panic(err)
	}
    
	var cmt *flacvorbis.MetadataBlockVorbisComment
	var cmtIdx int
	for idx, meta := range f.Meta {
		if meta.Type == flac.VorbisComment {
			cmt, err = flacvorbis.ParseFromMetaDataBlock(*meta)
			cmtIdx = idx
			if err != nil {
				panic(err)
			}
		}
    	}
	return cmt, cmtIdx
}

The following example adds a title to the FLAC metadata. It considers whether there is already an existing vorbis comment block and updates it accordingly or otherwise creates a new one. Only add a new entry if you are sure there is none existing, multiple entries can be misleading for some audio players.

package example

import (
    "github.com/go-flac/flacvorbis"
    "github.com/go-flac/go-flac"
)

func addFLACTitle(fileName string, title []byte) {
	f, err := flac.ParseFile(fileName)
	if err != nil {
		panic(err)
	}
	cmts, idx := extractFLACComment(f)
	if cmts == nil && idx > 0 {
		cmts = flacvorbis.New()
	}
	cmts.Add(flacvorbis.FIELD_TITLE, title)
	cmtsmeta := cmts.Marshal()
	if idx > 0 {	
		f.Meta[idx] = &cmtsmeta
	} else {
		f.Meta = append(f.Meta, &cmtsmeta)
	}
	f.Save(fileName)
}

About

FLAC vorbis comment metablock manipulation for go-flac

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages