-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
343 lines (309 loc) · 8.36 KB
/
utils.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package aio
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"unsafe"
)
// Returns true if file exists, false otherwise.
// https://stackoverflow.com/questions/12518876/how-to-check-if-a-file-exists-in-go.
func exists(filename string) bool {
_, err := os.Stat(filename)
if err == nil {
return true
}
if errors.Is(err, os.ErrNotExist) {
return false
}
return false
}
// Checks if the given program is installed.
func installed(program string) error {
cmd := exec.Command(program, "-version")
if err := cmd.Run(); err != nil {
return fmt.Errorf("%s is not installed", program)
}
return nil
}
// Runs ffprobe on the given file and returns a map of the metadata.
func ffprobe(filename, stype string) ([]map[string]string, error) {
// "stype" is stream stype. "v" for video, "a" for audio.
// Extract media metadata information with ffprobe.
cmd := exec.Command(
"ffprobe",
"-show_streams",
"-select_streams", stype,
"-print_format", "compact",
"-loglevel", "quiet",
filename,
)
pipe, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, err
}
// Read ffprobe output from Stdout.
builder := bytes.Buffer{}
buffer := make([]byte, 1024)
for {
n, err := pipe.Read(buffer)
builder.Write(buffer[:n])
if err == io.EOF {
break
}
}
// Wait for ffprobe command to complete.
if err := cmd.Wait(); err != nil {
return nil, err
}
// Parse ffprobe output to fill in audio data.
datalist := make([]map[string]string, 0)
metadata := builder.String()
for _, stream := range strings.Split(metadata, "\n") {
if len(strings.TrimSpace(stream)) > 0 {
data := make(map[string]string)
for _, line := range strings.Split(stream, "|") {
if strings.Contains(line, "=") {
keyValue := strings.Split(line, "=")
if _, ok := data[keyValue[0]]; !ok {
data[keyValue[0]] = keyValue[1]
}
}
}
datalist = append(datalist, data)
}
}
return datalist, nil
}
// Parses the given data into a float64.
func parse(data string) float64 {
n, err := strconv.ParseFloat(data, 64)
if err != nil {
return 0
}
return n
}
// Returns the microphone device name used for the -f option with ffmpeg.
func microphone() (string, error) {
switch runtime.GOOS {
case "linux":
return "pulse", nil
case "darwin":
return "avfoundation", nil // qtkit
case "windows":
return "dshow", nil // vfwcap
default:
return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
}
}
// For webcam streaming on windows, ffmpeg requires a device name.
// All device names are parsed and returned by this function.
func parseDevices(buffer string) []string {
index := strings.Index(strings.ToLower(buffer), "directshow audio device")
if index != -1 {
buffer = buffer[index:]
}
type Pair struct {
name string
alt string
}
// Parses ffmpeg output to get device names. Windows only.
// Uses parsing approach from https://github.com/imageio/imageio/blob/master/imageio/plugins/ffmpeg.py#L681.
pairs := []Pair{}
// Find all device names surrounded by quotes. E.g "Windows Camera Front"
regex := regexp.MustCompile("\"[^\"]+\"")
for _, line := range strings.Split(strings.ReplaceAll(buffer, "\r\n", "\n"), "\n") {
if strings.Contains(strings.ToLower(line), "alternative name") {
match := regex.FindString(line)
if len(match) > 0 {
pairs[len(pairs)-1].alt = match[1 : len(match)-1]
}
} else {
match := regex.FindString(line)
if len(match) > 0 {
pairs = append(pairs, Pair{name: match[1 : len(match)-1]})
}
}
}
devices := []string{}
// If two devices have the same name, use the alternate name of the later device as its name.
for _, pair := range pairs {
if contains(devices, pair.name) {
devices = append(devices, pair.alt)
} else {
devices = append(devices, pair.name)
}
}
return devices
}
func contains(list []string, item string) bool {
for _, i := range list {
if i == item {
return true
}
}
return false
}
// Returns the microphone device name.
// On windows, ffmpeg output from the -list_devices command is parsed to find the device name.
func getDevicesWindows() ([]string, error) {
// Run command to get list of devices.
cmd := exec.Command(
"ffmpeg",
"-hide_banner",
"-list_devices", "true",
"-f", "dshow",
"-i", "dummy",
)
pipe, err := cmd.StderrPipe()
if err != nil {
return nil, err
}
// Start the command and immediately continue so that the pipe can be read.
if err := cmd.Start(); err != nil {
return nil, err
}
// Read list devices from Stdout.
builder := bytes.Buffer{}
buffer := make([]byte, 1024)
for {
n, err := pipe.Read(buffer)
builder.Write(buffer[:n])
if err == io.EOF {
break
}
}
// Wait for the command to finish.
cmd.Wait()
devices := parseDevices(builder.String())
return devices, nil
}
// Check audio format string.
func checkFormat(format string) error {
match := regexp.MustCompile(`^(([us]8)|([us]((16)|(24)|(32))[bl]e)|(f((32)|(64))[bl]e))$`)
if len(match.FindString(format)) == 0 {
formats := "u8, s8, u16, s16, u24, s24, u32, s32, f32, or f64"
return fmt.Errorf("audio format %s is not supported, must be one of %s", format[:len(format)-2], formats)
}
return nil
}
func createFormat(format string) string {
switch format {
case "u8", "s8":
return format
default:
return fmt.Sprintf("%s%s", format, endianness())
}
}
// Little Endian -> "le", Big Endian -> "be".
func endianness() string {
x := 1
littleEndian := *(*byte)(unsafe.Pointer(&x)) == 1
if littleEndian {
return "le"
} else {
return "be"
}
}
// Alias the byte buffer as a certain type specified by the format string.
func bytesToSamples(buffer []byte, size int, format string) interface{} {
switch format {
case "f32be", "f32le":
var data []float32
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&data))
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&buffer)).Data
pointer.Cap = size
pointer.Len = size
return data
case "f64be", "f64le":
var data []float64
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&data))
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&buffer)).Data
pointer.Cap = size
pointer.Len = size
return data
case "s16be", "s16le":
var data []int16
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&data))
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&buffer)).Data
pointer.Cap = size
pointer.Len = size
return data
case "s32be", "s32le":
var data []int32
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&data))
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&buffer)).Data
pointer.Cap = size
pointer.Len = size
return data
case "s8":
var data []int8
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&data))
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&buffer)).Data
pointer.Cap = size
pointer.Len = size
return data
case "u16be", "u16le":
var data []uint16
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&data))
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&buffer)).Data
pointer.Cap = size
pointer.Len = size
return data
case "u32be", "u32le":
var data []uint32
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&data))
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&buffer)).Data
pointer.Cap = size
pointer.Len = size
return data
default:
return buffer
}
}
func samplesToBytes(data interface{}) []byte {
var buffer []byte
pointer := (*reflect.SliceHeader)(unsafe.Pointer(&buffer))
var size int
switch data := data.(type) {
case []uint8:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data)
case []int8:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data)
case []uint16:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data) * 2
case []int16:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data) * 2
case []uint32:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data) * 4
case []int32:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data) * 4
case []float32:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data) * 4
case []float64:
pointer.Data = (*reflect.SliceHeader)(unsafe.Pointer(&data)).Data
size = len(data) * 8
default:
return nil
}
pointer.Cap = size
pointer.Len = size
return buffer
}