-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4s-to-flv.go
269 lines (218 loc) · 5.27 KB
/
mp4s-to-flv.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"flag"
"fmt"
"io"
"log"
"strings"
"github.com/3d0c/gmf"
)
type arrayFlags []string
func (i *arrayFlags) String() string {
return strings.Join(*i, " ")
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, strings.TrimSpace(value))
return nil
}
func addStream(name string, oc *gmf.FmtCtx, ist *gmf.Stream) (int, int, error) {
var (
cc *gmf.CodecCtx
ost *gmf.Stream
options []gmf.Option
)
codec, err := gmf.FindEncoder(name)
if err != nil {
return 0, 0, err
}
if cc = gmf.NewCodecCtx(codec); cc == nil {
return 0, 0, fmt.Errorf("unable to create codec context")
}
if oc.IsGlobalHeader() {
cc.SetFlag(gmf.CODEC_FLAG_GLOBAL_HEADER)
}
if codec.IsExperimental() {
cc.SetStrictCompliance(gmf.FF_COMPLIANCE_EXPERIMENTAL)
}
if cc.Type() == gmf.AVMEDIA_TYPE_AUDIO {
options = append(
[]gmf.Option{
{Key: "time_base", Val: ist.CodecCtx().TimeBase().AVR()},
{Key: "ar", Val: ist.CodecCtx().SampleRate()},
{Key: "ac", Val: ist.CodecCtx().Channels()},
{Key: "channel_layout", Val: cc.SelectChannelLayout()},
},
)
cc.SetSampleFmt(ist.CodecCtx().SampleFmt())
cc.SelectSampleRate()
}
if cc.Type() == gmf.AVMEDIA_TYPE_VIDEO {
options = append(
[]gmf.Option{
{Key: "time_base", Val: gmf.AVR{Num: 1, Den: 25}},
{Key: "pixel_format", Val: gmf.AV_PIX_FMT_YUV420P},
// Save original
{Key: "video_size", Val: ist.CodecCtx().GetVideoSize()},
{Key: "b", Val: 500000},
},
)
cc.SetProfile(ist.CodecCtx().GetProfile())
}
cc.SetOptions(options)
if err := cc.Open(nil); err != nil {
return 0, 0, err
}
par := gmf.NewCodecParameters()
if err = par.FromContext(cc); err != nil {
return 0, 0, fmt.Errorf("error creating codec parameters from context - %s", err)
}
defer par.Free()
if ost = oc.NewStream(codec); ost == nil {
return 0, 0, fmt.Errorf("unable to create new stream in output context")
}
ost.CopyCodecPar(par)
ost.SetCodecCtx(cc)
if cc.Type() == gmf.AVMEDIA_TYPE_VIDEO {
ost.SetTimeBase(gmf.AVR{Num: 1, Den: 25})
ost.SetRFrameRate(gmf.AVR{Num: 25, Den: 1})
}
return ist.Index(), ost.Index(), nil
}
func main() {
var (
src arrayFlags
dst string
streamMap map[int]int = make(map[int]int)
pts int64 = 0
)
flag.Var(&src, "src", "source files, e.g.: -src=1.mp4 -src=2.mp4")
flag.StringVar(&dst, "dst", "", "destination file, e.g. -dst=result.flv")
flag.Parse()
if len(src) == 0 || dst == "" {
log.Fatal("at least one source and destination required")
}
octx, err := gmf.NewOutputCtx(dst)
if err != nil {
log.Fatal(err)
}
defer octx.Free()
for _, name := range src {
log.Printf("Processing file %s\n", name)
ictx, err := gmf.NewInputCtx(name)
if err != nil {
log.Fatal(err)
}
if len(streamMap) == 0 {
srcVideoStream, err := ictx.GetBestStream(gmf.AVMEDIA_TYPE_VIDEO)
if err != nil {
log.Fatalf("No video stream found in %s", name)
} else {
i, o, err := addStream("libx264", octx, srcVideoStream)
if err != nil {
log.Fatal(err)
}
streamMap[i] = o
}
srcAudioStream, err := ictx.GetBestStream(gmf.AVMEDIA_TYPE_AUDIO)
if err != nil {
log.Println("No audio stream found in", name)
} else {
i, o, err := addStream("aac", octx, srcAudioStream)
if err != nil {
log.Fatal(err)
}
streamMap[i] = o
}
if err := octx.WriteHeader(); err != nil {
log.Fatalf("error writing header - %s\n", err)
}
}
var (
pkt *gmf.Packet
ist, ost *gmf.Stream
streamIdx int
flush int = -1
)
for {
if flush < 0 {
pkt, err = ictx.GetNextPacket()
if err != nil && err != io.EOF {
if pkt != nil {
pkt.Free()
}
log.Fatalf("error getting next packet - %s", err)
} else if err != nil && pkt == nil {
log.Printf("=== flushing \n")
flush++
break
}
}
if flush == len(streamMap) {
break
}
if flush < 0 {
streamIdx = pkt.StreamIndex()
} else {
streamIdx = flush
flush++
}
if _, ok := streamMap[streamIdx]; !ok {
if pkt != nil {
pkt.Free()
}
continue
}
ist, err = ictx.GetStream(streamIdx)
if err != nil {
if pkt != nil {
pkt.Free()
}
log.Fatalf("error getting stream %d - %s", pkt.StreamIndex(), err)
}
ost, err = octx.GetStream(streamMap[ist.Index()])
if err != nil {
if pkt != nil {
pkt.Free()
}
log.Fatalf("error getting stream %d - %s", pkt.StreamIndex(), err)
}
frames, err := ist.CodecCtx().Decode(pkt)
if err != nil {
log.Fatalf("error decoding - %s\n", err)
}
for _, frame := range frames {
frame.SetPts(pts)
pts++
}
packets, err := ost.CodecCtx().Encode(frames, flush)
for _, op := range packets {
gmf.RescaleTs(op, ost.CodecCtx().TimeBase(), ost.TimeBase())
op.SetStreamIndex(ost.Index())
if err = octx.WritePacket(op); err != nil {
break
}
op.Free()
}
for _, frame := range frames {
if frame != nil {
frame.Free()
}
}
if pkt != nil {
pkt.Free()
}
}
for i := 0; i < ictx.StreamsCnt(); i++ {
st, _ := ictx.GetStream(i)
st.CodecCtx().Free()
st.Free()
}
ictx.Free()
}
octx.WriteTrailer()
for i := 0; i < octx.StreamsCnt(); i++ {
st, _ := octx.GetStream(i)
st.CodecCtx().Free()
st.Free()
}
}