forked from charmbracelet/vhs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.go
169 lines (153 loc) · 4.93 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
// 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"
)
const textFrameFormat = "frame-text-%05d.png"
const cursorFrameFormat = "frame-cursor-%05d.png"
// 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
}
// Options is the set of options for converting frames to a GIF.
type VideoOptions struct {
CleanupFrames bool
Framerate int
PlaybackSpeed float64
Input string
MaxColors int
Output VideoOutputs
Width int
Height int
Padding int
BackgroundColor string
}
const defaultFramerate = 50
const defaultHeight = 600
const defaultMaxColors = 256
const defaultPadding = 72
const defaultPlaybackSpeed = 1.0
const defaultWidth = 1200
// 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{
CleanupFrames: true,
Framerate: defaultFramerate,
Input: randomDir(),
MaxColors: defaultMaxColors,
Output: VideoOutputs{GIF: "out.gif", WebM: "", MP4: ""},
Width: defaultWidth,
Height: defaultHeight,
Padding: defaultPadding,
PlaybackSpeed: defaultPlaybackSpeed,
BackgroundColor: DefaultTheme.Background,
}
}
// MakeGIF takes a list of images (as frames) and converts them to a GIF.
func MakeGIF(opts VideoOptions) *exec.Cmd {
if opts.Output.GIF == "" {
return nil
}
fmt.Println("Creating GIF...")
//nolint:gosec
return exec.Command(
"ffmpeg", "-y",
"-r", fmt.Sprint(opts.Framerate),
"-i", filepath.Join(opts.Input, textFrameFormat),
"-r", fmt.Sprint(opts.Framerate),
"-i", filepath.Join(opts.Input, cursorFrameFormat),
"-filter_complex",
fmt.Sprintf(`[0][1]overlay[merged];[merged]scale=%d:%d:force_original_aspect_ratio=1[scaled];[scaled]fps=%d,setpts=PTS/%f[speed];[speed]pad=%d:%d:(ow-iw)/2:(oh-ih)/2:%s[padded];[padded]fillborders=left=%d:right=%d:top=%d:bottom=%d:mode=fixed:color=%s[bordered];[bordered]split[a][b];[a]palettegen=max_colors=256[p];[b][p]paletteuse[out]`,
opts.Width-2*opts.Padding, opts.Height-2*opts.Padding,
opts.Framerate, opts.PlaybackSpeed,
opts.Width, opts.Height,
opts.BackgroundColor,
opts.Padding, opts.Padding, opts.Padding, opts.Padding,
opts.BackgroundColor,
),
"-map", "[out]",
opts.Output.GIF,
)
}
// 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
}
fmt.Println("Creating WebM...")
//nolint:gosec
return exec.Command(
"ffmpeg", "-y",
"-r", fmt.Sprint(opts.Framerate),
"-i", filepath.Join(opts.Input, textFrameFormat),
"-r", fmt.Sprint(opts.Framerate),
"-i", filepath.Join(opts.Input, cursorFrameFormat),
"-filter_complex",
fmt.Sprintf(`[0][1]overlay,scale=%d:%d:force_original_aspect_ratio=1,fps=%d,setpts=PTS/%f,pad=%d:%d:(ow-iw)/2:(oh-ih)/2:%s,fillborders=left=%d:right=%d:top=%d:bottom=%d:mode=fixed:color=%s`,
opts.Width-2*opts.Padding, opts.Height-2*opts.Padding,
opts.Framerate, opts.PlaybackSpeed,
opts.Width, opts.Height,
opts.BackgroundColor,
opts.Padding, opts.Padding, opts.Padding, opts.Padding,
opts.BackgroundColor,
),
"-pix_fmt", "yuv420p",
"-an",
"-crf", "30",
"-b:v", "0",
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
}
fmt.Println("Creating MP4...")
//nolint:gosec
return exec.Command(
"ffmpeg", "-y",
"-r", fmt.Sprint(opts.Framerate),
"-i", filepath.Join(opts.Input, textFrameFormat),
"-r", fmt.Sprint(opts.Framerate),
"-i", filepath.Join(opts.Input, cursorFrameFormat),
"-filter_complex",
fmt.Sprintf(`[0][1]overlay,scale=%d:%d:force_original_aspect_ratio=1,fps=%d,setpts=PTS/%f,pad=%d:%d:(ow-iw)/2:(oh-ih)/2:%s,fillborders=left=%d:right=%d:top=%d:bottom=%d:mode=fixed:color=%s`,
opts.Width-2*opts.Padding, opts.Height-2*opts.Padding,
opts.Framerate, opts.PlaybackSpeed,
opts.Width, opts.Height,
opts.BackgroundColor,
opts.Padding, opts.Padding, opts.Padding, opts.Padding,
opts.BackgroundColor,
),
"-vcodec", "libx264",
"-pix_fmt", "yuv420p",
"-an",
"-crf", "20",
opts.Output.MP4,
)
}