yurit

package module
v0.0.0-...-4e6fec9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2021 License: BSD-2-Clause Imports: 12 Imported by: 0

README

MP3/MP4/OGG/FLAC metadata parsing library

Build Status GoDoc

This package provides MP3 (ID3v1,2.{2,3,4}) and MP4 (ACC, M4A, ALAC), OGG and FLAC metadata detection, parsing and artwork extraction.

Detect and parse tag metadata from an io.ReadSeeker (i.e. an *os.File):

m, err := tag.ReadFrom(f)
if err != nil {
	log.Fatal(err)
}
log.Print(m.Format()) // The detected format.
log.Print(m.Title())  // The title of the track (see Metadata interface for more details).

Parsed metadata is exported via a single interface (giving a consistent API for all supported metadata formats).

// Metadata is an interface which is used to describe metadata retrieved by this package.
type Metadata interface {
	Format() Format
	FileType() FileType

	Title() string
	Album() string
	Artist() string
	AlbumArtist() string
	Composer() string
	Genre() string
	Year() int

	Track() (int, int) // Number, Total
	Disc() (int, int) // Number, Total

	Picture() *Picture // Artwork
	Lyrics() string
	Comment() string

	Raw() map[string]interface{} // NB: raw tag names are not consistent across formats.
}

Audio Data Checksum (SHA1)

This package also provides a metadata-invariant checksum for audio files: only the audio data is used to construct the checksum.

http://godoc.org/github.com/dhowden/tag#Sum

Tools

There are simple command-line tools which demonstrate basic tag extraction and summing:

$ go get github.com/dhowden/tag/...
$ cd $GOPATH/bin
$ ./tag 11\ High\ Hopes.m4a
Metadata Format: MP4
Title: High Hopes
Album: The Division Bell
Artist: Pink Floyd
Composer: Abbey Road Recording Studios/David Gilmour/Polly Samson
Year: 1994
Track: 11 of 11
Disc: 1 of 1
Picture: Picture{Ext: jpeg, MIMEType: image/jpeg, Type: , Description: , Data.Size: 606109}

$ ./sum 11\ High\ Hopes.m4a
2ae208c5f00a1f21f5fac9b7f6e0b8e52c06da29

Documentation

Overview

Package tag provides MP3 (ID3: v1, 2.2, 2.3 and 2.4), MP4, FLAC and OGG metadata detection, parsing and artwork extraction.

Detect and parse tag metadata from an io.ReadSeeker (i.e. an *os.File):

m, err := tag.ReadFrom(f)
if err != nil {
	log.Fatal(err)
}
log.Print(m.Format()) // The detected format.
log.Print(m.Title())  // The title of the track (see Metadata interface for more details).

Index

Constants

View Source
const (
	AlwaysMinus2Key        = "alwaysMinus2"
	Always0Key             = "always0"
	Always3Key             = "always3"
	Always16Key            = "always16"
	Always65536Key         = "always65536"
	Always7F000000Key      = "always7F000000"
	AverageBitrateKey      = "averageBitrate"
	BytesPerFrameKey       = "bytesPerFrame"
	BytesPerPacketKey      = "bytesPerPacket"
	BytesPerSampleKey      = "bytesPerSample"
	ChannelsKey            = "channels"
	CompatibleBrandsKey    = "compatibleBrands"
	CompressionKey         = "compression"
	ConstBitsPerChannelKey = "constBitsPerChannel"
	ConstBytesPerPacket    = "constBytesPerPacket"
	ConstFramesPerPacket   = "constFramesPerPacket"
	DurationKey            = "duration"
	ExperimentalKey        = "experimental"
	ExtendedHeaderKey      = "extendedHeader"
	FooterKey              = "footer"
	FormatKey              = "format"
	FlagsKey               = "flags"
	HeaderSizeKey          = "headerSize"
	LPCMFlagsKey           = "lpcmFlags"
	MajorBrandKey          = "majorBrand"
	MaximumBitrateKey      = "maximumBitrate"
	MaximumBlockSizeKey    = "maximumBlockSize"
	MaximumFrameSizeKey    = "maximumFrameSize"
	MD5Key                 = "md5"
	MinimumBitrateKey      = "minimumBitrate"
	MinimumBlockSizeKey    = "minimumBlockSize"
	MinimumFrameSizeKey    = "minimumFrameSize"
	MinorVersionKey        = "minorVersion"
	PacketSizeKey          = "packetSize"
	RevisionKey            = "revision"
	SampleRateKey          = "sampleRate"
	SampleSizeKey          = "sampleSize"
	SamplesPerPacketKey    = "samplesPerPacket"
	SizeOfStructOnlyKey    = "sizeOfStructOnly"
	TimeScaleKey           = "timeScale"
	TotalBytesKey          = "totalBytes"
	TotalFramesKey         = "totalFrames"
	TotalSamplesKey        = "totalSamples"
	UnsynchronizationKey   = "unsynchronization"
	VendorKey              = "vendor"
	VersionKey             = "version"
)

