mpeg

package module
v0.0.0-...-0047c90 Latest Latest
Warning

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

Go to latest
Published: May 28, 2023 License: BSD-3-Clause Imports: 13 Imported by: 1

README

mpeg

GoDoc Build Status

This repository contains a Go module for reading information from MPEG (and particularly MP3) audio files.

It was originally written as part of nup and is exercised by tests in that repository.

Documentation

Overview

Package mpeg returns information about MPEG (and specifically MP3) audio files.

Index

Constants

View Source
const ID3v1Length = 128

ID3v1Length is the length in bytes of an ID3v1 tag.

Variables

This section is empty.

Functions

func ComputeAudioSHA1

func ComputeAudioSHA1(f *os.File, fi os.FileInfo, headerLen, footerLen int64) (string, error)

ComputeAudioSHA1 returns a SHA1 hash of the audio (i.e. non-metadata) portion of f.

func GetID3v2TextFrame

func GetID3v2TextFrame(gen taglib.GenericTag, id string) (string, error)

GetID3v2TextFrame returns the first ID3v2 text frame with the supplied ID from gen. If the frame isn't present, an empty string and nil error are returned.

The taglib library has built-in support for some frames ("TPE1", "TIT2", "TALB", etc.) and provides generic support for custom "TXXX" frames, but it doesn't seem to provide an easy way to read other well-known frames like "TPE2".

func GetID3v2UniqueFileIdentifiers

func GetID3v2UniqueFileIdentifiers(gen taglib.GenericTag) (map[string]string, error)

GetID3v2UniqueFileIdentifiers returns unique file IDs from gen keyed by URLs of database owners, e.g. "http://musicbrainz.org". Errors are ignored.

Types

type EncodingMethod

type EncodingMethod int

EncodingMethod describes the encoding method used for the file.

const (
	UnknownMethod EncodingMethod = 0
	CBR           EncodingMethod = 1
	ABR           EncodingMethod = 2
	VBR1          EncodingMethod = 3 // Lame: VBR old / VBR RH
	VBR2          EncodingMethod = 4 // Lame: VBR MTRH
	VBR3          EncodingMethod = 5 // LAME: VBR MT
	VBR4          EncodingMethod = 6
	CBR2Pass      EncodingMethod = 8
	ABR2Pass      EncodingMethod = 9
)

func (EncodingMethod) String

func (m EncodingMethod) String() string

type FrameInfo

type FrameInfo struct {
	KbitRate        int // in 1000 bits per second (not 1024)
	SampleRate      int // in hertz
	SamplesPerFrame int
	ChannelMode     uint8 // 0x0 stereo, 0x1 joint stereo, 0x2 dual channel, 0x3 single channel
	HasCRC          bool  // 16-bit CRC follows header
	HasPadding      bool  // frame is padded with one extra bit
}

FrameInfo contains information about an MPEG (MP3?) audio frame header.

func ReadFrameInfo

func ReadFrameInfo(f *os.File, start int64) (*FrameInfo, error)

ReadFrameInfo reads an MPEG audio frame header at the specified offset in f. Format details at http://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header.

func (*FrameInfo) Empty

func (fi *FrameInfo) Empty() bool

func (*FrameInfo) Size

func (fi *FrameInfo) Size() int64

type ID3v1Tag

type ID3v1Tag struct {
	Title, Artist, Album, Year, Comment string
	Genre, Track                        byte
}

ID3v1Tag contains information from an ID3v1 footer at the end of an MP3 file. ID3v1 is a terrible format: https://id3.org/ID3v1

func ReadID3v1Footer

func ReadID3v1Footer(f *os.File, fi os.FileInfo) (*ID3v1Tag, error)

ReadID3v1Footer reads an ID3v1 footer from the final ID3v1Length bytes of f. If the tag isn't present, the returned tag and error will be nil.

type Time

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

Time contains a UTC timestamp (including date) read from an MP3 ID3v2 tag. Individual components of the timestamp may be unset.

func GetID3v2Time

func GetID3v2Time(tag taglib.GenericTag, typ TimeType) (Time, error)

GetID3v2Time returns the requested timestamp from tag. ID3 v2.4 frames are preferred before falling back to v2.3 frames. If the requested timestamp time is not set, an empty time is returned. A non-nil error is only returned if an error was encountered while reading the tag.

func ParseID3v23Time

func ParseID3v23Time(yearStr, dateStr, timeStr string) Time

ParseID3v23Time parses the supplied ID3 v2.3 timestamp as split across YYYY, DDMM, and HHMM strings. See "4.2.1. Text information frames - details" in https://id3.org/id3v2.3.0. Empty or invalid strings are omitted from the returned Time object. If none of the strings can be parsed, an empty Time is returned.

func ParseID3v24Time

func ParseID3v24Time(str string) Time

ParseID3v24Time parses the supplied ID3 v2.4 variable-precision timestamp, e.g. "2021", "2021-04-10", or "2021-04-10T15:06:47". See "4. ID3v2 frame overview" in https://id3.org/id3v2.4.0-structure. If the timestamp is empty or invalid, an empty Time object is returned.

func (*Time) Day

func (t *Time) Day() int

func (*Time) Empty

func (t *Time) Empty() bool

Empty returns true if none of the components in t are set.

func (*Time) Hour

func (t *Time) Hour() int

func (*Time) Minute

func (t *Time) Minute() int

func (*Time) Month

func (t *Time) Month() int

func (*Time) Second

func (t *Time) Second() int

func (*Time) String

func (t *Time) String() string

func (*Time) Time

func (t *Time) Time() time.Time

Time returns t's underlying time.Time object. Components that were unset in the original timestamp will have default values.

func (*Time) Year

func (t *Time) Year() int

These functions return different components of t. If the corresponding component is unset, -1 is returned.

type TimeType

type TimeType int

TimeType describes a timestamp stored in an ID3v2 tag.

const (
	RecordingTime TimeType = iota
	OriginalReleaseTime
	ReleaseTime
)

type VBRHeaderID

type VBRHeaderID string

VBRHeaderID describes the type of header used to fill a VBRInfo.

const (
	// XingID typically indicates a VBR or ABR stream.
	XingID VBRHeaderID = "Xing"
	// InfoID typically indicates a CBR stream.
	InfoID VBRHeaderID = "Info"
)

type VBRInfo

type VBRInfo struct {
	// ID contains the ID from the beginning of the header.
	ID VBRHeaderID
	// Frames contains the number of audio frames in the file.
	Frames uint32
	// Bytes contains the number of bytes of audio data in the file.
	Bytes uint32
	// Quality contains a poorly-defined quality indicator in the range [0, 100].
	Quality int
	// Encoder describes the encoder version, e.g. "LAME3.90a".
	Encoder string
	// Method describes how the audio was encoded.
	Method EncodingMethod
}

VBRInfo contains information from an Xing (or Info) header in the first frame. See https://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header#XINGHeader.

func ComputeAudioDuration

func ComputeAudioDuration(f *os.File, fi os.FileInfo, headerLen, footerLen int64) (time.Duration, *VBRInfo, error)

ComputeAudioDuration reads an Xing header from the frame at headerLen in f to return the audio length. If no Xing header is present, it assumes that the file has a constant bitrate and returns a nil VBRInfo struct. Only supports MPEG Audio 1, Layer 3. TODO: Consider adding support for VBRI headers, apparently only written by the Fraunhofer encoder: https://www.codeproject.com/Articles/8295/MPEG-Audio-Frame-Header#VBRIHeader

Jump to

Keyboard shortcuts

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