-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathtranscode.go
179 lines (139 loc) · 3.84 KB
/
transcode.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
package main
import (
"errors"
"fmt"
"log"
"os"
"runtime/debug"
. "github.com/3d0c/gmf"
)
func fatal(err error) {
debug.PrintStack()
log.Fatal(err)
}
func assert(i interface{}, err error) interface{} {
if err != nil {
fatal(err)
}
return i
}
func addStream(codecName string, oc *FmtCtx, ist *Stream) (int, int) {
var cc *CodecCtx
var ost *Stream
codec := assert(FindEncoder(codecName)).(*Codec)
// Create Video stream in output context
if ost = oc.NewStream(codec); ost == nil {
fatal(errors.New("unable to create stream in output context"))
}
defer Release(ost)
if cc = NewCodecCtx(codec); cc == nil {
fatal(errors.New("unable to create codec context"))
}
defer Release(cc)
if oc.IsGlobalHeader() {
cc.SetFlag(CODEC_FLAG_GLOBAL_HEADER)
}
if codec.IsExperimental() {
cc.SetStrictCompliance(FF_COMPLIANCE_EXPERIMENTAL)
}
if cc.Type() == AVMEDIA_TYPE_AUDIO {
cc.SetSampleFmt(ist.CodecCtx().SampleFmt())
cc.SetSampleRate(ist.CodecCtx().SampleRate())
cc.SetChannels(ist.CodecCtx().Channels())
cc.SelectChannelLayout()
cc.SelectSampleRate()
}
if cc.Type() == AVMEDIA_TYPE_VIDEO {
cc.SetTimeBase(AVR{1, 25})
ost.SetTimeBase(AVR{1, 25})
cc.SetProfile(FF_PROFILE_MPEG4_SIMPLE)
fmt.Printf("setup dims: %d, %d\n", ist.CodecCtx().Width(), ist.CodecCtx().Height())
cc.SetDimension(ist.CodecCtx().Width(), ist.CodecCtx().Height())
cc.SetPixFmt(ist.CodecCtx().PixFmt())
}
if err := cc.Open(nil); err != nil {
fatal(err)
}
ost.SetCodecCtx(cc)
return ist.Index(), ost.Index()
}
func main() {
var srcFileName, dstFileName string
var stMap map[int]int = make(map[int]int, 0)
var lastDelta int64
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
if len(os.Args) != 3 {
fmt.Println("Simple transcoder, it guesses source format and codecs and tries to convert it to v:mpeg4/a:mp2.")
fmt.Println("usage: [input filename] [output.mp4]")
return
} else {
srcFileName = os.Args[1]
dstFileName = os.Args[2]
}
inputCtx := assert(NewInputCtx(srcFileName)).(*FmtCtx)
defer inputCtx.CloseInputAndRelease()
outputCtx := assert(NewOutputCtx(dstFileName)).(*FmtCtx)
defer outputCtx.CloseOutputAndRelease()
srcVideoStream, err := inputCtx.GetBestStream(AVMEDIA_TYPE_VIDEO)
if err != nil {
log.Println("No video stream found in", srcFileName)
} else {
i, o := addStream("mpeg4", outputCtx, srcVideoStream)
stMap[i] = o
}
srcAudioStream, err := inputCtx.GetBestStream(AVMEDIA_TYPE_AUDIO)
if err != nil {
log.Println("No audio stream found in", srcFileName)
} else {
i, o := addStream("aac", outputCtx, srcAudioStream)
stMap[i] = o
}
if err := outputCtx.WriteHeader(); err != nil {
fatal(err)
}
var (
packets int = 0
frames int = 0
encoded int = 0
)
for packet := range inputCtx.GetNewPackets() {
packets++
ist := assert(inputCtx.GetStream(packet.StreamIndex())).(*Stream)
ost := assert(outputCtx.GetStream(stMap[ist.Index()])).(*Stream)
frame, err := packet.Frames(ist.CodecCtx())
if err != nil {
fmt.Printf("error: %s\n", err)
}
if frame == nil {
Release(packet)
continue
}
frames++
if ost.IsAudio() {
fsTb := AVR{1, ist.CodecCtx().SampleRate()}
outTb := AVR{1, ist.CodecCtx().SampleRate()}
frame.SetPts(packet.Pts())
pts := RescaleDelta(ist.TimeBase(), frame.Pts(), fsTb.AVRational(), frame.NbSamples(), &lastDelta, outTb.AVRational())
frame.
SetNbSamples(ost.CodecCtx().FrameSize()).
SetFormat(ost.CodecCtx().SampleFmt()).
SetChannelLayout(ost.CodecCtx().ChannelLayout()).
SetPts(pts)
}
pkt, err := frame.Encode(ost.CodecCtx())
if err != nil {
fmt.Println(err)
continue
}
if pkt == nil {
continue
}
pkt.SetStreamIndex(ost.Index())
if err := outputCtx.WritePacket(pkt); err != nil {
fatal(err)
}
encoded++
Release(packet)
}
fmt.Printf("packets: %d, frames: %d, encoded: %d\n", packets, frames, encoded)
}