-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4.go
52 lines (48 loc) · 1.45 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
package audioduration
import (
"encoding/binary"
"io"
)
// The specification of MP4 file could be get here.
// https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html
// MP4 format has a hierarchy structure. The basic unit is Atom. Different type
// of Atom has different structure. And Atom could contain other types of Atom.
// The duration information could be accessed at:
// moov.trak.mdia.mdhd
// which structure is difined at:
// https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-SW34
// Mp4 Calculate mp4 files duration.
func Mp4(r io.ReadSeeker) (float64, error) {
var bufSize uint32 = 8
var err error = nil
var timeScale uint32 = 0
var duration uint32 = 0
mainloop:
for {
buf := make([]byte, bufSize)
_, err = io.ReadFull(r, buf)
if err != nil {
break
}
atomLen := binary.BigEndian.Uint32(buf[0:4])
atomType := string(buf[4:8])
switch atomType {
case "moov", "trak", "mdia", "minf", "stbl":
continue
case "mdhd":
r.Seek(12, io.SeekCurrent)
mdhdBuf := make([]byte, 8)
_, err = io.ReadFull(r, mdhdBuf)
if err != nil {
break
}
timeScale = binary.BigEndian.Uint32(mdhdBuf[0:4])
duration = binary.BigEndian.Uint32(mdhdBuf[4:8])
r.Seek(4, io.SeekCurrent)
break mainloop
default:
r.Seek(int64(atomLen-bufSize), io.SeekCurrent)
}
}
return float64(duration) / float64(timeScale), err
}