aboutsummaryrefslogtreecommitdiff
path: root/cmd/mp4dump/main.go
blob: 0d42d186868649db4fe9fb0403501eb7fe859a22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Command mp4dump reads an MP4 file and prints its box structure.
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/tetsuo/mp4"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, "usage: %s <file.mp4>\n", os.Args[0])
		os.Exit(1)
	}

	f, err := os.Open(os.Args[1])
	if err != nil {
		fmt.Fprintf(os.Stderr, "error opening file: %v\n", err)
		os.Exit(1)
	}
	defer f.Close()

	sc := mp4.NewScanner(f)
	for sc.Next() {
		e := sc.Entry()
		fmt.Printf("[%s] size=%d\n", e.Type, e.Size)

		// Only load metadata boxes into memory for deep parsing
		if e.Type == mp4.TypeMoov || e.Type == mp4.TypeMoof {
			buf := make([]byte, e.DataSize())
			if err := sc.ReadBody(buf); err != nil {
				fmt.Fprintf(os.Stderr, "error reading %s: %v\n", e.Type, err)
				continue
			}
			r := mp4.NewReader(buf)
			walk(&r, 1)
		} else if e.Type == mp4.TypeFtyp {
			buf := make([]byte, e.DataSize())
			if err := sc.ReadBody(buf); err != nil {
				fmt.Fprintf(os.Stderr, "error reading ftyp: %v\n", err)
				continue
			}
			f := mp4.ReadFtyp(buf)
			fmt.Printf("  brand=%s ver=%d", string(f.MajorBrand[:]), f.MinorVersion)
			if len(f.Compatible) > 0 {
				fmt.Printf(" compat=[")
				for i, c := range f.Compatible {
					if i > 0 {
						fmt.Printf(",")
					}
					fmt.Printf("%s", string(c[:]))
				}
				fmt.Printf("]")
			}
			fmt.Println()
		} else if e.Type == mp4.TypeMdat {
			fmt.Printf("  dataLen=%d\n", e.DataSize())
		}
	}
	if err := sc.Err(); err != nil {
		fmt.Fprintf(os.Stderr, "scan error: %v\n", err)
		os.Exit(1)
	}
}

func walk(r *mp4.Reader, depth int) {
	for r.Next() {
		indent := strings.Repeat("  ", depth)

		fmt.Printf("%s[%s] size=%d", indent, r.Type(), r.Size())

		if mp4.IsFullBox(r.Type()) {
			fmt.Printf(" v=%d flags=0x%06x", r.Version(), r.Flags())
		}

		printBoxInfo(r)
		fmt.Println()

		// Descend into containers
		if mp4.IsContainerBox(r.Type()) {
			r.Enter()
			walk(r, depth+1)
			r.Exit()
			continue
		}

		// Handle stsd: entry count + sub-boxes
		if r.Type() == mp4.TypeStsd {
			r.Enter()
			r.Skip(4) // skip entry count
			for r.Next() {
				printSampleEntry(r, depth+1)
			}
			r.Exit()
			continue
		}
	}
}

func printSampleEntry(r *mp4.Reader, depth int) {
	indent := strings.Repeat("  ", depth)

	switch r.Type() {
	case mp4.TypeAvc1:
		v := mp4.ReadVisualSampleEntry(r.Data())
		fmt.Printf("%s[%s] size=%d %dx%d compressor=%q\n", indent, r.Type(), r.Size(), v.Width, v.Height, v.CompressorName)
		// Enter to find avcC and other children
		r.Enter()
		r.Skip(v.ChildOffset)
		for r.Next() {
			childIndent := strings.Repeat("  ", depth+1)
			if mp4.IsFullBox(r.Type()) {
				fmt.Printf("%s[%s] size=%d v=%d flags=0x%06x", childIndent, r.Type(), r.Size(), r.Version(), r.Flags())
			} else {
				fmt.Printf("%s[%s] size=%d", childIndent, r.Type(), r.Size())
			}
			if r.Type() == mp4.TypeAvcC {
				codec := mp4.ReadAvcC(r.Data())
				fmt.Printf(" codec=%s", codec)
			}
			fmt.Println()
		}
		r.Exit()

	case mp4.TypeMp4a:
		a := mp4.ReadAudioSampleEntry(r.Data())
		fmt.Printf("%s[%s] size=%d ch=%d sampleSize=%d sampleRate=%d\n", indent, r.Type(), r.Size(), a.ChannelCount, a.SampleSize, a.SampleRate>>16)
		// Enter to find esds and other children
		r.Enter()
		r.Skip(a.ChildOffset)
		for r.Next() {
			childIndent := strings.Repeat("  ", depth+1)
			if mp4.IsFullBox(r.Type()) {
				fmt.Printf("%s[%s] size=%d v=%d flags=0x%06x", childIndent, r.Type(), r.Size(), r.Version(), r.Flags())
			} else {
				fmt.Printf("%s[%s] size=%d", childIndent, r.Type(), r.Size())
			}
			if r.Type() == mp4.TypeEsds {
				codec := mp4.ReadEsdsCodec(r.Data())
				fmt.Printf(" codec=%s", codec)
			}
			fmt.Println()
		}
		r.Exit()

	default:
		fmt.Printf("%s[%s] size=%d", indent, r.Type(), r.Size())
		if mp4.IsFullBox(r.Type()) {
			fmt.Printf(" v=%d flags=0x%06x", r.Version(), r.Flags())
		}
		fmt.Printf(" (raw %d bytes)\n", len(r.Data()))
	}
}

