-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFMpeg.go
95 lines (69 loc) · 1.51 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
package ffmpeg
import (
"bytes"
"image/draw"
"image/png"
"os/exec"
)
const (
commandName = "ffmpeg"
stdIn = "pipe:0"
)
type FFMpeg struct {
input, framerate, vcodec, output string
}
func (F *FFMpeg) SetInput(input string) *FFMpeg {
F.input = input
return F
}
func (F *FFMpeg) SetFramerate(framerate int) *FFMpeg {
F.framerate = string(rune(framerate))
return F
}
func (F *FFMpeg) SetVcodec(vcodec string) *FFMpeg {
F.vcodec = vcodec
return F
}
func (F *FFMpeg) SetOutput(output string) *FFMpeg {
F.output = output
return F
}
func NewFFMpeg() *FFMpeg {
return &FFMpeg{vcodec: "png", framerate: "24"}
}
func (F *FFMpeg) CreateVideoFromImages(images []draw.Image) (err error) {
cmd := F.SetInput(stdIn).createCommand()
stdin, err := cmd.StdinPipe() // Open stdin pipe
if nil != err {
return
}
err = cmd.Start() // Start a process on another goroutine
if nil != err {
return
}
for _, image := range images {
buf := new(bytes.Buffer)
err = png.Encode(buf, image)
if nil != err {
continue
}
_, _ = stdin.Write(buf.Bytes())
}
err = stdin.Close()
if nil != err {
return
}
err = cmd.Wait()
if nil != err {
return
}
return nil
}
func (F *FFMpeg) createCommand() *exec.Cmd {
return exec.Command(commandName, "-y", // Yes to all
"-i", F.input, // take stdin as input
"-framerate", F.framerate, // suppress "Frame rate very high for a muxer not efficiently supporting it"
"-vcodec", F.vcodec, // Down sample audio birate to 128k
F.output, // output to stdout
)
}