mp4

package module
v0.0.0-...-b5bc081 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2015 License: MIT Imports: 7 Imported by: 11

README

mp4

wercker status

A encoder/decoder class, io.Reader and io.Writer compatible, usable for HTTP pseudo streaming

For the complete MP4 specifications, see http://standards.iso.org/ittf/PubliclyAvailableStandards/c061988_ISO_IEC_14496-12_2012.zip and http://standards.iso.org/ittf/PubliclyAvailableStandards/c061989_ISO_IEC_15444-12_2012.zip

Doc

See http://godoc.org/github.com/jfbus/mp4 and http://godoc.org/github.com/jfbus/mp4/filter

Warning

Some boxes can have multiple formats (ctts, elst, tkhd, ...). Only the version 0 of those boxes is currently decoded (see https://github.com/jfbus/mp4/issues/7). Version 1 will be supported, and this will break a few things (e.g. some uint32 attributes will switch to uint64).

CLI

A CLI can be found in cli/mp4tool.go

It can :

  • Display info about a media
mp4tool info file.mp4
  • Copy a video (decode it and reencode it to another file, useful for debugging)
mp4tool copy in.mp4 out.mp4
  • Generate a clip
mp4tool clip --start 10 --duration 30 in.mp4 out.mp4

(if you really want to generate a clip, you should use ffmpeg, you will ge better results)

LICENSE

See LICENSE

Documentation

Overview

Package mp4 implements encoding/decoding of MP4 media.

Index

Constants

View Source
const (
	BoxHeaderSize = 8
)

Variables

View Source
var (
	ErrUnknownBoxType  = errors.New("unknown box type")
	ErrTruncatedHeader = errors.New("truncated header")
	ErrBadFormat       = errors.New("bad format")
)

Functions

func EncodeHeader

func EncodeHeader(b Box, w io.Writer) error

EncodeHeader encodes a box header to a writer

Types

type Box

type Box interface {
	Type() string
	Size() int
	Encode(w io.Writer) error
}

A box

func DecodeBox

func DecodeBox(h BoxHeader, r io.Reader) (Box, error)

DecodeBox decodes a box

func DecodeContainer

func DecodeContainer(r io.Reader) ([]Box, error)

DecodeContainer decodes a container box

func DecodeCtts

func DecodeCtts(r io.Reader) (Box, error)

func DecodeDinf

func DecodeDinf(r io.Reader) (Box, error)

func DecodeDref

func DecodeDref(r io.Reader) (Box, error)

func DecodeEdts

func DecodeEdts(r io.Reader) (Box, error)

func DecodeElst

func DecodeElst(r io.Reader) (Box, error)

func DecodeFree

func DecodeFree(r io.Reader) (Box, error)

func DecodeFtyp

func DecodeFtyp(r io.Reader) (Box, error)

func DecodeHdlr

func DecodeHdlr(r io.Reader) (Box, error)

func DecodeIods

func DecodeIods(r io.Reader) (Box, error)

func DecodeMdat

func DecodeMdat(r io.Reader) (Box, error)

func DecodeMdhd

func DecodeMdhd(r io.Reader) (Box, error)

func DecodeMdia

func DecodeMdia(r io.Reader) (Box, error)

func DecodeMeta

func DecodeMeta(r io.Reader) (Box, error)

func DecodeMinf

func DecodeMinf(r io.Reader) (Box, error)

func DecodeMoov

func DecodeMoov(r io.Reader) (Box, error)

func DecodeMvhd

func DecodeMvhd(r io.Reader) (Box, error)

func DecodeSmhd

func DecodeSmhd(r io.Reader) (Box, error)

func DecodeStbl

func DecodeStbl(r io.Reader) (Box, error)

func DecodeStco

func DecodeStco(r io.Reader) (Box, error)

func DecodeStsc

func DecodeStsc(r io.Reader) (Box, error)

func DecodeStsd

func DecodeStsd(r io.Reader) (Box, error)

func DecodeStss

func DecodeStss(r io.Reader) (Box, error)

func DecodeStsz

func DecodeStsz(r io.Reader) (Box, error)

func DecodeStts

func DecodeStts(r io.Reader) (Box, error)

func DecodeTkhd

func DecodeTkhd(r io.Reader) (Box, error)

func DecodeTrak

func DecodeTrak(r io.Reader) (Box, error)

func DecodeUdta

func DecodeUdta(r io.Reader) (Box, error)

func DecodeVmhd

func DecodeVmhd(r io.Reader) (Box, error)

type BoxDecoder

type BoxDecoder func(r io.Reader) (Box, error)

type BoxHeader

type BoxHeader struct {
	Type string
	Size uint32
}

The header of a box

func DecodeHeader

func DecodeHeader(r io.Reader) (BoxHeader, error)

DecodeHeader decodes a box header (size + box type)

type CttsBox

type CttsBox struct {
	Version      byte
	Flags        [3]byte
	SampleCount  []uint32
	SampleOffset []uint32 // int32 for version 1
}

Composition Time to Sample Box (ctts - optional)

Contained in: Sample Table Box (stbl)

Status: version 0 decoded. version 1 uses int32 for offsets

func (*CttsBox) Encode

func (b *CttsBox) Encode(w io.Writer) error

func (*CttsBox) Size

func (b *CttsBox) Size() int

func (*CttsBox) Type

func (b *CttsBox) Type() string

type DinfBox

type DinfBox struct {
	Dref *DrefBox
}

Data Information Box (dinf - mandatory)

Contained in : Media Information Box (minf) or Meta Box (meta)

Status : decoded

func (*DinfBox) Encode

func (b *DinfBox) Encode(w io.Writer) error

func (*DinfBox) Size

func (b *DinfBox) Size() int

func (*DinfBox) Type

func (b *DinfBox) Type() string

type DrefBox

type DrefBox struct {
	Version byte
	Flags   [3]byte
	// contains filtered or unexported fields
}

Data Reference Box (dref - mandatory)

Contained id: Data Information Box (dinf)

Status: not decoded

Defines the location of the media data. If the data for the track is located in the same file it contains nothing useful.

func (*DrefBox) Encode

func (b *DrefBox) Encode(w io.Writer) error

func (*DrefBox) Size

func (b *DrefBox) Size() int

func (*DrefBox) Type

func (b *DrefBox) Type() string

type EdtsBox

type EdtsBox struct {
	Elst *ElstBox
}

Edit Box (edts - optional)

Contained in: Track Box ("trak")

Status: decoded

The edit box maps the presentation timeline to the media-time line

func (*EdtsBox) Dump

func (b *EdtsBox) Dump()

func (*EdtsBox) Encode

func (b *EdtsBox) Encode(w io.Writer) error

func (*EdtsBox) Size

func (b *EdtsBox) Size() int

func (*EdtsBox) Type

func (b *EdtsBox) Type() string

type ElstBox

type ElstBox struct {
	Version                             byte
	Flags                               [3]byte
	SegmentDuration, MediaTime          []uint32 // should be uint32/int32 for version 0 and uint64/int32 for version 1
	MediaRateInteger, MediaRateFraction []uint16 // should be int16
}

Edit List Box (elst - optional)

Contained in : Edit Box (edts)

Status: version 0 decoded. version 1 not supported

func (*ElstBox) Dump

func (b *ElstBox) Dump()

func (*ElstBox) Encode

func (b *ElstBox) Encode(w io.Writer) error

func (*ElstBox) Size

func (b *ElstBox) Size() int

func (*ElstBox) Type

func (b *ElstBox) Type() string

type Fixed16

type Fixed16 uint16

An 8.8 fixed point number

func (Fixed16) String

func (f Fixed16) String() string

type Fixed32

type Fixed32 uint32

A 16.16 fixed point number

func (Fixed32) String

func (f Fixed32) String() string

type FreeBox

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

File Type Box (ftyp - mandatory)

Status: decoded

func (*FreeBox) Encode

func (b *FreeBox) Encode(w io.Writer) error

func (*FreeBox) Size

func (b *FreeBox) Size() int

func (*FreeBox) Type

func (b *FreeBox) Type() string

type FtypBox

type FtypBox struct {
	MajorBrand       string
	MinorVersion     []byte
	CompatibleBrands []string
}

File Type Box (ftyp - mandatory)

Status: decoded

func (*FtypBox) Dump

func (b *FtypBox) Dump()

func (*FtypBox) Encode

func (b *FtypBox) Encode(w io.Writer) error

func (*FtypBox) Size

func (b *FtypBox) Size() int

func (*FtypBox) Type

func (b *FtypBox) Type() string

type HdlrBox

type HdlrBox struct {
	Version     byte
	Flags       [3]byte
	PreDefined  uint32
	HandlerType string
	Name        string
}

Handler Reference Box (hdlr - mandatory)

Contained in: Media Box (mdia) or Meta Box (meta)

Status: decoded

This box describes the type of data contained in the trak.

HandlerType can be : "vide" (video track), "soun" (audio track), "hint" (hint track), "meta" (timed Metadata track), "auxv" (auxiliary video track).

func (*HdlrBox) Encode

func (b *HdlrBox) Encode(w io.Writer) error

func (*HdlrBox) Size

func (b *HdlrBox) Size() int

func (*HdlrBox) Type

func (b *HdlrBox) Type() string

type IodsBox

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

Object Descriptor Container Box (iods - optional)

Contained in : Movie Box (‘moov’)

Status: not decoded

func (*IodsBox) Encode

func (b *IodsBox) Encode(w io.Writer) error

func (*IodsBox) Size

func (b *IodsBox) Size() int

func (*IodsBox) Type

func (b *IodsBox) Type() string

type MP4

type MP4 struct {
	Ftyp *FtypBox
	Moov *MoovBox
	Mdat *MdatBox
	// contains filtered or unexported fields
}

A MPEG-4 media

A MPEG-4 media contains three main boxes :

ftyp : the file type box
moov : the movie box (meta-data)
mdat : the media data (chunks and samples)

Other boxes can also be present (pdin, moof, mfra, free, ...), but are not decoded.

func Decode

func Decode(r io.Reader) (*MP4, error)

Decode decodes a media from a Reader

func (*MP4) Boxes

func (m *MP4) Boxes() []Box

Boxes lists the top-level boxes from a media

func (*MP4) Dump

func (m *MP4) Dump()

Dump displays some information about a media

func (*MP4) Encode

func (m *MP4) Encode(w io.Writer) error

Encode encodes a media to a Writer

type MdatBox

type MdatBox struct {
	ContentSize uint32
	// contains filtered or unexported fields
}

Media Data Box (mdat - optional)

Status: not decoded

The mdat box contains media chunks/samples.

It is not read, only the io.Reader is stored, and will be used to Encode (io.Copy) the box to a io.Writer.

func (*MdatBox) Encode

func (b *MdatBox) Encode(w io.Writer) error

func (*MdatBox) Reader

func (b *MdatBox) Reader() io.Reader

func (*MdatBox) Size

func (b *MdatBox) Size() int

func (*MdatBox) Type

func (b *MdatBox) Type() string

type MdhdBox

type MdhdBox struct {
	Version          byte
	Flags            [3]byte
	CreationTime     uint32
	ModificationTime uint32
	Timescale        uint32
	Duration         uint32
	Language         uint16
}

Media Header Box (mdhd - mandatory)

Contained in : Media Box (mdia)

Status : only version 0 is decoded. version 1 is not supported

Timescale defines the timescale used for tracks. Language is a ISO-639-2/T language code stored as 1bit padding + [3]int5

func (*MdhdBox) Dump

func (b *MdhdBox) Dump()

func (*MdhdBox) Encode

func (b *MdhdBox) Encode(w io.Writer) error

func (*MdhdBox) Size

func (b *MdhdBox) Size() int

func (*MdhdBox) Type

func (b *MdhdBox) Type() string

type MdiaBox

type MdiaBox struct {
	Mdhd *MdhdBox
	Hdlr *HdlrBox
	Minf *MinfBox
}

Media Box (mdia - mandatory)

Contained in : Track Box (trak)

Status: decoded

Contains all information about the media data.

func (*MdiaBox) Dump

func (b *MdiaBox) Dump()

func (*MdiaBox) Encode

func (b *MdiaBox) Encode(w io.Writer) error

func (*MdiaBox) Size

func (b *MdiaBox) Size() int

func (*MdiaBox) Type

func (b *MdiaBox) Type() string

type MetaBox

type MetaBox struct {
	Version byte
	Flags   [3]byte
	// contains filtered or unexported fields
}

Meta Box (meta - optional)

Status: not decoded

func (*MetaBox) Encode

func (b *MetaBox) Encode(w io.Writer) error

func (*MetaBox) Size

func (b *MetaBox) Size() int

func (*MetaBox) Type

func (b *MetaBox) Type() string

type MinfBox

type MinfBox struct {
	Vmhd *VmhdBox
	Smhd *SmhdBox
	Stbl *StblBox
	Dinf *DinfBox
	Hdlr *HdlrBox
}

Media Information Box (minf - mandatory)

Contained in : Media Box (mdia)

Status: partially decoded (hmhd - hint tracks - and nmhd - null media - are ignored)

func (*MinfBox) Dump

func (b *MinfBox) Dump()

func (*MinfBox) Encode

func (b *MinfBox) Encode(w io.Writer) error

func (*MinfBox) Size

func (b *MinfBox) Size() int

func (*MinfBox) Type

func (b *MinfBox) Type() string

type MoovBox

type MoovBox struct {
	Mvhd *MvhdBox
	Iods *IodsBox
	Trak []*TrakBox
	Udta *UdtaBox
}

Movie Box (moov - mandatory)

Status: partially decoded (anything other than mvhd, iods, trak or udta is ignored)

Contains all meta-data. To be able to stream a file, the moov box should be placed before the mdat box.

func (*MoovBox) Dump

func (b *MoovBox) Dump()

func (*MoovBox) Encode

func (b *MoovBox) Encode(w io.Writer) error

func (*MoovBox) Size

func (b *MoovBox) Size() int

func (*MoovBox) Type

func (b *MoovBox) Type() string

type MvhdBox

type MvhdBox struct {
	Version          byte
	Flags            [3]byte
	CreationTime     uint32
	ModificationTime uint32
	Timescale        uint32
	Duration         uint32
	NextTrackId      uint32
	Rate             Fixed32
	Volume           Fixed16
	// contains filtered or unexported fields
}

Movie Header Box (mvhd - mandatory)

Contained in : Movie Box (‘moov’)

Status: version 0 is partially decoded. version 1 is not supported

Contains all media information (duration, ...).

Duration is measured in "time units", and timescale defines the number of time units per second.

Only version 0 is decoded.

func (*MvhdBox) Dump

func (b *MvhdBox) Dump()

func (*MvhdBox) Encode

func (b *MvhdBox) Encode(w io.Writer) error

func (*MvhdBox) Size

func (b *MvhdBox) Size() int

func (*MvhdBox) Type

func (b *MvhdBox) Type() string

type SmhdBox

type SmhdBox struct {
	Version byte
	Flags   [3]byte
	Balance uint16 // should be int16
}

Sound Media Header Box (smhd - mandatory for sound tracks)

Contained in : Media Information Box (minf)

Status: decoded

func (*SmhdBox) Encode

func (b *SmhdBox) Encode(w io.Writer) error

func (*SmhdBox) Size

func (b *SmhdBox) Size() int

func (*SmhdBox) Type

func (b *SmhdBox) Type() string

type StblBox

type StblBox struct {
	Stsd *StsdBox
	Stts *SttsBox
	Stss *StssBox
	Stsc *StscBox
	Stsz *StszBox
	Stco *StcoBox
	Ctts *CttsBox
}

Sample Table Box (stbl - mandatory)

Contained in : Media Information Box (minf)

Status: partially decoded (anything other than stsd, stts, stsc, stss, stsz, stco, ctts is ignored)

The table contains all information relevant to data samples (times, chunks, sizes, ...)

func (*StblBox) Dump

func (b *StblBox) Dump()

func (*StblBox) Encode

func (b *StblBox) Encode(w io.Writer) error

func (*StblBox) Size

func (b *StblBox) Size() int

func (*StblBox) Type

func (b *StblBox) Type() string

type StcoBox

type StcoBox struct {
	Version     byte
	Flags       [3]byte
	ChunkOffset []uint32
}

Chunk Offset Box (stco - mandatory)

Contained in : Sample Table box (stbl)

Status: decoded

This is the 32bits version of the box, the 64bits version (co64) is not decoded.

The table contains the offsets (starting at the beginning of the file) for each chunk of data for the current track. A chunk contains samples, the table defining the allocation of samples to each chunk is stsc.

func (*StcoBox) Dump

func (b *StcoBox) Dump()

func (*StcoBox) Encode

func (b *StcoBox) Encode(w io.Writer) error

func (*StcoBox) Size

func (b *StcoBox) Size() int

func (*StcoBox) Type

func (b *StcoBox) Type() string

type StscBox

type StscBox struct {
	Version             byte
	Flags               [3]byte
	FirstChunk          []uint32
	SamplesPerChunk     []uint32
	SampleDescriptionID []uint32
}

Sample To Chunk Box (stsc - mandatory)

Contained in : Sample Table box (stbl)

Status: decoded

A chunk contains samples. This table defines to which chunk a sample is associated. Each entry is defined by :

  • first chunk : all chunks starting at this index up to the next first chunk have the same sample count/description
  • samples per chunk : number of samples in the chunk
  • description id : description (see the sample description box - stsd)

func (*StscBox) Dump

func (b *StscBox) Dump()

func (*StscBox) Encode

func (b *StscBox) Encode(w io.Writer) error

func (*StscBox) Size

func (b *StscBox) Size() int

func (*StscBox) Type

func (b *StscBox) Type() string

type StsdBox

type StsdBox struct {
	Version byte
	Flags   [3]byte
	// contains filtered or unexported fields
}

Sample Description Box (stsd - manatory)

Contained in : Sample Table box (stbl)

Status: not decoded

This box contains information that describes how the data can be decoded.

func (*StsdBox) Encode

func (b *StsdBox) Encode(w io.Writer) error

func (*StsdBox) Size

func (b *StsdBox) Size() int

func (*StsdBox) Type

func (b *StsdBox) Type() string

type StssBox

type StssBox struct {
	Version      byte
	Flags        [3]byte
	SampleNumber []uint32
}

Sync Sample Box (stss - optional)

Contained in : Sample Table box (stbl)

Status: decoded

This lists all sync samples (key frames for video tracks) in the data. If absent, all samples are sync samples.

func (*StssBox) Dump

func (b *StssBox) Dump()

func (*StssBox) Encode

func (b *StssBox) Encode(w io.Writer) error

func (*StssBox) Size

func (b *StssBox) Size() int

func (*StssBox) Type

func (b *StssBox) Type() string

type StszBox

type StszBox struct {
	Version           byte
	Flags             [3]byte
	SampleUniformSize uint32
	SampleNumber      uint32
	SampleSize        []uint32
}

Sample Size Box (stsz - mandatory)

Contained in : Sample Table box (stbl)

Status : decoded

For each track, either stsz of the more compact stz2 must be present. stz2 variant is not supported.

This table lists the size of each sample. If all samples have the same size, it can be defined in the SampleUniformSize attribute.

func (*StszBox) Dump

func (b *StszBox) Dump()

func (*StszBox) Encode

func (b *StszBox) Encode(w io.Writer) error

func (*StszBox) GetSampleSize

func (b *StszBox) GetSampleSize(i int) uint32

GetSampleSize returns the size (in bytes) of a sample

func (*StszBox) Size

func (b *StszBox) Size() int

func (*StszBox) Type

func (b *StszBox) Type() string

type SttsBox

type SttsBox struct {
	Version         byte
	Flags           [3]byte
	SampleCount     []uint32
	SampleTimeDelta []uint32
}

Decoding Time to Sample Box (stts - mandatory)

Contained in : Sample Table box (stbl)

Status: decoded

This table contains the duration in time units for each sample.

  • sample count : the number of consecutive samples having the same duration
  • time delta : duration in time units

func (*SttsBox) Dump

func (b *SttsBox) Dump()

func (*SttsBox) Encode

func (b *SttsBox) Encode(w io.Writer) error

func (*SttsBox) GetTimeCode

func (b *SttsBox) GetTimeCode(sample, timescale uint32) time.Duration

GetTimeCode returns the timecode (duration since the beginning of the media) of the beginning of a sample

func (*SttsBox) Size

func (b *SttsBox) Size() int

func (*SttsBox) Type

func (b *SttsBox) Type() string

type TkhdBox

type TkhdBox struct {
	Version          byte
	Flags            [3]byte
	CreationTime     uint32
	ModificationTime uint32
	TrackId          uint32
	Duration         uint32
	Layer            uint16
	AlternateGroup   uint16 // should be int16
	Volume           Fixed16
	Matrix           []byte
	Width, Height    Fixed32
}

Track Header Box (tkhd - mandatory)

Status : only version 0 is decoded. version 1 is not supported

This box describes the track. Duration is measured in time units (according to the time scale defined in the movie header box).

Volume (relevant for audio tracks) is a fixed point number (8 bits + 8 bits). Full volume is 1.0. Width and Height (relevant for video tracks) are fixed point numbers (16 bits + 16 bits). Video pixels are not necessarily square.

func (*TkhdBox) Dump

func (b *TkhdBox) Dump()

func (*TkhdBox) Encode

func (b *TkhdBox) Encode(w io.Writer) error

func (*TkhdBox) Size

func (b *TkhdBox) Size() int

func (*TkhdBox) Type

func (b *TkhdBox) Type() string

type TrakBox

type TrakBox struct {
	Tkhd *TkhdBox
	Mdia *MdiaBox
	Edts *EdtsBox
}

Track Box (tkhd - mandatory)

Contained in : Movie Box (moov)

A media file can contain one or more tracks.

func (*TrakBox) Dump

func (b *TrakBox) Dump()

func (*TrakBox) Encode

func (b *TrakBox) Encode(w io.Writer) error

func (*TrakBox) Size

func (b *TrakBox) Size() int

func (*TrakBox) Type

func (b *TrakBox) Type() string

type UdtaBox

type UdtaBox struct {
	Meta *MetaBox
}

User Data Box (udta - optional)

Contained in: Movie Box (moov) or Track Box (trak)

func (*UdtaBox) Encode

func (b *UdtaBox) Encode(w io.Writer) error

func (*UdtaBox) Size

func (b *UdtaBox) Size() int

func (*UdtaBox) Type

func (b *UdtaBox) Type() string

type VmhdBox

type VmhdBox struct {
	Version      byte
	Flags        [3]byte
	GraphicsMode uint16
	OpColor      [3]uint16
}

Video Media Header Box (vhmd - mandatory for video tracks)

Contained in : Media Information Box (minf)

Status: decoded

func (*VmhdBox) Encode

func (b *VmhdBox) Encode(w io.Writer) error

func (*VmhdBox) Size

func (b *VmhdBox) Size() int

func (*VmhdBox) Type

func (b *VmhdBox) Type() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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