Variables

View Source
var DefaultUTF16WithBOMByteOrder binary.ByteOrder = binary.LittleEndian

DefaultUTF16WithBOMByteOrder is the byte order used when the "UTF16 with BOM" encoding is specified without a corresponding BOM in the data.

Functions

func ReadID3v1Tags

func ReadID3v1Tags(r io.ReadSeeker) (id3v1tags, error)

ReadID3v1Tags reads ID3v1 tags from the io.ReadSeeker. If there is no ID3v1 tag, returns nil.

func ReadID3v2Tags

func ReadID3v2Tags(r io.ReadSeeker) (*id3v2Tags, error)

ReadID3v2Tags reads ID3v2 tags from the io.ReadSeeker. If there is no ID3v2 tag, returns nil. Method assumes that the reader has been prepositioned to the beginning of the tag, which should be the beginning of the file.

Types

type Comm

type Comm struct {
	Language    string
	Description string
	Text        string
}

Comm is a type used in COMM, UFID, TXXX, WXXX and USLT tag. It's a text with a description and a specified language For WXXX, TXXX and UFID, we don't set a Language

func (Comm) String

func (t Comm) String() string

String returns a string representation of the underlying Comm instance.

type FLACMetadata

type FLACMetadata struct {
	// contains filtered or unexported fields
}

FLACMetadata is a collection of metadata and other useful data from a native FLAC container with a Vorbis comment.

func ReadFLACTags

func ReadFLACTags(file *os.File) (*FLACMetadata, error)

ReadFLACTags reads FLAC metadata from a FLAC file, returning the resulting metadata in a Metadata implementation, or non-nil error if there was a problem.

func (FLACMetadata) Album

func (m FLACMetadata) Album() string

func (FLACMetadata) AlbumArtist

func (m FLACMetadata) AlbumArtist() string

func (FLACMetadata) Artist

func (m FLACMetadata) Artist() string

func (FLACMetadata) AverageBitrate

func (m FLACMetadata) AverageBitrate() int

AverageBitrate returns the roughly calculated average bitrate of the file in bits per second.

While metadata is discounted for this calculation, frame headers are not, so the returned value is likely to be slightly higher than the actual. This difference is expected to be minor in most cases, though, and since average bitrate for a FLAC file is fairly meaningless, the returned value is considered sufficiently accurate.

func (FLACMetadata) Comment

func (m FLACMetadata) Comment() string

func (FLACMetadata) Composer

func (m FLACMetadata) Composer() string

func (FLACMetadata) Disc

func (m FLACMetadata) Disc() (int, int)

func (FLACMetadata) Duration

func (m FLACMetadata) Duration() time.Duration

func (FLACMetadata) FileType

func (m FLACMetadata) FileType() FileType

func (FLACMetadata) Format

func (m FLACMetadata) Format() Format

func (FLACMetadata) Genre

func (m FLACMetadata) Genre() string

func (FLACMetadata) Lyrics

