-
Notifications
You must be signed in to change notification settings - Fork 22
/
moov.go
92 lines (85 loc) · 1.48 KB
/
moov.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
package mp4
import (
"fmt"
"io"
)
// Movie Box (moov - mandatory)
//
// Status: partially decoded (anything other than mvhd, iods, trak or udta is ignored)
//
// Contains all meta-data. To be able to stream a file, the moov box should be placed before the mdat box.
type MoovBox struct {
Mvhd *MvhdBox
Iods *IodsBox
Trak []*TrakBox
Udta *UdtaBox
}
func DecodeMoov(r io.Reader) (Box, error) {
l, err := DecodeContainer(r)
if err != nil {
return nil, err
}
m := &MoovBox{}
for _, b := range l {
switch b.Type() {
case "mvhd":
m.Mvhd = b.(*MvhdBox)
case "iods":
m.Iods = b.(*IodsBox)
case "trak":
m.Trak = append(m.Trak, b.(*TrakBox))
case "udta":
m.Udta = b.(*UdtaBox)
}
}
return m, err
}
func (b *MoovBox) Type() string {
return "moov"
}
func (b *MoovBox) Size() int {
sz := b.Mvhd.Size()
if b.Iods != nil {
sz += b.Iods.Size()
}
for _, t := range b.Trak {
sz += t.Size()
}
if b.Udta != nil {
sz += b.Udta.Size()
}
return sz + BoxHeaderSize
}
func (b *MoovBox) Dump() {
b.Mvhd.Dump()
for i, t := range b.Trak {
fmt.Println("Track", i)
t.Dump()
}
}
func (b *MoovBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
err = b.Mvhd.Encode(w)
if err != nil {
return err
}
if b.Iods != nil {
err = b.Iods.Encode(w)
if err != nil {
return err
}
}
for _, t := range b.Trak {
err = t.Encode(w)
if err != nil {
return err
}
}
if b.Udta != nil {
return b.Udta.Encode(w)
}
return nil
}