-
Notifications
You must be signed in to change notification settings - Fork 22
/
mp4.go
80 lines (74 loc) · 1.32 KB
/
mp4.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
package mp4
import "io"
// A MPEG-4 content
//
// A MPEG-4 media contains three main boxes :
//
// ftyp : the file type box
// moov : the movie box (meta-data)
// mdat : the media data (chunks and samples)
//
// Other boxes can also be present (pdin, moof, mfra, free, ...), but are not decoded.
type MP4 struct {
Ftyp *FtypBox
Moov *MoovBox
Mdat *MdatBox
}
func Decode(r io.Reader) (*MP4, error) {
h, err := DecodeHeader(r)
if err != nil {
return nil, err
}
if h.Type != "ftyp" {
return nil, ErrBadFormat
}
ftyp, err := DecodeBox(h, r)
if err != nil {
return nil, err
}
h, err = DecodeHeader(r)
if h.Type != "moov" {
return nil, ErrBadFormat
}
moov, err := DecodeBox(h, r)
if err != nil {
return nil, err
}
v := &MP4{
Ftyp: ftyp.(*FtypBox),
Moov: moov.(*MoovBox),
}
for {
h, err = DecodeHeader(r)
if err != nil {
break
}
if h.Type != "mdat" {
DecodeBox(h, r)
} else {
mdat, err := DecodeBox(h, r)
if err != nil {
return nil, err
}
v.Mdat = mdat.(*MdatBox)
v.Mdat.ContentSize = h.Size - BoxHeaderSize
break
}
}
return v, nil
}
func (m *MP4) Dump() {
m.Ftyp.Dump()
m.Moov.Dump()
}
func (m *MP4) Encode(w io.Writer) error {
err := m.Ftyp.Encode(w)
if err != nil {
return err
}
err = m.Moov.Encode(w)
if err != nil {
return err
}
return m.Mdat.Encode(w)
}