func (m FLACMetadata) Lyrics() string

func (FLACMetadata) Picture

func (m FLACMetadata) Picture() *Picture

Picture attempts to return front cover art, else it returns the first picture found in the FLAC metadata

func (FLACMetadata) Pictures

func (m FLACMetadata) Pictures() []Picture

Pictures returns ALL pictures found in a FLAC file's metadata. https://xiph.org/flac/format.html#metadata_block_picture

func (FLACMetadata) SampleRate

func (m FLACMetadata) SampleRate() int

SampleRate returns the SampleRate from a FLAC file's stream info block

func (FLACMetadata) StreamInfo

func (m FLACMetadata) StreamInfo() map[string]interface{}

StreamInfo returns the data extracted from a FLAC file's stream info metadata block. See the StreamInfo struct type for more information.

func (FLACMetadata) Title

func (m FLACMetadata) Title() string

func (FLACMetadata) Track

func (m FLACMetadata) Track() (int, int)

func (FLACMetadata) VorbisComment

func (m FLACMetadata) VorbisComment() map[string]string

VorbisComment returns the information found in a FLAC file's Vorbis comment metadata block. The data in this block is sometimes also referred to as FLAC tags.

func (FLACMetadata) Year

func (m FLACMetadata) Year() int

type FileType

type FileType string

FileType is an enumeration of the audio file types supported by this package, in particular there are audio file types which share metadata formats, and this type is used to distinguish between them.

const (
	UnknownFileType FileType = ""     // Unknown FileType.
	MP1             FileType = "MP1"  // MP1 file
	MP2             FileType = "MP2"  // MP2 file
	MP3             FileType = "MP3"  // MP3 file
	M4A             FileType = "M4A"  // M4A file Apple iTunes (ACC) Audio
	M4B             FileType = "M4B"  // M4A file Apple iTunes (ACC) Audio Book
	M4P             FileType = "M4P"  // M4A file Apple iTunes (ACC) AES Protected Audio
	ALAC            FileType = "ALAC" // Apple Lossless file FIXME: actually detect this
	FLAC            FileType = "FLAC" // FLAC file
	OGG             FileType = "OGG"  // OGG file
	DSF             FileType = "DSF"  // DSF file DSD Sony format see https://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf
)

Supported file types.

type Format

type Format string

Format is an enumeration of metadata types supported by this package.

const (
	UnknownFormat Format = ""        // Unknown Format.
	ID3v1         Format = "ID3v1"   // ID3v1 tag format.
	ID3v2_2       Format = "ID3v2.2" // ID3v2.2 tag format.
	ID3v2_3       Format = "ID3v2.3" // ID3v2.3 tag format (most common).
	ID3v2_4       Format = "ID3v2.4" // ID3v2.4 tag format.
	MP4           Format = "MP4"     // MP4 tag (atom) format (see http://www.ftyps.com/ for a full file type list)
	VORBIS        Format = "VORBIS"  // Vorbis Comment tag format.
)

Supported tag formats.

type MP3Metadata

type MP3Metadata struct {
	// contains filtered or unexported fields
}

MP3Metadata is a collection of metadata from an mp3 file including tags and frame information.

func ReadFromMP3

func ReadFromMP3(file *os.File) (*MP3Metadata, error)

func (MP3Metadata) Album

func (m MP3Metadata) Album() string

func (MP3Metadata) AlbumArtist

func (m MP3Metadata) AlbumArtist() string

func (MP3Metadata) Artist

func (m MP3Metadata) Artist() string

func (MP3Metadata) AverageBitrate

func (m MP3Metadata) AverageBitrate() int

func (MP3Metadata) Comment

func (m MP3Metadata) Comment() string

func (MP3Metadata) Composer

func (m MP3Metadata) Composer() string

func (MP3Metadata) Disc

func (m MP3Metadata) Disc() (int, int)

func (MP3Metadata) Duration

func (m MP3Metadata) Duration() time.Duration

func (MP3Metadata) FileType

