-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_compressor.go
187 lines (159 loc) · 5.19 KB
/
auto_compressor.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
package main
import (
"fmt"
"io"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// check number of args
func checkArgs() {
if len(os.Args) != 3 {
fmt.Println("usage: go run auto_compressor.go <input_file.mp4> <desired_output_size_MB>")
os.Exit(1)
}
}
// run ffmpeg to get video duration and bitrates
func getVideoInfo(inputFile string) (float64, int, int, error) {
cmd := exec.Command("ffmpeg", "-i", inputFile)
// ffmpeg sends the info to stderr, not stdout
stderr, err := cmd.StderrPipe()
if err != nil {
return 0, 0, 0, err
}
// start the ffmpeg command
if err := cmd.Start(); err != nil {
return 0, 0, 0, err
}
// read the stderr output where ffmpeg writes the info
outputBytes, err := io.ReadAll(stderr)
if err != nil {
return 0, 0, 0, err
}
// wait for the command to finish
if err := cmd.Wait(); err != nil {
// ffmpeg returns a non-zero exit code even when just printing info
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() != 0 {
// ignore the error since it's expected
} else {
return 0, 0, 0, err
}
}
outputStr := string(outputBytes)
// extract duration and bitrates from the output
duration := parseDuration(outputStr)
if duration == 0 {
return 0, 0, 0, fmt.Errorf("could not parse duration")
}
videoBitrate := parseVideoBitrate(outputStr)
if videoBitrate == 0 {
return 0, 0, 0, fmt.Errorf("could not parse video bitrate")
}
audioBitrate := parseAudioBitrate(outputStr)
if audioBitrate == 0 {
// default audio bitrate if not found
audioBitrate = 128
}
return duration, videoBitrate, audioBitrate, nil
}
// parse duration from ffmpeg output
func parseDuration(output string) float64 {
if strings.Contains(output, "Duration:") {
start := strings.Index(output, "Duration:") + 10
end := start + 11
durationStr := output[start:end]
timeParts := strings.Split(durationStr, ":")
hours, _ := strconv.ParseFloat(timeParts[0], 64)
minutes, _ := strconv.ParseFloat(timeParts[1], 64)
seconds, _ := strconv.ParseFloat(timeParts[2], 64)
return hours*3600 + minutes*60 + seconds
}
return 0
}
// parse video bitrate from ffmpeg output
func parseVideoBitrate(output string) int {
if strings.Contains(output, "bitrate:") {
start := strings.Index(output, "bitrate:") + 9
end := strings.Index(output[start:], " kb/s") + start
bitrateStr := output[start:end]
bitrate, err := strconv.Atoi(strings.TrimSpace(bitrateStr))
if err != nil {
return 0
}
return bitrate
}
return 0
}
// parse the audio bitrate from ffmpeg output
func parseAudioBitrate(output string) int {
// look for the specific audio stream bitrate (ie 128 kb/s under the audio stream)
if strings.Contains(output, "Audio:") && strings.Contains(output, " kb/s") {
audioIdx := strings.Index(output, "Audio:")
kbpsIdx := strings.Index(output[audioIdx:], " kb/s") + audioIdx
start := kbpsIdx - 4
audioBitrateStr := output[start:kbpsIdx]
audioBitrate, err := strconv.Atoi(strings.TrimSpace(audioBitrateStr))
if err != nil {
return 0
}
return audioBitrate
}
return 0
}
// calculate the desired video bitrate based on desired output size and audio bitrate
func calculateDesiredBitrate(duration float64, desiredSizeMB int, audioBitrate int) (int, error) {
// convert mb to kb
desiredSizeKB := float64(desiredSizeMB * 1024)
// calculate total bitrate in kbps
totalBitrate := (desiredSizeKB * 8) / duration
// subtract the audio bitrate to get video bitrate
videoBitrate := totalBitrate - float64(audioBitrate)
// set a minimum video bitrate at 100 kbps
const minVideoBitrate = 100
if videoBitrate < minVideoBitrate {
return 0, fmt.Errorf("calculated video bitrate (%d kbps) is too low, desired output size may be too small", int(videoBitrate))
}
return int(math.Round(videoBitrate)), nil
}
// compress the video with the calculated bitrate
func compressVideo(inputFile string, desiredBitrate int) {
baseName := filepath.Base(inputFile)
outputFile := "compressed_" + baseName
// run ffmpeg with the new video bitrate
cmd := exec.Command("ffmpeg", "-i", inputFile, "-b:v", fmt.Sprintf("%dk", desiredBitrate), outputFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalf("ffmpeg compression failed: %v", err)
}
fmt.Printf("video compressed successfully to %s with bitrate %d kbps\n", outputFile, desiredBitrate)
}
func main() {
checkArgs()
inputFile := os.Args[1]
desiredSizeMB, err := strconv.Atoi(os.Args[2])
if err != nil {
log.Fatalf("invalid output size: %v", err)
}
// get video info like duration and bitrates
duration, videoBitrate, audioBitrate, err := getVideoInfo(inputFile)
if err != nil {
log.Fatalf("failed to get video info: %v", err)
}
fmt.Printf("video duration: %.2f seconds\n", duration)
fmt.Printf("original video bitrate: %d kbps\n", videoBitrate)
fmt.Printf("audio bitrate: %d kbps\n", audioBitrate)
// calculate what the new video bitrate should be
desiredBitrate, err := calculateDesiredBitrate(duration, desiredSizeMB, audioBitrate)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("calculated video bitrate for desired output size: %d kbps\n", desiredBitrate)
// run the compression with the new bitrate
compressVideo(inputFile, desiredBitrate)
}