-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathscale_video.go
More file actions
111 lines (82 loc) · 2.23 KB
/
scale_video.go
File metadata and controls
111 lines (82 loc) · 2.23 KB
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
package main
import (
"log"
. "github.com/3d0c/gmf"
)
func fatal(err interface{}) {
log.Fatal(err)
}
func main() {
outputfilename := "sample-scale.mp4"
srcWidth, srcHeight := 640, 480
dstWidth, dstHeight := 320, 200
// codec, err := FindEncoder(AV_CODEC_ID_MPEG1VIDEO)
codec, err := FindEncoder("mpeg4")
if err != nil {
fatal(err)
}
srcEncCtx := NewCodecCtx(codec)
if srcEncCtx == nil {
fatal("Unable to allocate codec context")
}
srcEncCtx.SetWidth(640).SetHeight(480).SetPixFmt(AV_PIX_FMT_YUV420P)
dstCodecCtx := NewCodecCtx(codec)
if dstCodecCtx == nil {
fatal("Unable to allocate codec context")
}
defer Release(dstCodecCtx)
dstCodecCtx.
SetBitRate(400000).
SetWidth(dstWidth).
SetHeight(dstHeight).
SetTimeBase(AVR{1, 25}).
SetPixFmt(AV_PIX_FMT_YUV420P).
SetProfile(FF_PROFILE_MPEG4_SIMPLE)
outputCtx, err := NewOutputCtx(outputfilename)
if err != nil {
fatal(err)
}
if outputCtx.IsGlobalHeader() {
dstCodecCtx.SetFlag(CODEC_FLAG_GLOBAL_HEADER)
}
videoStream := outputCtx.NewStream(codec)
if videoStream == nil {
fatal("Unable to create stream for videoEnc " + codec.LongName())
}
defer Release(videoStream)
if err := dstCodecCtx.Open(nil); err != nil {
fatal(err)
}
videoStream.SetCodecCtx(dstCodecCtx)
// outputCtx.SetStartTime(0)
if err := outputCtx.WriteHeader(); err != nil {
fatal(err)
}
swsCtx := NewSwsCtx(srcEncCtx, dstCodecCtx, SWS_BICUBIC)
defer Release(swsCtx)
dstFrame := NewFrame().SetWidth(dstWidth).SetHeight(dstHeight).SetFormat(AV_PIX_FMT_YUV420P)
if err := dstFrame.ImgAlloc(); err != nil {
fatal(err)
}
var frame *Frame
i := int64(0)
for frame = range GenSyntVideoNewFrame(srcWidth, srcHeight, srcEncCtx.PixFmt()) {
frame.SetPts(i)
swsCtx.Scale(frame, dstFrame)
if p, err := dstFrame.Encode(videoStream.CodecCtx()); err == nil {
p.SetPts(RescaleQ(i, videoStream.CodecCtx().TimeBase(), videoStream.TimeBase()))
p.SetDts(RescaleQ(i, videoStream.CodecCtx().TimeBase(), videoStream.TimeBase()))
if err := outputCtx.WritePacket(p); err != nil {
fatal(err)
}
Release(p)
} else {
fatal(err)
}
i++
Release(frame)
}
Release(dstFrame)
outputCtx.CloseOutputAndRelease()
log.Println(i, "frames written to", outputfilename)
}