54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package audio
|
|
|
|
import (
|
|
"os/exec"
|
|
"fmt"
|
|
"regexp"
|
|
)
|
|
|
|
|
|
type MicStreamConfig struct {
|
|
Format string
|
|
Device string
|
|
Encoding string
|
|
Bitrate int
|
|
Channels int
|
|
}
|
|
|
|
func StreamMic(config MicStreamConfig) *exec.Cmd {
|
|
cmd := exec.Command("ffmpeg", "-re",
|
|
"-f", config.Format,
|
|
"-i", config.Device,
|
|
"-f", config.Encoding,
|
|
"-ar", fmt.Sprintf("%d", config.Bitrate),
|
|
"-ac", fmt.Sprintf("%d", config.Channels),
|
|
"-",
|
|
)
|
|
return cmd
|
|
}
|
|
|
|
func GetMicDevice() (string, error) {
|
|
cmd := exec.Command("ffmpeg",
|
|
"-f", "avfoundation",
|
|
"-list_devices", "true",
|
|
"-i", "\"\"",
|
|
)
|
|
// ignore error, this ffmpeg command returns exit status 1
|
|
outputBytes, _ := cmd.CombinedOutput()
|
|
output := string(outputBytes)
|
|
parsedOutput := parseDeviceNumber(output)
|
|
if parsedOutput == "" {
|
|
return "", fmt.Errorf("Command: %s\nFailed to parse FFmpeg output:\n%s", cmd, output)
|
|
}
|
|
return parsedOutput, nil
|
|
}
|
|
|
|
func parseDeviceNumber(ffmpegOutput string) string {
|
|
re := regexp.MustCompile(`\[(\d)\].*?[Mm]icrophone`)
|
|
matches := re.FindStringSubmatch(ffmpegOutput)
|
|
if (len(matches) == 2) {
|
|
return matches[1]
|
|
}
|
|
return ""
|
|
}
|