-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathmain.go
208 lines (192 loc) · 5.27 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"encoding/hex"
"fmt"
"os"
"path"
"github.com/Eyevinn/mp4ff/aac"
"github.com/Eyevinn/mp4ff/bits"
"github.com/Eyevinn/mp4ff/mp4"
)
const (
avcSPSnalu = "67640020accac05005bb0169e0000003002000000c9c4c000432380008647c12401cb1c31380"
avcPPSnalu = "68b5df20"
hevcVPSnalu = "40010c01ffff022000000300b0000003000003007b18b024"
hevcSPSnalu = "420101022000000300b0000003000003007ba0078200887db6718b92448053888892cf24a69272c9124922dc91aa48fca223ff000100016a02020201"
hevcPPSnalu = "4401c0252f053240"
)
func main() {
if err := run("."); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
}
}
func run(outDir string) error {
err := writeVideoAVCInitSegment(path.Join(outDir, "video_avc_init.cmfv"))
if err != nil {
return err
}
err = writeVideoHEVCInitSegment(path.Join(outDir, "video_hevc_init.cmfv"))
if err != nil {
return err
}
err = writeAudioAACInitSegment(path.Join(outDir, "audio_aac_init.cmfa"))
if err != nil {
return err
}
err = writeAudioAC3InitSegment(path.Join(outDir, "audio_ac3_init.cmfa"))
if err != nil {
return err
}
err = writeAudioEC3InitSegment(path.Join(outDir, "audio_ec3_init.cmfa"))
if err != nil {
return err
}
err = writeSubtitlesWvttInitSegment(path.Join(outDir, "subtitles_wvtt_init.cmft"))
if err != nil {
return err
}
err = writeSubtitlesStppInitSegment(path.Join(outDir, "subtitles_stpp_init.cmft"))
if err != nil {
return err
}
return nil
}
func writeVideoAVCInitSegment(outPath string) error {
sps, _ := hex.DecodeString(avcSPSnalu)
spsNALUs := [][]byte{sps}
pps, _ := hex.DecodeString(avcPPSnalu)
ppsNALUs := [][]byte{pps}
videoTimescale := uint32(180000)
init := mp4.CreateEmptyInit()
init.AddEmptyTrack(videoTimescale, "video", "und")
trak := init.Moov.Trak
includePS := true
err := trak.SetAVCDescriptor("avc1", spsNALUs, ppsNALUs, includePS)
if err != nil {
return err
}
width := trak.Mdia.Minf.Stbl.Stsd.AvcX.Width
height := trak.Mdia.Minf.Stbl.Stsd.AvcX.Height
if width != 1280 || height != 720 {
return fmt.Errorf("got %dx%d instead of 1280x720", width, height)
}
err = writeToFile(init, outPath)
return err
}
func writeVideoHEVCInitSegment(outPath string) error {
vps, _ := hex.DecodeString(hevcVPSnalu)
vpsNALUs := [][]byte{vps}
sps, _ := hex.DecodeString(hevcSPSnalu)
spsNALUs := [][]byte{sps}
pps, _ := hex.DecodeString(hevcPPSnalu)
ppsNALUs := [][]byte{pps}
videoTimescale := uint32(180000)
init := mp4.CreateEmptyInit()
init.AddEmptyTrack(videoTimescale, "video", "und")
trak := init.Moov.Trak
err := trak.SetHEVCDescriptor("hvc1", vpsNALUs, spsNALUs, ppsNALUs, nil, true)
if err != nil {
return err
}
width := trak.Mdia.Minf.Stbl.Stsd.HvcX.Width
height := trak.Mdia.Minf.Stbl.Stsd.HvcX.Height
if width != 960 || height != 540 {
return fmt.Errorf("got %dx%d instead of 960x540", width, height)
}
err = writeToFile(init, outPath)
return err
}
func writeAudioAACInitSegment(outPath string) error {
audioTimeScale := 48000
init := mp4.CreateEmptyInit()
init.AddEmptyTrack(uint32(audioTimeScale), "audio", "en")
trak := init.Moov.Trak
err := trak.SetAACDescriptor(aac.AAClc, audioTimeScale)
if err != nil {
return err
}
err = writeToFile(init, outPath)
return err
}
func writeAudioAC3InitSegment(outPath string) error {
dac3Hex := "0000000b646163330c3dc0"
dac3Bytes, err := hex.DecodeString(dac3Hex)
if err != nil {
return err
}
sr := bits.NewFixedSliceReader(dac3Bytes)
box, err := mp4.DecodeBoxSR(0, sr)
if err != nil {
return err
}
dac3 := box.(*mp4.Dac3Box)
init := mp4.CreateEmptyInit()
samplingRate := mp4.AC3SampleRates[dac3.FSCod]
init.AddEmptyTrack(uint32(samplingRate), "audio", "en")
trak := init.Moov.Trak
err = trak.SetAC3Descriptor(dac3)
if err != nil {
return err
}
err = writeToFile(init, outPath)
return err
}
func writeAudioEC3InitSegment(outPath string) error {
dec3Hex := "0000000e646563330c00200f0202"
dec3Bytes, err := hex.DecodeString(dec3Hex)
if err != nil {
return err
}
sr := bits.NewFixedSliceReader(dec3Bytes)
box, err := mp4.DecodeBoxSR(0, sr)
if err != nil {
return err
}
dec3 := box.(*mp4.Dec3Box)
init := mp4.CreateEmptyInit()
samplingRate := mp4.AC3SampleRates[dec3.EC3Subs[0].FSCod]
init.AddEmptyTrack(uint32(samplingRate), "audio", "en")
trak := init.Moov.Trak
err = trak.SetEC3Descriptor(dec3)
if err != nil {
return err
}
err = writeToFile(init, outPath)
return err
}
func writeSubtitlesWvttInitSegment(outPath string) error {
subtitleTimescale := 1000
init := mp4.CreateEmptyInit()
init.AddEmptyTrack(uint32(subtitleTimescale), "wvtt", "en")
trak := init.Moov.Trak
err := trak.SetWvttDescriptor("WEBVTT")
if err != nil {
return err
}
err = writeToFile(init, outPath)
return err
}
func writeSubtitlesStppInitSegment(outPath string) error {
subtitleTimescale := 1000
init := mp4.CreateEmptyInit()
init.AddEmptyTrack(uint32(subtitleTimescale), "stpp", "en")
trak := init.Moov.Trak
schemaLocation := ""
auxiliaryMimeType := ""
err := trak.SetStppDescriptor("http://www.w3.org/ns/ttml", schemaLocation, auxiliaryMimeType)
if err != nil {
return err
}
err = writeToFile(init, outPath)
return err
}
func writeToFile(init *mp4.InitSegment, filePath string) error {
// Next write to a file
ofd, err := os.Create(filePath)
if err != nil {
return err
}
defer ofd.Close()
err = init.Encode(ofd)
return err
}