-
Notifications
You must be signed in to change notification settings - Fork 260
/
video.go
189 lines (161 loc) · 4.63 KB
/
video.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
// Package vhs video.go spawns the ffmpeg process to convert the frames,
// collected by go-rod's screenshots into the input folder, to a GIF, WebM,
// MP4.
//
// MakeGIF takes several options to modify the behaviour of the ffmpeg process,
// which can be configured through the Set command.
//
// Set MaxColors 256
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
textFrameFormat = "frame-text-%05d.png"
cursorFrameFormat = "frame-cursor-%05d.png"
)
const (
mp4 = ".mp4"
webm = ".webm"
gif = ".gif"
)
// randomDir returns a random temporary directory to be used for storing frames
// from screenshots of the terminal.
func randomDir() string {
tmp, err := os.MkdirTemp(os.TempDir(), "vhs")
if err != nil {
log.Printf("Error creating temporary directory: %v", err)
}
return tmp
}
// VideoOutputs is a mapping from file type to file path for all video outputs
// of VHS.
type VideoOutputs struct {
GIF string
WebM string
MP4 string
Frames string
}
// VideoOptions is the set of options for converting frames to a GIF.
type VideoOptions struct {
Framerate int
PlaybackSpeed float64
Input string
MaxColors int
Output VideoOutputs
StartingFrame int
Style *StyleOptions
}
const (
defaultFramerate = 50
defaultStartingFrame = 1
)
// DefaultVideoOptions is the set of default options for converting frames
// to a GIF, which are used if they are not overridden.
func DefaultVideoOptions() VideoOptions {
return VideoOptions{
Framerate: defaultFramerate,
Input: randomDir(),
MaxColors: defaultMaxColors,
Output: VideoOutputs{GIF: "", WebM: "", MP4: "", Frames: ""},
PlaybackSpeed: defaultPlaybackSpeed,
StartingFrame: defaultStartingFrame,
}
}
func marginFillIsColor(marginFill string) bool {
return strings.HasPrefix(marginFill, "#")
}
// ensureDir ensures that the file path of the output can be created by
// creating all the necessary nested folders.
func ensureDir(output string) {
err := os.MkdirAll(filepath.Dir(output), os.ModePerm)
if err != nil {
fmt.Println(ErrorStyle.Render("Unable to create output directory: "), output)
}
}
// buildFFopts assembles an ffmpeg command from some VideoOptions
func buildFFopts(opts VideoOptions, targetFile string) []string {
var args []string
streamCounter := 2
streamBuilder := NewStreamBuilder(streamCounter, opts.Input, opts.Style)
// Input frame options, used no matter what
// Stream 0: text frames
// Stream 1: cursor frames
streamBuilder.args = append(streamBuilder.args,
"-y",
"-r", fmt.Sprint(opts.Framerate),
"-start_number", fmt.Sprint(opts.StartingFrame),
"-i", filepath.Join(opts.Input, textFrameFormat),
"-r", fmt.Sprint(opts.Framerate),
"-start_number", fmt.Sprint(opts.StartingFrame),
"-i", filepath.Join(opts.Input, cursorFrameFormat),
)
streamBuilder = streamBuilder.
WithMargin().
WithBar().
WithCorner()
filterBuilder := NewVideoFilterBuilder(&opts).
WithWindowBar(streamBuilder.barStream).
WithBorderRadius(streamBuilder.cornerStream).
WithMarginFill(streamBuilder.marginStream)
// Format-specific options
switch filepath.Ext(targetFile) {
case gif:
filterBuilder = filterBuilder.WithGIF()
case webm:
streamBuilder = streamBuilder.WithWebm()
case mp4:
streamBuilder = streamBuilder.WithMP4()
}
args = append(args, streamBuilder.Build()...)
args = append(args, filterBuilder.Build()...)
args = append(args, targetFile)
return args
}
// MakeGIF takes a list of images (as frames) and converts them to a GIF.
func MakeGIF(opts VideoOptions) *exec.Cmd {
targetFile := opts.Output.GIF
if opts.Output.GIF == "" && opts.Output.WebM == "" && opts.Output.MP4 == "" {
targetFile = "out.gif"
} else if opts.Output.GIF == "" {
return nil
}
log.Println(GrayStyle.Render("Creating " + opts.Output.GIF + "..."))
ensureDir(opts.Output.GIF)
//nolint:gosec
return exec.Command(
"ffmpeg",
buildFFopts(opts, targetFile)...,
)
}
// MakeWebM takes a list of images (as frames) and converts them to a WebM.
func MakeWebM(opts VideoOptions) *exec.Cmd {
if opts.Output.WebM == "" {
return nil
}
log.Println(GrayStyle.Render("Creating " + opts.Output.WebM + "..."))
ensureDir(opts.Output.WebM)
//nolint:gosec
return exec.Command(
"ffmpeg",
buildFFopts(opts, opts.Output.WebM)...,
)
}
// MakeMP4 takes a list of images (as frames) and converts them to an MP4.
func MakeMP4(opts VideoOptions) *exec.Cmd {
if opts.Output.MP4 == "" {
return nil
}
log.Println(GrayStyle.Render("Creating " + opts.Output.MP4 + "..."))
ensureDir(opts.Output.MP4)
//nolint:gosec
return exec.Command(
"ffmpeg",
buildFFopts(opts, opts.Output.MP4)...,
)
}