Bugfix / ffmpeg input device (mic) not working on Windows (#27051)

* fix input audio device for windows.

* ffmpeg audio device Windows

* Fixes wrong input device assignment in Windows

* Fixed getting mic on Windows systems by adding _get_microphone_name() function.
This commit is contained in:
Ondrej Major 2024-01-08 13:32:36 +01:00 committed by GitHub
parent 7d9d5cea55
commit 98dba52ccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,7 +52,7 @@ def ffmpeg_microphone(
format_for_conversion: str = "f32le",
):
"""
Helper function ro read raw microphone data.
Helper function to read raw microphone data.
"""
ar = f"{sampling_rate}"
ac = "1"
@ -72,7 +72,7 @@ def ffmpeg_microphone(
input_ = ":0"
elif system == "Windows":
format_ = "dshow"
input_ = "default"
input_ = _get_microphone_name()
ffmpeg_command = [
"ffmpeg",
@ -226,3 +226,23 @@ def _ffmpeg_stream(ffmpeg_command, buflen: int):
yield raw
except FileNotFoundError as error:
raise ValueError("ffmpeg was not found but is required to stream audio files from filename") from error
def _get_microphone_name():
"""
Retrieve the microphone name in Windows .
"""
command = ["ffmpeg", "-list_devices", "true", "-f", "dshow", "-i", ""]
try:
ffmpeg_devices = subprocess.run(command, text=True, stderr=subprocess.PIPE, encoding="utf-8")
microphone_lines = [line for line in ffmpeg_devices.stderr.splitlines() if "(audio)" in line]
if microphone_lines:
microphone_name = microphone_lines[0].split('"')[1]
print(f"Using microphone: {microphone_name}")
return f"audio={microphone_name}"
except FileNotFoundError:
print("ffmpeg was not found. Please install it or make sure it is in your system PATH.")
return "default"