-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
avfoundation.go
66 lines (50 loc) · 1.2 KB
/
avfoundation.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
package skills
import (
"bufio"
"bytes"
"os/exec"
"regexp"
"strings"
)
// DevicesAvfoundation returns a list of AVFoundation devices. You have to provide
// the path to the ffmpeg binary.
func DevicesAvfoundation(binary string) ([]HWDevice, error) {
buf := bytes.NewBuffer(nil)
cmd := exec.Command(binary, "-f", "avfoundation", "-list_devices", "true", "-i", "")
cmd.Env = []string{}
cmd.Stderr = buf
cmd.Run()
devices := parseAvfoundationDevices(buf)
return devices, nil
}
func parseAvfoundationDevices(data *bytes.Buffer) []HWDevice {
devices := []HWDevice{}
re := regexp.MustCompile(`^\s*\[.*?\] \[([0-9]+)\] (.*?)$`)
media := ""
scanner := bufio.NewScanner(data)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "AVFoundation video devices") {
media = "video"
continue
} else if strings.Contains(line, "AVFoundation audio devices") {
media = "audio"
continue
}
if media == "" {
continue
}
matches := re.FindStringSubmatch(line)
if matches == nil {
continue
}
device := HWDevice{
Id: matches[1],
Name: matches[2],
Media: media,
}
devices = append(devices, device)
}
return devices
}