func (m MP3Metadata) FileType() FileType

func (MP3Metadata) Format

func (m MP3Metadata) Format() Format

func (MP3Metadata) Genre

func (m MP3Metadata) Genre() string

func (MP3Metadata) ID3v2Frames

func (m MP3Metadata) ID3v2Frames() map[string]interface{}

func (MP3Metadata) Lyrics

func (m MP3Metadata) Lyrics() string

func (MP3Metadata) Picture

func (m MP3Metadata) Picture() *Picture

func (MP3Metadata) Title

func (m MP3Metadata) Title() string

func (MP3Metadata) Track

func (m MP3Metadata) Track() (int, int)

func (MP3Metadata) Year

func (m MP3Metadata) Year() int

type MP4Metadata

type MP4Metadata struct {
	// contains filtered or unexported fields
}

MP4Metadata is the implementation of Metadata for MP4 tag (atom) data.

func ReadMP4

func ReadMP4(r io.ReadSeeker) (*MP4Metadata, error)

ReadMP4 reads MP4 metadata atoms from the io.ReadSeeker into a Metadata, returning non-nil error if there was a problem.

func (MP4Metadata) Album

func (m MP4Metadata) Album() string

func (MP4Metadata) AlbumArtist

func (m MP4Metadata) AlbumArtist() string

func (MP4Metadata) Artist

func (m MP4Metadata) Artist() string

func (MP4Metadata) AverageBitrate

func (m MP4Metadata) AverageBitrate() int

func (MP4Metadata) Comment

func (m MP4Metadata) Comment() string

func (MP4Metadata) Composer

func (m MP4Metadata) Composer() string

func (MP4Metadata) Disc

func (m MP4Metadata) Disc() (int, int)

func (MP4Metadata) Duration

func (m MP4Metadata) Duration() time.Duration

func (MP4Metadata) ESDS

func (m MP4Metadata) ESDS() map[string]interface{}

Returns information extracted from the elementary stream descriptor atom ('esds') found in the file.

func (MP4Metadata) FTYP

func (m MP4Metadata) FTYP() map[string]interface{}

func (MP4Metadata) FileType

func (m MP4Metadata) FileType() FileType

func (MP4Metadata) Format

func (m MP4Metadata) Format() Format

func (MP4Metadata) Genre

func (m MP4Metadata) Genre() string

func (MP4Metadata) Lyrics

func (m MP4Metadata) Lyrics() string

func (MP4Metadata) MP4A

func (m MP4Metadata) MP4A() map[string]interface{}

Returns information extracted from the MP4A sound sample description atom ('mp4a') found in the file.

func (MP4Metadata) MVHD

func (m MP4Metadata) MVHD() map[string]interface{}

Returns information extracted from the movie header atom ('mvhd') found in the file.

func (MP4Metadata) Picture

func (m MP4Metadata) Picture() *Picture

func (MP4Metadata) Raw

func (m MP4Metadata) Raw() map[string]interface{}

func (MP4Metadata) Title

func (m MP4Metadata) Title() string

func (MP4Metadata) Track

func (m MP4Metadata) Track() (int, int)

func (MP4Metadata) Year

func (m MP4Metadata) Year() int

type MPEGChannelMode

type MPEGChannelMode string

MPEGChannelMode is simply the channel mode for the audio

const (
	MPEGChannelStereo      MPEGChannelMode = "Stereo"
	MPEGChannelJointStereo MPEGChannelMode = "Joint stereo (Stereo)"
	MPEGChannelDual        MPEGChannelMode = "Dual channel (2 mono channels)"
	MPEGChannelSingle      MPEGChannelMode = "Single channel (Mono)"
)

All possible channel mode values

type MPEGEmphasis

type MPEGEmphasis string

MPEGEmphasis gives the decoder instructions on how to de-emphasize sound in the file. It is rarely used.

const (
	MPEGEmphasisNone     MPEGEmphasis = "none"
	MPEGEmphasis50_15    MPEGEmphasis = "50/15 ms"
	MPEGEmphasisReserved MPEGEmphasis = "reserved"
	MPEGEmphasisCCIT     MPEGEmphasis = "CCIT J.17"
)

