forked from godeep/mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4.go
83 lines (73 loc) · 1.46 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
82
83
package mp4
import (
"io"
"time"
)
// 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 {
Moov *MoovBox
Mdat *MdatBox
boxes []Box
}
// Decode decodes a media from a Reader
func Decode(r io.Reader) (*MP4, error) {
l, err := DecodeContainer(r)
if err != nil {
return nil, err
}
v := &MP4{
boxes: make([]Box, 0, len(l)),
}
for _, b := range l {
switch b.Type() {
case "moov":
v.Moov = b.(*MoovBox)
case "mdat":
v.Mdat = b.(*MdatBox)
default:
v.boxes = append(v.boxes, b)
}
}
return v, nil
}
// Dump displays some information about a media
func (m *MP4) 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) (err error) {
err = m.Moov.Encode(w)
if err != nil {
return err
}
for _, b := range m.boxes {
err = b.Encode(w)
if err != nil {
return err
}
}
return m.Mdat.Encode(w)
}
func (m *MP4) Size() (sz int) {
sz += m.Moov.Size()
sz += m.Mdat.Size()
for _, b := range m.Boxes() {
sz += b.Size()
}
return
}
func (m *MP4) Duration() time.Duration {
return time.Second * time.Duration(m.Moov.Mvhd.Duration) / time.Duration(m.Moov.Mvhd.Timescale)
}