-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmp4.go
62 lines (56 loc) · 1.33 KB
/
gmp4.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
// This package was written based on ISO/IEC 14496
// Gain video time,only supported mp4 now
package gmp4
import (
"bytes"
"encoding/binary"
"log"
)
type mvhdBox struct {
metadata []byte
metadataIndex int
totalTimeSecond uint32
done bool
}
type video interface {
collectData()
getMvhdBox() mvhdBox
}
// Translate byte to int
func byteSliceToInt(raw []byte) (result uint32) {
if len(raw) != 4 {
log.Panicln("gmp4.go:data is invalid")
return 0
}
buf := bytes.NewBuffer(raw)
err := binary.Read(buf, binary.BigEndian, &result)
if err != nil {
log.Panicln("gmp4.go:data read failed!")
return 0
}
return
}
// check find the index of mvhd box
func check(raw []byte) (int, string) {
for i := 0; i < len(raw); i++ {
//lmvhd stand for mvhd box
if string(raw[i:i+5]) == "lmvhd" {
return i, "mvhd"
} else if string(raw[i:i+3]) == "dat" {
return i, "mdat"
}
}
return 0, ""
}
// GetDuration get video time(second)
// by downloading mvhd box of video and parse it
func GetDuration(m video) uint32 {
m.collectData()
data := m.getMvhdBox()
if data.done {
return data.totalTimeSecond
}
data.totalTimeSecond = byteSliceToInt(data.metadata[data.metadataIndex+21:data.metadataIndex+25]) / byteSliceToInt(data.metadata[data.metadataIndex+17:data.metadataIndex+21])
data.done = true
return data.totalTimeSecond
}