All possible emphasis values

type MPEGLayer

type MPEGLayer string

MPEGLayer is the layer index for the file. For an MP3 this will be Layer III, an MP2 would be Layer II, and an MP1 would be Layer I.

const (
	MPEGLayerReserved MPEGLayer = "reserved"
	MPEGLayer3        MPEGLayer = "Layer III" //mp3
	MPEGLayer2        MPEGLayer = "Layer II"  //mp2
	MPEGLayer1        MPEGLayer = "Layer I"   //mp1
)

All possible layer values

type MPEGModeExtension

type MPEGModeExtension string

MPEGModeExtension provides additional information about how the audio is encoded if the channel mode is 'Joint stereo'. The mode extension is not applicable for other channel modes.

const (
	//Channel mode is not joint stereo
	MPEGModeExtensionNA MPEGModeExtension = "not applicable"
	//These four mode extensions only apply to layers I and II
	MPEGModeExtension4To31  MPEGModeExtension = "bands 4 to 31"
	MPEGModeExtension8To31  MPEGModeExtension = "bands 8 to 31"
	MPEGModeExtension12To31 MPEGModeExtension = "bands 12 to 31"
	MPEGModeExtension16To31 MPEGModeExtension = "bands 16 to 31"
	//These four mode extensions only apply to layer III
	MPEGModeExtensionMSOffIntensityOff MPEGModeExtension = "M/S stereo off, Intensity stereo off"
	MPEGModeExtensionMSOffIntensityOn  MPEGModeExtension = "M/S stereo off, Intensity stereo on"
	MPEGModeExtensionMSOnIntensityOff  MPEGModeExtension = "M/S stereo on, Intensity stereo off"
	MPEGModeExtensionMSOnIntensityOn   MPEGModeExtension = "M/S stereo on, Intensity stereo on"
)

type MPEGVersion

type MPEGVersion string

MPEGVersion is the audio version ID for the file. For most common MP3 files this will almost always be MPEG Version 1. The sampling rate for a file will exclusively map to one of these versions. (e.g. All 44.1 kHz files are MPEG Version 1)

const (
	MPEGVersion_2_5     MPEGVersion = "MPEG Version 2.5"
	MPEGVersionReserved MPEGVersion = "reserved"
	MPEGVersion_2       MPEGVersion = "MPEG Version 2"
	MPEGVersion_1       MPEGVersion = "MPEG Version 1"
)

All possible version values.

type Metadata

type Metadata interface {
	// Album returns the album name of the track.
	Album() string
	// AlbumArtist returns the album artist name of the track.
	AlbumArtist() string
	// Artist returns the artist name of the track.
	Artist() string
	// AverageBitrate returns the average bitrate of the file in bits per second
	AverageBitrate() int
	// Comment returns the comment, or an empty string if unavailable.
	Comment() string
	// Composer returns the composer of the track.
	Composer() string
	// Disc returns the disc number and total discs, or zero values if unavailable.
	Disc() (int, int)
	// Duration returns the length of the track as time.
	Duration() time.Duration
	// FileType returns the file type of the audio file.
	FileType() FileType
	// Format returns the metadata Format used to encode the data.
	Format() Format
	// Genre returns the genre of the track.
	Genre() string
	// Lyrics returns the lyrics, or an empty string if unavailable.
	Lyrics() string
	// Picture returns a picture, or nil if not available.
	Picture() *Picture
	// Raw returns the raw mapping of retrieved tag names and associated values.
	// NB: tag/atom names are not standardised between formats.
	Raw() map[string]interface{}
	// Title returns the title of the track.
	Title() string
	// Track returns the track number and total tracks, or zero values if unavailable.
	Track() (int, int)
	// Year returns the year of the track.
	Year() int
}

Metadata is an interface which is used to describe metadata retrieved by this package.

type Mp4Atom

