-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_io.go
197 lines (170 loc) · 4.4 KB
/
video_io.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
190
191
192
193
194
195
196
197
package main
import (
"image"
"image/draw"
"image/jpeg"
"path/filepath"
"strings"
"strconv"
"io/ioutil"
"bufio"
"github.com/schollz/progressbar/v3"
"os"
"os/exec"
"fmt"
)
// CalculateWidthHeight calculates the width of a 16:9 video given the height in pixels,
// ensuring both width and height are divisible by 2.
func CalculateWidthHeight(height int) (width, newHeight int) {
// Calculate width based on the 16:9 aspect ratio formula: width = (height * 16) / 9
width = (height * 16) / 9
// Ensure width is divisible by 2
if width%2 != 0 {
width++
}
// Ensure height is divisible by 2
if height%2 != 0 {
height++
}
return width, height
}
// copyAndWrite copies the image.RGBA and passes it to a video writer.
func copyAndWrite(img *image.RGBA) {
// Copy the image.RGBA to a new image.RGBA
copiedImg := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy()))
draw.Draw(copiedImg, copiedImg.Bounds(), img, img.Bounds().Min, draw.Src)
// Write the frame to the image
abspath := filepath.Join(tempDirFrames, fmt.Sprintf("%d.jpg", cframes))
f, _ := os.Create(abspath)
jpeg.Encode(f, copiedImg, nil)
f.Close()
cframes++
bar.Add(1)
}
func joinOutputImgs() {
abspath, err := filepath.Abs(tempDirFrames)
if err != nil {
fmt.Println("Error:", err)
}
err = convertImagesToMP4(abspath, inputVideo, filepath.Join(outputDir,getBasenameWithoutExt(inputVideo)+".mp4"), fps)
if err != nil {
fmt.Println("Error:", err)
}
}
func convertImagesToMP4(inputFolder, audioFile, outputFile string, fps float64) error {
cmd := exec.Command("ffmpeg",
"-r", strconv.FormatFloat(fps, 'f', -1, 64),
"-framerate", strconv.FormatFloat(fps, 'f', -1, 64),
// "-pattern_type", "glob",
"-i", inputFolder+"/%d.jpg",
"-i", audioFile,
"-map", "0:v:0",
"-map", "1:a:0",
// "-c:v", "copy",
"-c:v", "mjpeg",
"-q:v", "6",
// "-vf", "setsar=1,crop=aspect_ratio=1",//"cropdetect=limit=0:round=2:reset=0,scale="+strconv.Itoa(squareSize)+":"+strconv.Itoa(squareSize),
// "-aspect", "1:1",
"-c:a", "copy",
outputFile,"-y", // Overwrite output file without asking
)
fmt.Println(cmd.String())
// Create a progress bar
files, err := ioutil.ReadDir(inputFolder)
if err != nil {
return err
}
totalFrames := len(files)
barp := progressbar.Default(int64(totalFrames))
// Create a progress bar
cmdReader, err := cmd.StderrPipe()
if err != nil {
return err
}
// Start the command
err = cmd.Start()
if err != nil {
return err
}
scanner := bufio.NewScanner(cmdReader)
scanner.Split(bufio.ScanWords)
getF := false
for scanner.Scan() {
line := scanner.Text()
// fmt.Println(line)
if getF {
getF = false
cf, err := strconv.Atoi(line)
if err != nil {
continue
}
barp.Set(cf)
}
if strings.Contains(line, "frame=") {
getF = true
}
}
// Wait for the command to finish
err = cmd.Wait()
if err != nil {
return err
}
barp.Clear()
barp.Close()
//! Must clear output folder image sequence after, or it might get appended to other videos being processed
err = deleteFilesInFolder(inputFolder)
if err != nil {
return err
}
return nil
}
func resizeTiny(inputFile, outputFile string, width, height int) error {
cmd := exec.Command("ffmpeg",
"-i", inputFile,
"-vf", "scale="+strconv.Itoa(width)+":"+strconv.Itoa(height),
outputFile,
"-y", // Overwrite output file without asking
)
fmt.Println(cmd.String())
// Create a progress bar
cmdReader, err := cmd.StderrPipe()
if err != nil {
return err
}
// Start the command
err = cmd.Start()
if err != nil {
return err
}
totalFrames, err := countFrames(inputFile)
if err != nil {
return err
}
scanner := bufio.NewScanner(cmdReader)
scanner.Split(bufio.ScanWords)
getF := false
barp := progressbar.Default(int64(totalFrames))
for scanner.Scan() {
line := scanner.Text()
// fmt.Println(line)
if getF {
getF = false
cf, err := strconv.Atoi(line)
if err != nil {
continue
}
barp.Set(cf)
}
if strings.Contains(line, "frame=") {
getF = true
}
}
// Wait for the command to finish
err = cmd.Wait()
if err != nil {
return err
}
barp.Clear()
barp.Close()
return nil
}