README.md (3746B)
1 go-mp4 2 ------ 3 4 [![Go Reference](https://pkg.go.dev/badge/github.com/abema/go-mp4.svg)](https://pkg.go.dev/github.com/abema/go-mp4) 5 ![Test](https://github.com/abema/go-mp4/actions/workflows/test.yml/badge.svg) 6 [![Coverage Status](https://coveralls.io/repos/github/abema/go-mp4/badge.svg)](https://coveralls.io/github/abema/go-mp4) 7 [![Go Report Card](https://goreportcard.com/badge/github.com/abema/go-mp4)](https://goreportcard.com/report/github.com/abema/go-mp4) 8 9 go-mp4 is Go library for reading and writing MP4. 10 11 ## Integration with your Go application 12 13 ### Reading 14 15 You can parse MP4 file as follows: 16 17 ```go 18 // expand all boxes 19 _, err := mp4.ReadBoxStructure(file, func(h *mp4.ReadHandle) (interface{}, error) { 20 fmt.Println("depth", len(h.Path)) 21 22 // Box Type (e.g. "mdhd", "tfdt", "mdat") 23 fmt.Println("type", h.BoxInfo.Type.String()) 24 25 // Box Size 26 fmt.Println("size", h.BoxInfo.Size) 27 28 if h.BoxInfo.IsSupportedType() { 29 // Payload 30 box, _, err := h.ReadPayload() 31 if err != nil { 32 return nil, err 33 } 34 str, err := mp4.Stringify(box, h.BoxInfo.Context) 35 if err != nil { 36 return nil, err 37 } 38 fmt.Println("payload", str) 39 40 // Expands children 41 return h.Expand() 42 } 43 return nil, nil 44 }) 45 ``` 46 47 ```go 48 // extract specific boxes 49 boxes, err := mp4.ExtractBoxWithPayload(file, nil, mp4.BoxPath{mp4.BoxTypeMoov(), mp4.BoxTypeTrak(), mp4.BoxTypeTkhd()}) 50 if err != nil { 51 : 52 } 53 for _, box := range boxes { 54 tkhd := box.Payload.(*mp4.Tkhd) 55 fmt.Println("track ID:", tkhd.TrackID) 56 } 57 ``` 58 59 ```go 60 // get basic informations 61 info, err := mp4.Probe(bufseekio.NewReadSeeker(file, 1024, 4)) 62 if err != nil { 63 : 64 } 65 fmt.Println("track num:", len(info.Tracks)) 66 ``` 67 68 ### Writing 69 70 Writer helps you to write box tree. 71 The following sample code edits emsg box and writes to another file. 72 73 ```go 74 r := bufseekio.NewReadSeeker(inputFile, 128*1024, 4) 75 w := mp4.NewWriter(outputFile) 76 _, err = mp4.ReadBoxStructure(r, func(h *mp4.ReadHandle) (interface{}, error) { 77 switch h.BoxInfo.Type { 78 case mp4.BoxTypeEmsg(): 79 // write box size and box type 80 _, err := w.StartBox(&h.BoxInfo) 81 if err != nil { 82 return nil, err 83 } 84 // read payload 85 box, _, err := h.ReadPayload() 86 if err != nil { 87 return nil, err 88 } 89 // update MessageData 90 emsg := box.(*mp4.Emsg) 91 emsg.MessageData = []byte("hello world") 92 // write box playload 93 if _, err := mp4.Marshal(w, emsg, h.BoxInfo.Context); err != nil { 94 return nil, err 95 } 96 // rewrite box size 97 _, err = w.EndBox() 98 return nil, err 99 default: 100 // copy all 101 return nil, w.CopyBox(r, &h.BoxInfo) 102 } 103 }) 104 ``` 105 106 ### User-defined Boxes 107 108 You can create additional box definition as follows: 109 110 ```go 111 func BoxTypeXxxx() BoxType { return mp4.StrToBoxType("xxxx") } 112 113 func init() { 114 mp4.AddBoxDef(&Xxxx{}, 0) 115 } 116 117 type Xxxx struct { 118 FullBox `mp4:"0,extend"` 119 UI32 uint32 `mp4:"1,size=32"` 120 ByteArray []byte `mp4:"2,size=8,len=dynamic"` 121 } 122 123 func (*Xxxx) GetType() BoxType { 124 return BoxTypeXxxx() 125 } 126 ``` 127 128 ### Buffering 129 130 go-mp4 has no buffering feature for I/O. 131 If you should reduce Read function calls, you can wrap the io.ReadSeeker by [bufseekio](https://github.com/sunfish-shogi/bufseekio). 132 133 ## Command Line Tool 134 135 Install mp4tool as follows: 136 137 ```sh 138 go install github.com/abema/go-mp4/mp4tool@latest 139 140 mp4tool -help 141 ``` 142 143 For example, `mp4tool dump MP4_FILE_NAME` command prints MP4 box tree as follows: 144 145 ``` 146 [moof] Size=504 147 [mfhd] Size=16 Version=0 Flags=0x000000 SequenceNumber=1 148 [traf] Size=480 149 [tfhd] Size=28 Version=0 Flags=0x020038 TrackID=1 DefaultSampleDuration=9000 DefaultSampleSize=33550 DefaultSampleFlags=0x1010000 150 [tfdt] Size=20 Version=1 Flags=0x000000 BaseMediaDecodeTimeV1=0 151 [trun] Size=424 ... (use -a option to show all) 152 [mdat] Size=44569 Data=[...] (use -mdat option to expand) 153 ```