Windows shell: How can I get the audio device(s) n

2019-03-22 06:38发布

问题:

I am not sure if this is strictly a programming question, as long as I don't mind to use additional software in order to solve the problem, as long as it keeps being scriptable or command-line (this is: a not GUI solution). Anyway, I have posted another (a bit different) question at SuperUser. By the way, I will update here if I get the answer there.

My Windows program (just a shell script) invokes VLC command-line in order to record audio from the audio card and stream it out, like:

vlc dshow:// :dshow-vdev=none :dshow-adev="SoundMAX HD Audio I" :std{mux=ts,access=http,dst=:8080} :sout-keep [rest of the cmdline not relevant]

As long as there could be multiple audio devices on a computer (webcam, multiple audio cards, bluettoth devices... etc), I need to programmatically get the name of them.

I have used FFMPEG until now:

ffmpeg -list_devices true -f dshow -i dummy

... but sometimes it does not work OK, for example:

d:\Utils\FFMPEG\bin>ffmpeg -list_devices true -f dshow -i dummy
ffmpeg version N-72460-g11aa050 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.9.2 (GCC)  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmfx --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --ena
ble-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-lzma --enable-decklink --enable-zlib
  libavutil      54. 26.100 / 54. 26.100
  libavcodec     56. 41.100 / 56. 41.100
  libavformat    56. 34.100 / 56. 34.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 16.101 /  5. 16.101
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
[dshow @ 0000000000363520] DirectShow video devices (some may be both video and
audio devices)
[dshow @ 0000000000363520] Could not enumerate video devices (or none found).
[dshow @ 0000000000363520] DirectShow audio devices
[dshow @ 0000000000363520] Could not enumerate audio only devices (or none found
).
dummy: Immediate exit requested

The command line tool SystemInfo does neither work:

C:\>systeminfo | find "audio" /i

C:\>

Searching inside regedit found nothing about my soundcard example (SoundMAX HD Audio I).

Isn't there any other more reliable way to programmatically obtain the sound device(s) name(s)?

If possible, I would prefer to obtain answers for both Windows XP and Vista or later.

Additional tests performed:

  • Installed latest MicroSoft .NET FrameWork v4.5.2 & rebooted.
  • Tested older versions of FFMPEG: v4.8 (2014) and v4.7 (2013).
  • Tested UAC elevated prompt.

回答1:

Answer obtained (thanks, @Karan) from the above mentioned SuperUser thread.

Firstly, FFMPEG should return the device name value. If not, try:

  • Update FFMPEG to latest version.
  • Update .NET FrameWork.
  • Try another version. Note that v4.7.3 seems not to be needing .NET Framework, if this were the issue (extracted from this thread).
  • For microphone devices make sure that the jack is connected. On some systems it seems the device is disabled (disappears) if not. Of course, this one does not apply to portables with embedded (built-in) microphone.

As a second option, only working (as for today, June 2015) for Vista and above, and not needing for the 30-40MB download of FFMPEG, you can try SoundVolumeView from NirSoft. Send values to .txt file and filter by doing:

SoundVolumeView /scomma Audio.txt
for /f "tokens=1 delims=," %d in ('type Audio.txt ^| find "Capture"') do @echo Default recording device is: "%d"

As a third option, still for Vista and above, all the audio input (capture & recording) devices can be extracted from the register (explained here by @Karan) by issuing this command line:

for /f "tokens=9 delims=\" %a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture" /s ^| find "\Properties"') do @for /f "tokens=2*" %k in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture\%a\Properties" /v "{a45c254e-df1c-4efd-8020-67d146a850e0},2"') do @echo "%l"

Note: remember that when scripting such devices you can find special localized characters, like the spanish word Micrófono for Microphone. FFMPEG, for example, would show it as some rather strange Micrófono.

All the three above mentioned methods, including the NirSoft's, work fine on remote console (SSH, for example).

Additional points:

  • Note that this answer solves the audio device name issue only for recording/input/capture devices, so (except for the FFMPEG case, maybe) the above methods are not valid or must be modified accordingly if you search for the playing ones.