Categorygithub.com/abema/go-mp4
modulepackage
1.4.1
Repository: https://github.com/abema/go-mp4.git
Documentation: pkg.go.dev

# README

go-mp4

Go Reference Test Coverage Status Go Report Card

go-mp4 is Go library which provides low-level I/O interfaces of MP4. This library supports you to parse or build any MP4 boxes(atoms) directly.

go-mp4 provides very flexible interfaces for reading boxes. If you want to read only specific parts of MP4 file, this library extracts those boxes via io.ReadSeeker interface.

On the other hand, this library is not suitable for complex data conversions.

Integration with your Go application

Reading

Using ReadBoxStructure or ReadBoxStructureFromInternal, you can scan box(atom) tree by depth-first order.

// expand all boxes
_, err := mp4.ReadBoxStructure(file, func(h *mp4.ReadHandle) (interface{}, error) {
	fmt.Println("depth", len(h.Path))

	// Box Type (e.g. "mdhd", "tfdt", "mdat")
	fmt.Println("type", h.BoxInfo.Type.String())

	// Box Size
	fmt.Println("size", h.BoxInfo.Size)

	if h.BoxInfo.IsSupportedType() {
		// Payload
		box, _, err := h.ReadPayload()
		if err != nil {
			return nil, err
		}
		str, err := mp4.Stringify(box, h.BoxInfo.Context)
		if err != nil {
			return nil, err
		}
		fmt.Println("payload", str)

		// Expands children
		return h.Expand()
	}
	return nil, nil
})

ExtractBox, ExtractBoxes, ExtractBoxWithPayload, ExtractBoxesWithPayload, and Probe are wrapper functions of ReadBoxStructure.

// extract specific boxes
boxes, err := mp4.ExtractBoxWithPayload(file, nil, mp4.BoxPath{mp4.BoxTypeMoov(), mp4.BoxTypeTrak(), mp4.BoxTypeTkhd()})
if err != nil {
   :
}
for _, box := range boxes {
  tkhd := box.Payload.(*mp4.Tkhd)
  fmt.Println("track ID:", tkhd.TrackID)
}
// get basic informations
info, err := mp4.Probe(bufseekio.NewReadSeeker(file, 1024, 4))  
if err != nil {
   :
}
fmt.Println("track num:", len(info.Tracks))

Writing

Writer helps you to write box tree. The following sample code edits emsg box and writes to another file.

r := bufseekio.NewReadSeeker(inputFile, 128*1024, 4)
w := mp4.NewWriter(outputFile)
_, err = mp4.ReadBoxStructure(r, func(h *mp4.ReadHandle) (interface{}, error) {
	switch h.BoxInfo.Type {
	case mp4.BoxTypeEmsg():
		// write box size and box type
		_, err := w.StartBox(&h.BoxInfo)
		if err != nil {
			return nil, err
		}
		// read payload
		box, _, err := h.ReadPayload()
		if err != nil {
			return nil, err
		}
		// update MessageData
		emsg := box.(*mp4.Emsg)
		emsg.MessageData = []byte("hello world")
		// write box playload
		if _, err := mp4.Marshal(w, emsg, h.BoxInfo.Context); err != nil {
			return nil, err
		}
		// rewrite box size
		_, err = w.EndBox()
		return nil, err
	default:
		// copy all
		return nil, w.CopyBox(r, &h.BoxInfo)
	}
})

Please note that the above sample code doesn't work for some MP4 files. If your MP4 files include specific box types (ex. stco, mfra), you should update those when offsets of mdat box changed. Next sample code adds an metadata box and updates chunk offsets in stco boxes.

Sample Code: Insert Metadata and Update stco box

User-defined Boxes

You can create additional box definition as follows:

func BoxTypeXxxx() BoxType { return mp4.StrToBoxType("xxxx") }

func init() {
	mp4.AddBoxDef(&Xxxx{}, 0)
}

type Xxxx struct {
	FullBox  `mp4:"0,extend"`
	UI32      uint32 `mp4:"1,size=32"`
	ByteArray []byte `mp4:"2,size=8,len=dynamic"`
}

func (*Xxxx) GetType() BoxType {
	return BoxTypeXxxx()
}

Buffering

go-mp4 has no buffering feature for I/O. If you should reduce Read function calls, you can wrap the io.ReadSeeker by bufseekio.

Command Line Tool

Install mp4tool as follows:

go install github.com/abema/go-mp4/cmd/mp4tool@latest

mp4tool -help

