-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmvhd.go
82 lines (75 loc) · 2.18 KB
/
mvhd.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
package mp4
import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"time"
)
// Movie Header Box (mvhd - mandatory)
//
// Contained in : Movie Box (‘moov’)
//
// Status: version 0 is partially decoded. version 1 is not supported
//
// Contains all media information (duration, ...).
//
// Duration is measured in "time units", and timescale defines the number of time units per second.
//
// Only version 0 is decoded.
type MvhdBox struct {
Version byte
Flags [3]byte
CreationTime uint32
ModificationTime uint32
Timescale uint32
Duration uint32
NextTrackId uint32
Rate Fixed32
Volume Fixed16
notDecoded []byte
}
func DecodeMvhd(r io.Reader) (Box, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return &MvhdBox{
Version: data[0],
Flags: [3]byte{data[1], data[2], data[3]},
CreationTime: binary.BigEndian.Uint32(data[4:8]),
ModificationTime: binary.BigEndian.Uint32(data[8:12]),
Timescale: binary.BigEndian.Uint32(data[12:16]),
Duration: binary.BigEndian.Uint32(data[16:20]),
Rate: fixed32(data[20:24]),
Volume: fixed16(data[24:26]),
notDecoded: data[26:],
}, nil
}
func (b *MvhdBox) Type() string {
return "mvhd"
}
func (b *MvhdBox) Size() int {
return BoxHeaderSize + 26 + len(b.notDecoded)
}
func (b *MvhdBox) Dump() {
fmt.Printf("Movie Header:\n Timescale: %d units/sec\n Duration: %d units (%s)\n Rate: %s\n Volume: %s\n", b.Timescale, b.Duration, time.Duration(b.Duration/b.Timescale)*time.Second, b.Rate, b.Volume)
}
func (b *MvhdBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
buf := makebuf(b)
buf[0] = b.Version
buf[1], buf[2], buf[3] = b.Flags[0], b.Flags[1], b.Flags[2]
binary.BigEndian.PutUint32(buf[4:], b.CreationTime)
binary.BigEndian.PutUint32(buf[8:], b.ModificationTime)
binary.BigEndian.PutUint32(buf[12:], b.Timescale)
binary.BigEndian.PutUint32(buf[16:], b.Duration)
binary.BigEndian.PutUint32(buf[20:], uint32(b.Rate))
binary.BigEndian.PutUint16(buf[24:], uint16(b.Volume))
copy(buf[26:], b.notDecoded)
_, err = w.Write(buf)
return err
}