-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (188 loc) · 5.02 KB
/
main.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
198
199
200
201
202
203
204
205
206
207
package main
import (
"fmt"
"os"
"github.com/bartdeboer/flag"
)
var initial = Config{
VideoStream: 0,
AudioStream: 0,
SubtitleStream: 0,
ConstantQuality: -1,
ConstantRateFactor: -1,
}
func SetPreset(preset string) {
switch preset {
case "":
break
case "telegram-small":
initial.Codec = "libx264"
// initial.Size = "1080p"
initial.AudioRate = 144 // 128 = good
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.AudioStream = 0
initial.DrawTitle = false
initial.Extension = "mp4"
// initial.ConstantQuality = 23 // 1080p:19 720p:23
initial.ConstantRateFactor = 26
initial.PixelFormat = "yuv420p"
initial.ColorTransfer = "bt709"
initial.OptMetadata = false
// initial.WatermarkFile = "watermark-small.png"
// initial.WatermarkPosition = "W-w-48:48"
break
case "telegram-fair":
initial.Codec = "libx264"
initial.Size = "1080p"
initial.AudioRate = 144 // 128 = good
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.AudioStream = 0
initial.DrawTitle = false
initial.Extension = "mp4"
// initial.ConstantQuality = 23 // 1080p:19 720p:23
initial.ConstantRateFactor = 23
initial.PixelFormat = "yuv420p"
initial.ColorTransfer = "bt709"
initial.OptMetadata = false
// initial.WatermarkFile = "watermark-small.png"
// initial.WatermarkPosition = "W-w-48:48"
break
case "telegram":
initial.Codec = "h264_nvenc"
initial.Size = "1080p"
initial.FileSize = 2016 // max 2048
initial.AudioRate = 144 // 128 = good
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.AudioStream = 0
initial.DrawTitle = true
initial.Extension = "mp4"
initial.ConstantQuality = 19 // 1080p:19 720p:23
initial.PixelFormat = "yuv420p"
initial.ColorTransfer = "bt709"
initial.OptMetadata = true
break
case "telegram-hevc":
initial.Codec = "hevc_nvenc"
initial.Size = "1080p"
initial.FileSize = 2016 // max 2048
initial.AudioRate = 144 // 128 = good
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.AudioStream = 0
initial.DrawTitle = true
initial.Extension = "mp4"
initial.ConstantQuality = 22 // 1080p:19 720p:23
initial.PixelFormat = "yuv420p"
initial.ColorTransfer = "bt709"
initial.OptMetadata = true
case "telegram-x265":
initial.Codec = "libx265"
initial.Size = "1080p"
initial.FileSize = 2016 // max 2048
initial.AudioRate = 144 // 128 = good
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.AudioStream = 0
initial.DrawTitle = true
initial.Extension = "mp4"
// initial.ConstantRateFactor = 26
initial.PixelFormat = "yuv420p"
initial.ColorTransfer = "bt709"
initial.OptMetadata = true
break
case "phone":
// Size = "720p"
// FileSize = 1490 // max 1536
initial.AudioRate = 196
initial.AudioChannels = 2
initial.AudioCodec = "aac"
// DrawTitle = true
initial.Extension = "mp4"
case "homevideo":
initial.Codec = "libx265"
// initial.AudioRate = 196
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.ConstantRateFactor = 21
// DrawTitle = true
initial.Extension = "mp4"
case "homevideo2":
initial.Codec = "hevc_nvenc"
// initial.AudioRate = 196
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.ConstantQuality = 22
// DrawTitle = true
initial.Extension = "mp4"
case "teams":
initial.Codec = "h264_nvenc"
initial.Size = "1080p"
initial.AudioRate = 144 // 128 = good
initial.AudioChannels = 2
initial.AudioCodec = "aac"
initial.AudioStream = 0
initial.Extension = "mp4"
initial.ConstantQuality = 27 // 1080p:19 720p:23
initial.PixelFormat = "yuv420p"
initial.ColorTransfer = "bt709"
initial.OptMetadata = true
break
default:
fmt.Print("Unknown preset\n")
os.Exit(0)
}
}
func SetInitial() ([]string, error) {
if err := flag.SetDefaults(&initial); err != nil {
return nil, fmt.Errorf("error setting default values: %v", err)
}
if err := flag.ParseEnv(&initial); err != nil {
return nil, fmt.Errorf("error parsing environment variables: %v", err)
}
yamlCfg := struct {
Encode *Config `yaml:"encode"`
}{
Encode: &initial,
}
if err := LoadYaml(&yamlCfg); err != nil {
return nil, err
}
args, flags := flag.ParseArgs(os.Args[1:])
if preset, exists := flags["preset"]; exists {
SetPreset(preset)
}
err := flag.SetFlags(&initial, flags)
if err != nil {
return nil, fmt.Errorf("error parsing command-line arguments: %v", err)
}
{
_, helpExists := flags["help"]
_, hExists := flags["h"]
if helpExists || hExists {
fmt.Println("Usage:")
flag.PrintDefaults(&initial)
os.Exit(0)
}
}
return args, nil
}
func main() {
args, err := SetInitial()
if err != nil {
fmt.Printf("error initializing: %v", err)
os.Exit(1)
}
if len(args) == 0 {
flag.PrintDefaults(&initial)
os.Exit(1)
}
switch args[0] {
case "encode":
encode(args[1])
case "bulk":
bulkEncode(args[1])
}
}