forked from godeep/mp4
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mp4.go
81 lines (75 loc) · 1.44 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
81
package mp4
import "io"
// A MPEG-4 media
//
// 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
boxes []Box
}
// Decode decodes a media from a Reader
func Decode(r io.Reader) (*MP4, error) {
v := &MP4{
boxes: []Box{},
}
LoopBoxes:
for {
h, err := DecodeHeader(r)
if err != nil {
return nil, err
}
box, err := DecodeBox(h, r)
if err != nil {
return nil, err
}
v.boxes = append(v.boxes, box)
switch h.Type {
case "ftyp":
v.Ftyp = box.(*FtypBox)
case "moov":
v.Moov = box.(*MoovBox)
case "mdat":
v.Mdat = box.(*MdatBox)
v.Mdat.ContentSize = h.Size - BoxHeaderSize
break LoopBoxes
}
}
return v, nil
}
// Dump displays some information about a media
func (m *MP4) Dump() {
m.Ftyp.Dump()
m.Moov.Dump()
}
// Boxes lists the top-level boxes from a media
func (m *MP4) Boxes() []Box {
return m.boxes
}
// Encode encodes a media to a Writer
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
}
for _, b := range m.boxes {
if b.Type() != "ftyp" && b.Type() != "moov" {
err = b.Encode(w)
if err != nil {
return err
}
}
}
return nil
}