Skip to content

Commit

Permalink
Bugfix / ffmpeg input device (mic) not working on Windows (#27051)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
Teapack1 authored Jan 8, 2024
1 parent 7d9d5ce commit 98dba52
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/transformers/pipelines/audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -72,7 +72,7 @@ def ffmpeg_microphone(
input_ = ":0"
elif system == "Windows":
format_ = "dshow"
input_ = "default"
input_ = _get_microphone_name()

ffmpeg_command = [
"ffmpeg",
Expand Down Expand Up @@ -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"

0 comments on commit 98dba52

Please sign in to comment.