func printBoxInfo(r *mp4.Reader) {
	switch r.Type() {
	case mp4.TypeFtyp:
		f := mp4.ReadFtyp(r.Data())
		fmt.Printf(" brand=%s ver=%d", string(f.MajorBrand[:]), f.MinorVersion)
		if len(f.Compatible) > 0 {
			fmt.Printf(" compat=[")
			for i, c := range f.Compatible {
				if i > 0 {
					fmt.Printf(",")
				}
				fmt.Printf("%s", string(c[:]))
			}
			fmt.Printf("]")
		}

	case mp4.TypeMvhd:
		ts, dur, ntid := r.ReadMvhd()
		fmt.Printf(" timescale=%d duration=%d nextTrackId=%d", ts, dur, ntid)

	case mp4.TypeTkhd:
		tid, dur, w, h := r.ReadTkhd()
		fmt.Printf(" trackId=%d duration=%d size=%dx%d", tid, dur, w>>16, h>>16)

	case mp4.TypeMdhd:
		ts, dur, lang := r.ReadMdhd()
		fmt.Printf(" timescale=%d duration=%d lang=%d", ts, dur, lang)

	case mp4.TypeHdlr:
		ht := r.ReadHdlr()
		name := r.ReadHdlrName()
		fmt.Printf(" type=%s name=%q", string(ht[:]), name)

	case mp4.TypeStsd:
		if len(r.Data()) >= 4 {
			fmt.Printf(" entries=%d", r.EntryCount())
		}

	case mp4.TypeStsz:
		it := mp4.NewStszIter(r.Data())
		fmt.Printf(" entries=%d", it.Count())

	case mp4.TypeStco, mp4.TypeStss:
		it := mp4.NewUint32Iter(r.Data())
		fmt.Printf(" entries=%d", it.Count())

	case mp4.TypeCo64:
		it := mp4.NewCo64Iter(r.Data())
		fmt.Printf(" entries=%d", it.Count())

	case mp4.TypeStts:
		it := mp4.NewSttsIter(r.Data())
		fmt.Printf(" entries=%d", it.Count())

	case mp4.TypeCtts:
		it := mp4.NewCttsIter(r.Data())
		fmt.Printf(" entries=%d", it.Count())

	case mp4.TypeStsc:
		it := mp4.NewStscIter(r.Data())
		fmt.Printf(" entries=%d", it.Count())

	case mp4.TypeElst:
		it := mp4.NewElstIter(r.Data(), r.Version())
		fmt.Printf(" entries=%d", it.Count())

	case mp4.TypeDref:
		if len(r.Data()) >= 4 {
			fmt.Printf(" entries=%d", r.EntryCount())
		}

	case mp4.TypeMehd:
		dur := r.ReadMehd()
		fmt.Printf(" fragmentDuration=%d", dur)

	case mp4.TypeTrex:
		tid, _, _, _, _ := r.ReadTrex()
		fmt.Printf(" trackId=%d", tid)

	case mp4.TypeMfhd:
		seq := r.ReadMfhd()
		fmt.Printf(" seq=%d", seq)

	case mp4.TypeTfhd:
		tid := r.ReadTfhd()
		fmt.Printf(" trackId=%d", tid)

	case mp4.TypeTfdt:
		bt := r.ReadTfdt()
		fmt.Printf(" baseMediaDecodeTime=%d", bt)

	case mp4.TypeTrun:
		it := mp4.NewTrunIter(r.Data(), r.Flags())
		fmt.Printf(" entries=%d", it.Count())
		if r.Flags()&mp4.TrunDataOffsetPresent != 0 {
			fmt.Printf(" dataOffset=%d", it.DataOffset())
		}

	case mp4.TypeMdat:
		fmt.Printf(" dataLen=%d", len(r.Data()))

	case mp4.TypeVmhd:
		// graphicsMode and opcolor
	case mp4.TypeSmhd:
		// balance
	default:
		if !mp4.IsContainerBox(r.Type()) {
			if len(r.Data()) > 0 {
				fmt.Printf(" (%d bytes)", len(r.Data()))
			}
		}
	}
}