-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffmpeg.go
202 lines (164 loc) · 4.31 KB
/
ffmpeg.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
package main
import (
"errors"
"fmt"
"io"
"log"
gmf "github.com/3d0c/gmf"
)
func addStream(codecName string, oc *gmf.FmtCtx, ist *gmf.Stream) (int, int) {
var cc *gmf.CodecCtx
var ost *gmf.Stream
codec := assert(gmf.FindEncoder(codecName)).(*gmf.Codec)
// Create Video stream in output context
if ost = oc.NewStream(codec); ost == nil {
panic(errors.New("unable to create stream in output context"))
}
//defer gmf.Release(ost)
if cc = gmf.NewCodecCtx(codec); cc == nil {
panic(errors.New("unable to create codec context"))
}
//defer gmf.Release(cc)
if oc.IsGlobalHeader() {
cc.SetFlag(gmf.CODEC_FLAG_GLOBAL_HEADER)
}
if codec.IsExperimental() {
cc.SetStrictCompliance(gmf.FF_COMPLIANCE_EXPERIMENTAL)
}
cc.SetSampleFmt(gmf.AV_SAMPLE_FMT_S16P)
cc.SetSampleRate(ist.CodecCtx().SampleRate())
cc.SetChannels(ist.CodecCtx().Channels())
cc.SetChannelLayout(ist.CodecCtx().ChannelLayout())
if err := cc.Open(nil); err != nil {
panic(err)
}
ost.SetCodecCtx(cc)
ost.DumpContexCodec(cc)
return ist.Index(), ost.Index()
}
func transcode_audio(w io.Writer, r io.ReadCloser) {
var _, outputIndex int
// Input
inputCtx := gmf.NewCtx()
defer inputCtx.Free()
iavioCtx, err := NewSourceReader(r).GetAVIOContext(inputCtx)
if err != nil {
panic(err)
}
defer gmf.Release(iavioCtx)
inputCtx.SetPb(iavioCtx).OpenInput("")
// Output
ofmt := gmf.FindOutputFmt("", "output.mp3", "")
if ofmt == nil {
panic(fmt.Errorf("Failed to determine output format"))
}
defer ofmt.Free()
ofmt.Filename = ""
outputCtx, err := gmf.NewOutputCtx(ofmt, nil)
if err != nil {
panic(err)
}
defer outputCtx.Free()
oavioCtx, err := NewDestinationWriter(w).GetAVIOContext(outputCtx)
if err != nil {
panic(err)
}
defer gmf.Release(oavioCtx)
outputCtx.SetPb(oavioCtx)
// Audio stream from source file...
srcAudioStream, err := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_AUDIO)
if err != nil {
panic(fmt.Errorf("No audio stream found in input"))
} else {
_, outputIndex = addStream("libmp3lame", outputCtx, srcAudioStream)
}
inputCodecCtx := srcAudioStream.CodecCtx()
// Output audio stream...
ost := assert(outputCtx.GetStream(outputIndex)).(*gmf.Stream)
defer gmf.Release(ost.CodecCtx())
defer gmf.Release(ost)
// Resampler...
options := []*gmf.Option{
{Key: "in_channel_layout", Val: inputCodecCtx.ChannelLayout()},
{Key: "out_channel_layout", Val: inputCodecCtx.ChannelLayout()},
{Key: "in_sample_rate", Val: inputCodecCtx.SampleRate()},
{Key: "out_sample_rate", Val: ost.CodecCtx().SampleRate()},
{Key: "in_sample_fmt", Val: inputCodecCtx.SampleFmt()},
{Key: "out_sample_fmt", Val: ost.CodecCtx().SampleFmt()},
}
swrCtx, err := gmf.NewSwrCtx(options, ost.CodecCtx().Channels(), ost.CodecCtx().SampleFmt())
if err != nil {
panic(fmt.Errorf("new swr context error: %s", err.Error()))
}
if swrCtx == nil {
panic(fmt.Errorf("unable to create Swr Context"))
}
// Start writing to the output file...
if err := outputCtx.WriteHeader(); err != nil {
panic(err)
}
var (
packets int = 0
frames int = 0
encoded int = 0
)
for packet := range inputCtx.GetNewPackets() {
func() {
packets++
srcFrames, err := inputCodecCtx.Decode(packet)
defer packet.Free()
if err != nil {
log.Printf("capture audio error: %s", err)
return
}
for _, frame := range srcFrames {
frames++
func() {
dstFrame, err := swrCtx.Convert(frame)
if err != nil {
log.Printf("convert audio error: %s", err)
panic(err)
}
if dstFrame == nil {
return
}
defer frame.Free()
defer dstFrame.Free()
pkt, err := dstFrame.Encode(ost.CodecCtx())
if err != nil {
log.Println(err)
return
}
if pkt == nil {
return
}
defer pkt.Free()
pkt.SetStreamIndex(ost.Index())
if err := outputCtx.WritePacket(pkt); err != nil {
panic(err)
}
}()
}
encoded++
}()
}
// Drain the encoder...
for {
pkts, err := ost.CodecCtx().Encode(nil, 1)
if err != nil && err.Error() != "End of file" {
panic(err)
}
if len(pkts) <= 0 {
break
}
for _, pkt := range pkts {
pkt.SetStreamIndex(ost.Index())
if err := outputCtx.WritePacket(pkt); err != nil {
panic(err)
}
pkt.Free()
}
}
oavioCtx.Flush()
log.Printf("packets: %d, frames: %d, encoded: %d\n", packets, frames, encoded)
}