-
Notifications
You must be signed in to change notification settings - Fork 5
/
imageio.go
167 lines (156 loc) · 3.45 KB
/
imageio.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
package imageio
import (
"errors"
"image"
"io"
"log"
"os"
"os/exec"
"strconv"
"fmt"
)
// Mp4 video convert options.
type Options struct {
FPS int
Codec string
Pixelformat string
Pixfmt string
}
// Video struct.
type Video struct {
Cmd *exec.Cmd
StdinWr io.WriteCloser
Dimension string
ExePath string
Output string
Option *Options
}
// New Instance.
func NewVideo(output string, op *Options) *Video {
if op.Codec == "" {
op.Codec = "libx264"
}
if op.Pixelformat == "" {
op.Pixelformat = "yuv420p"
}
if op.Pixfmt == "" {
op.Pixfmt = "rgba"
}
if op.FPS == 0 {
op.FPS = 25
}
return &Video{Cmd: nil, StdinWr: nil, Dimension: "", Output: output, Option: op}
}
// Initialize FFmpeg thread.
func (this *Video) initialize() error {
exe, err := GetFFmpegExe()
if err != nil {
return err
}
if this.Dimension == "" {
this.Dimension = "512x512"
}
pix_fmt := this.Option.Pixfmt
fps := strconv.Itoa(this.Option.FPS)
codec := this.Option.Codec
pixelformat := this.Option.Pixelformat
outputfile := this.Output
cmdstr := []string{"-y",
"-f", "rawvideo",
"-vcodec", "rawvideo",
"-s", this.Dimension,
"-pix_fmt", pix_fmt,
"-r", fps,
"-i", "-", "-an",
"-vcodec", codec,
"-pix_fmt", pixelformat,
"-crf", "25",
"-r", "50",
"-v", "warning", outputfile}
return this.execFFmpegCommands(exe, cmdstr)
}
// Write image by file path.
func (this *Video) WriteImageFile(imagePath string) error {
img, err := LoadImage(imagePath)
if err != nil {
return err
}
width, height, err := this.getImageDimension(imagePath)
if err != nil {
return err
}
dimension := fmt.Sprintf("%dx%d", width, height)
if this.Dimension == "" {
this.Dimension = dimension
if err := this.initialize(); err != nil {
return err
}
}
if dimension != this.Dimension {
return errors.New("All images in a movie should have same size.")
}
if img != nil && this.Cmd != nil && this.StdinWr != nil {
imgstring := LoadImageBitmap(img)
this.StdinWr.Write(imgstring)
}
return nil
}
// Write image by image.Image
func (this *Video) WriteImage(image image.Image) error {
width := image.Bounds().Size().X
height := image.Bounds().Size().Y
dimension := fmt.Sprintf("%dx%d", width, height)
if this.Dimension == "" {
this.Dimension = dimension
if err := this.initialize(); err != nil {
return err
}
}
if dimension != this.Dimension {
return errors.New("All images in a movie should have same size.")
}
if image != nil && this.Cmd != nil && this.StdinWr != nil {
imagestring := LoadImageBitmap(image)
_, err := this.StdinWr.Write(imagestring)
if err != nil {
return err
}
}
return nil
}
// Get image Dimension
func (this *Video) getImageDimension(imagePath string) (int, int, error) {
file, err := os.Open(imagePath)
if err != nil {
return 0, 0, err
}
image, _, err := image.DecodeConfig(file)
return image.Width, image.Height, err
}
// Close ffmpeg
func (this *Video) Close() error {
if this.Cmd == nil {
return errors.New("FFmpeg command is nil.")
}
if this.StdinWr != nil {
err := this.StdinWr.Close()
if err != nil {
return err
}
log.Print("Close the mp4 video.")
}
this.Cmd.Wait()
this.Cmd = nil
return nil
}
// Execute FFmpeg commands.
func (this *Video) execFFmpegCommands(commandName string, params []string) error {
this.Cmd = exec.Command(commandName, params...)
stdinWr, err := this.Cmd.StdinPipe()
if err != nil {
return err
}
this.StdinWr = stdinWr
this.Cmd.Start()
return nil
}