-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmp4_example_01.go
154 lines (134 loc) · 3.24 KB
/
mp4_example_01.go
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
package main
import (
"encoding/binary"
"fmt"
"io"
"os"
)
const (
// BoxHeaderSize Size of box header.
BoxHeaderSize = int64(8)
)
// FtypBox - File Type Box
// Box Type: ftyp
// Container: File
// Mandatory: Yes
// Quantity: Exactly one
type FtypBox struct {
*Box
MajorBrand string // Brand identifer.
MinorVersion uint32 // Informative integer for the minor version of the major brand.
CompatibleBrands []string // A list, to the end of the box, of brands.
}
func (b *FtypBox) parse() error {
data := b.ReadBoxData()
b.MajorBrand = string(data[0:4])
b.MinorVersion = binary.BigEndian.Uint32(data[4:8])
if len(data) > 8 {
for i := 8; i < len(data); i += 4 {
b.CompatibleBrands = append(b.CompatibleBrands, string(data[i:i+4]))
}
}
return nil
}
// Mp4Reader defines an mp4 reader structure.
type Mp4Reader struct {
Reader io.ReaderAt
Ftyp *FtypBox
Size int64
}
// Parse reads an MP4 reader for atom boxes.
func (m *Mp4Reader) Parse() error {
if m.Size == 0 {
if ofile, ok := m.Reader.(*os.File); ok {
info, err := ofile.Stat()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return err
}
m.Size = info.Size()
}
}
boxes := readBoxes(m, int64(0), m.Size)
for _, box := range boxes {
switch box.Name {
case "ftyp":
m.Ftyp = &FtypBox{Box: box}
m.Ftyp.parse()
// Add cases to check for more boxes here.
// case "moov":
// m.Moov = &MoovBox{Box: box}
// m.Moov.parse()
}
}
return nil
}
// ReadBoxAt reads a box from an offset.
func (m *Mp4Reader) ReadBoxAt(offset int64) (boxSize uint32, boxType string) {
buf := m.ReadBytesAt(BoxHeaderSize, offset)
boxSize = binary.BigEndian.Uint32(buf[0:4])
boxType = string(buf[4:8])
return boxSize, boxType
}
// ReadBytesAt reads a box at n and offset.
func (m *Mp4Reader) ReadBytesAt(n int64, offset int64) (word []byte) {
buf := make([]byte, n)
if _, error := m.Reader.ReadAt(buf, offset); error != nil {
fmt.Println(error)
return
}
return buf
}
// Box defines an Atom Box structure.
type Box struct {
Name string
Size, Start int64
Reader *Mp4Reader
}
// ReadBoxData reads the box data from an atom box.
func (b *Box) ReadBoxData() []byte {
if b.Size <= BoxHeaderSize {
return nil
}
return b.Reader.ReadBytesAt(b.Size-BoxHeaderSize, b.Start+BoxHeaderSize)
}
func readBoxes(m *Mp4Reader, start int64, n int64) (l []*Box) {
for offset := start; offset < start+n; {
size, name := m.ReadBoxAt(offset)
b := &Box{
Name: string(name),
Size: int64(size),
Reader: m,
Start: offset,
}
l = append(l, b)
offset += int64(size)
}
return l
}
// Open opens a file and returns an &Mp4Reader{}.
func Open(path string) (f *Mp4Reader, err error) {
file, err := os.Open(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, err
}
f = &Mp4Reader{
Reader: file,
}
return f, f.Parse()
}
func main() {
if len(os.Args) < 2 {
fmt.Println("missing argument, provide an mp4 file!")
return
}
mp4, err := Open(os.Args[1])
if err != nil {
fmt.Println("unable to open file")
}
fmt.Println("ftyp.name: ", mp4.Ftyp.Name)
fmt.Println("ftyp.major_brand: ", mp4.Ftyp.MajorBrand)
fmt.Println("ftyp.minor_version: ", mp4.Ftyp.MinorVersion)
fmt.Println("ftyp.compatible_brands: ", mp4.Ftyp.CompatibleBrands)
}