type Mp4Atom struct {
	Name     string
	Size     uint32
	Data     []byte
	Children []Mp4Atom
}

type OggMetadata

type OggMetadata struct {
	// contains filtered or unexported fields
}

OggMetadata is a collection of metadata and other useful data from an Ogg container that contains Vorbis encoded audio

func ReadOggTags

func ReadOggTags(r io.ReadSeeker) (*OggMetadata, error)

ReadOggTags reads Ogg metadata from the io.ReadSeeker, returning the resulting metadata in a Metadata implementation, or non-nil error if there was a problem. See http://www.xiph.org/vorbis/doc/Vorbis_I_spec.html and http://www.xiph.org/ogg/doc/framing.html for details.

func (OggMetadata) Album

func (m OggMetadata) Album() string

func (OggMetadata) AlbumArtist

func (m OggMetadata) AlbumArtist() string

func (OggMetadata) Artist

func (m OggMetadata) Artist() string

func (OggMetadata) AverageBitrate

func (m OggMetadata) AverageBitrate() int

func (OggMetadata) Comment

func (m OggMetadata) Comment() string

func (OggMetadata) Composer

func (m OggMetadata) Composer() string

func (OggMetadata) Disc

func (m OggMetadata) Disc() (int, int)

func (OggMetadata) Duration

func (m OggMetadata) Duration() time.Duration

func (OggMetadata) FileType

func (m OggMetadata) FileType() FileType

func (OggMetadata) Format

func (m OggMetadata) Format() Format

func (OggMetadata) Genre

func (m OggMetadata) Genre() string

func (OggMetadata) Lyrics

func (m OggMetadata) Lyrics() string

func (OggMetadata) Picture

func (m OggMetadata) Picture() *Picture

Picture for OggMetadata always returns nil. There is no standard location for pictures in an Ogg container unless they are muxed into a separate stream, which this library does not handle.

func (OggMetadata) Title

func (m OggMetadata) Title() string

func (OggMetadata) TotalGranules

func (m OggMetadata) TotalGranules() int64

Returns the total number of granules in this Ogg container

func (OggMetadata) Track

func (m OggMetadata) Track() (int, int)

func (OggMetadata) VorbisComment

func (m OggMetadata) VorbisComment() map[string]string

VorbisComment returns the Vorbis comment information associated with this Ogg file.

func (OggMetadata) VorbisIDHeader

func (m OggMetadata) VorbisIDHeader() map[string]interface{}

VorbisIDHeader returns the Vorbis identification header information associated with this Ogg file. See the vorbisIDHeader struct type for more information.

func (OggMetadata) Year

func (m OggMetadata) Year() int

type Picture

type Picture struct {
	Ext         string // Extension of the picture file.
	MIMEType    string // MIMEType of the picture.
	Type        string // Type of the picture (see pictureTypes).
	Description string // Description.
	Data        []byte // Raw picture data.
}

Picture is a type which represents an attached picture extracted from metadata.

func (Picture) String

func (p Picture) String() string

String returns a string representation of the underlying Picture instance.

type UFID

type UFID struct {
	Provider   string
	Identifier []byte
}

UFID is composed of a provider (frequently a URL and a binary identifier) The identifier can be a text (Musicbrainz use texts, but not necessary)

func (UFID) String

func (u UFID) String() string

Directories

Path Synopsis
cmd
check
The check tool performs tag lookups on full music collections (iTunes or directory tree of files).
The check tool performs tag lookups on full music collections (iTunes or directory tree of files).
sum
The sum tool constructs a checksum of a media file exluding any metadata (as recognised by the tag library).
The sum tool constructs a checksum of a media file exluding any metadata (as recognised by the tag library).
tag
The tag tool reads metadata from media files (as supported by the tag library).
The tag tool reads metadata from media files (as supported by the tag library).
internal
Package mbz extracts MusicBrainz Picard-specific tags from general tag metadata.
Package mbz extracts MusicBrainz Picard-specific tags from general tag metadata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL