mirror of
1
Fork 0
gotosocial/vendor/github.com/abema/go-mp4
dependabot[bot] 5a4ceebcbd
[chore]: Bump github.com/abema/go-mp4 from 0.12.0 to 0.13.0 (#2113)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-16 16:10:01 +01:00
..
bitio [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
util [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
.gitignore [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
LICENSE [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
README.md [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
anytype.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
box.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
box_info.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
box_types_3gpp.go [chore]: Bump github.com/abema/go-mp4 from 0.11.0 to 0.12.0 (#2075) 2023-08-07 08:18:28 +00:00
box_types_av1.go [chore]: Bump github.com/abema/go-mp4 from 0.11.0 to 0.12.0 (#2075) 2023-08-07 08:18:28 +00:00
box_types_iso14496_12.go [chore]: Bump github.com/abema/go-mp4 from 0.11.0 to 0.12.0 (#2075) 2023-08-07 08:18:28 +00:00
box_types_iso14496_14.go [chore]: Bump github.com/abema/go-mp4 from 0.11.0 to 0.12.0 (#2075) 2023-08-07 08:18:28 +00:00
box_types_iso23001_7.go [chore]: Bump github.com/abema/go-mp4 from 0.11.0 to 0.12.0 (#2075) 2023-08-07 08:18:28 +00:00
box_types_metadata.go [chore]: Bump github.com/abema/go-mp4 from 0.11.0 to 0.12.0 (#2075) 2023-08-07 08:18:28 +00:00
box_types_opus.go [chore]: Bump github.com/abema/go-mp4 from 0.11.0 to 0.12.0 (#2075) 2023-08-07 08:18:28 +00:00
box_types_vp.go [chore]: Bump github.com/abema/go-mp4 from 0.12.0 to 0.13.0 (#2113) 2023-08-16 16:10:01 +01:00
extract.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
field.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
marshaller.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
mp4.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
probe.go [chore] update go-mp4 to latest commit (#2028) 2023-07-27 10:31:36 +02:00
read.go [chore]: Bump github.com/abema/go-mp4 from 0.10.0 to 0.10.1 (#1617) 2023-03-13 09:52:57 +01:00
string.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00
write.go [feature] Enable basic video support (mp4 only) (#1274) 2022-12-17 04:38:56 +00:00

README.md

go-mp4

Go Reference Test Coverage Status Go Report Card

go-mp4 is Go library for reading and writing MP4.

Integration with your Go application

Reading

You can parse MP4 file as follows:

// 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
})
// 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)
	}
})

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/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)