For example, mp4tool dump MP4_FILE_NAME command prints MP4 box tree as follows:

[moof] Size=504
  [mfhd] Size=16 Version=0 Flags=0x000000 SequenceNumber=1
  [traf] Size=480
    [tfhd] Size=28 Version=0 Flags=0x020038 TrackID=1 DefaultSampleDuration=9000 DefaultSampleSize=33550 DefaultSampleFlags=0x1010000
    [tfdt] Size=20 Version=1 Flags=0x000000 BaseMediaDecodeTimeV1=0
    [trun] Size=424 ... (use -a option to show all)
[mdat] Size=44569 Data=[...] (use -mdat option to expand)

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Probe probes MP4 file.
ProbeFra probes fragmented MP4 file Deprecated: replace with Probe.
ReadBoxInfo reads common fields which are defined as "Box" class member at ISO/IEC 14496-12.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Uint32ToBoxType returns a new BoxType from the provied uint32.
No description provided by the author
No description provided by the author
WriteBoxInfo writes common fields which are defined as "Box" class member at ISO/IEC 14496-12.

# Constants

0x42.
0x58.
0x6e.
0x7a.
0x64.
0x4d.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
BoxInfo has common infomations of box.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Data is a Value BoxType https://developer.apple.com/documentation/quicktime-file-format/value_atom.
No description provided by the author
No description provided by the author
Dinf is ISOBMFF dinf box type.
No description provided by the author
Dref is ISOBMFF dref box type.
No description provided by the author
Edts is ISOBMFF edts box type.
Elst is ISOBMFF elst box type.
No description provided by the author
Emsg is ISOBMFF emsg box type.
No description provided by the author
Esds is ES descripter box.
No description provided by the author
No description provided by the author
Frma is ISOBMFF frma box type.
Ftyp is ISOBMFF ftyp box type.
FullBox is ISOBMFF FullBox.
Hdlr is ISOBMFF hdlr box type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Item is a numbered item under an item list atom https://developer.apple.com/documentation/quicktime-file-format/metadata_item_list_atom/item_list.
Key is a key value field in the Keys BoxType https://developer.apple.com/documentation/quicktime-file-format/metadata_item_keys_atom/key_value_key_size-8.
Keys is the Keys BoxType https://developer.apple.com/documentation/quicktime-file-format/metadata_item_keys_atom.
Mdat is ISOBMFF mdat box type.
Mdhd is ISOBMFF mdhd box type.
Mdia is ISOBMFF mdia box type.
Mehd is ISOBMFF mehd box type.
Meta is ISOBMFF meta box type.
Mfhd is ISOBMFF mfhd box type.
Mfra is ISOBMFF mfra box type.
Mfro is ISOBMFF mfro box type.
Minf is ISOBMFF minf box type.
Moof is ISOBMFF moof box type.
Moov is ISOBMFF moov box type.
No description provided by the author
Mvex is ISOBMFF mvex box type.
Mvhd is ISOBMFF mvhd box type.
No description provided by the author
No description provided by the author
No description provided by the author
Pssh is ISOBMFF pssh box type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Stbl is ISOBMFF stbl box type.
Stco is ISOBMFF stco box type.
No description provided by the author
Stsc is ISOBMFF stsc box type.
No description provided by the author
Stsd is ISOBMFF stsd box type.
No description provided by the author
Stsz is ISOBMFF stsz box type.
Stts is ISOBMFF stts box type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Tenc is ISOBMFF tenc box type.
No description provided by the author
Tfdt is ISOBMFF tfdt box type.
Tfhd is ISOBMFF tfhd box type.
Tfra is ISOBMFF tfra box type.
No description provided by the author
Tkhd is ISOBMFF tkhd box type.
No description provided by the author
Traf is ISOBMFF traf box type.
Trak is ISOBMFF trak box type.
Trep is ISOBMFF trep box type.
Trex is ISOBMFF trex box type.
Trun is ISOBMFF trun box type.
No description provided by the author
Udta is ISOBMFF udta box type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Vmhd is ISOBMFF vmhd box type.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Wave is QuickTime wave box.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
IBox is common interface of box.
No description provided by the author
IImmutableBox is common interface of box.

# Type aliases

No description provided by the author
BoxType is mpeg box type.
No description provided by the author
No description provided by the author
No description provided by the author
Deprecated: replace with ProbeInfo.
No description provided by the author
No description provided by the author
No description provided by the author
Deprecated: replace with Segment.
No description provided by the author
No description provided by the author
Deprecated: replace with Track.
No description provided by the author