mp4

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mp4 decodes the sound of MP4 files for package audio: .m4a and .mp4, whole or cut into fragments as a stream for DASH is (.m4s). It reads the container, and a codec registered with RegisterCodec decodes its packets. Apple Lossless (ALAC) is built in; import package audio/aac for AAC, and audio/flac for FLAC in MP4:

import (
	_ "github.com/HyperparameterSI/golet/backend/audio/aac"  // AAC, in MP4 too
	_ "github.com/HyperparameterSI/golet/backend/audio/flac" // FLAC, in MP4 too
)

Each imports this package, which registers the format with package audio.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMovie = errors.New("mp4: no movie header")

ErrNoMovie says an MP4 has no movie header: no tracks to play.

Functions

func NewDecoder

func NewDecoder(r io.ReadSeeker) (audio.Decoder, error)

NewDecoder returns a decoder of the first sound track of the MP4 file r holds, which decodes to stereo. It reads the movie header, and the packets as they are played, so a file still coming in plays as it comes when r waits for the bytes asked of it: a fragmented one, a fragment at a time, with its length from its segment index. One with no index is read through for its length.

func RegisterCodec

func RegisterCodec(fourcc string, open func(config []byte) (Codec, error))

RegisterCodec registers the codec of tracks whose sample entry is of type fourcc, as "mp4a": open returns a decoder of its packets from the track's configuration (see Track's Config). A package for a codec calls it in its init function.

Types

type Codec

type Codec interface {
	// SampleRate returns the frames per second it decodes to.
	SampleRate() int
	// Decode appends the sound of a packet to dst, as interleaved left
	// and right samples in [-1, 1], and returns it. A mono track plays in
	// both; of more channels, the front left and right play.
	Decode(dst []float32, packet []byte) ([]float32, error)
	// Reset forgets the packets decoded before, as after a seek.
	Reset()
	// Preroll returns how many packets before the one holding a frame
	// sought to are decoded first, for codecs whose packets overlap the
	// sound of the one before, as AAC's (1); 0 when packets stand alone.
	Preroll() int
	// PacketFrames returns how many frames every packet decodes to, when
	// all do, as AAC's 1024: packets are then placed in time by counting
	// them, whatever the file says they last. 0 places them as the file
	// says, for codecs whose packets differ.
	PacketFrames() int
}

A Codec decodes the packets of a track, one at a time, to stereo.

type Track

type Track struct {
	// Codec is the type of the track's sample entry: "mp4a" (AAC),
	// "alac", "fLaC", "Opus", ...
	Codec string
	// Config configures the codec: AAC's AudioSpecificConfig, ALAC's
	// magic cookie, FLAC's metadata blocks, or another codec's boxes
	// within the sample entry.
	Config []byte
	// Duration is how long it plays, without the encoder's priming, or 0
	// for a fragmented file whose length its header doesn't say.
	Duration time.Duration
	// Fragmented says its packets are in fragments after the movie header.
	Fragmented bool
}

Track describes an MP4's sound track, as its movie header says, for an app to know of a file without decoding it, as a music library scanning files does.

func ReadTrack

func ReadTrack(r io.ReaderAt, size int64) (*Track, error)

ReadTrack reads the first sound track of the MP4 file r holds, of size bytes, from its movie header.

func (*Track) Decodable

func (t *Track) Decodable() bool

Decodable says whether a codec registered decodes the track: HE-AAC, for one, package aac does not.

Jump to

Keyboard shortcuts

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