Continue prototyping
This commit is contained in:
		
							
								
								
									
										53
									
								
								audio/audio.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								audio/audio.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
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 ""
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user