Get windows devices names with python and sounddevice

I needed to get the mic name to avoid changing a script that used it, every time I changed the mic or just the usb port for the mic. So I found that the sounddevice module was the best for my purpose and that’s how easy is to get it and use it in a program that needs it. You can check the specs too of the devices.

So, if you need to know the name that windows uses for its devices, like a microphone for example, in your programs you can do this:

import sounddevice as sd

def get_mic_name():
    "Uses sounddevice to get the name of the mic in case it changed"
    s = sd.query_devices()
    audio = f"""{s[1]["name"]})"""
    return audio

name = get_mic_name()

So, here I am getting the name of the mic, that (in my case) is the “name” key of the second dictionary in the list of dictionaries that are made by the query_devices function class of sounddevice.

Watch the video to get an example of use of it.

The program I use to record the screen

This needs to be adapted to your needs, but I post it here in case you could take some tips out of it for your needs.

import os
import glob
import sounddevice as sd


x = 0

def ask():
    root = "G:\\video\\"
    filename = input("Nome del file: ")
    filepath = root + filename + ".mp4"
    return filepath, root

def automatic_name():
    "Crea il nome da solo / ora chiedo il nome del file prima"
    global x

    fld = "G:\\video"
    if not fld + str(x) + ".mp4" in glob.glob(fld + "*.mp4"):
        filename = fld + str(x) + ".mp4"
    else:
        x += 1
        record()
    filename = fld + filename + ".mp4"
    return filename


def get_mic_name():
    "Uses sounddevice to get the name of the mic in case it changed"
    s = sd.query_devices()
    audio = f"""{s[1]["name"]})"""
    return audio


def record(filename):
    # audio = "Microfono (Logitech USB Headset)"
    # audio = "Stereo Mix (Realtek High Definition Audio(SST))"
    audio = get_mic_name()
    print(f"Using {audio}")
    i = f"-i audio=\"{audio}\""
    # video_size = "1366x768"
    video_size = "1680x1050"
    
    # added a compressor 15/03/2020
    # Capture both screens
    # i = f"-i video=\"UScreenCapture\":audio=\"{audio}\""
    #Capture one screen
    #screen = "UScreenCapture"
    #screen = "gdigrab"
    screen = "UScreenCapture"

    compressor = "-af acompressor=threshold=0.089:ratio=9:attack=200:release=1000"
    # attenuate 10db at 1000 Hz
    # equalizer = "-af \"equalizer=f=1000:width_type=h:width=200:g=-10\""
    # Apply 2 dB gain at 1000 Hz with Q 1 and attenuate 5 dB at 100 Hz with Q 2:
    # equalizer = "-af \"equalizer=f=1000:t=q:w=1:g=2,equalizer=f=100:t=q:w=2:g=-5\""

    #lowpass at 1000
    b0 = -8
    b250 = 4
    e1000 = -8
    e4000 = 0
    e16000 = -8
    equalizer = f"-af \"firequalizer=gain_entry='entry(0,{b0});entry(250,{b250});entry(1000,{e1000});entry(4000,{e4000});entry(16000,{e16000})'\""

    # echo = "-af aecho=1:0.3:10:1"
    # echo = "-af aecho=1.0:0.7:10:0.5"
    echo = ""

    # fr = input("Frame rate (10):")
    fr = 10
    os.system(f"""ffmpeg -y -rtbufsize 200M -f gdigrab -thread_queue_size 1024 -probesize 10M -r {fr} -draw_mouse 1 -video_size {video_size} -i desktop -f dshow -channel_layout stereo -thread_queue_size 1024 {i} {compressor} {equalizer} -c:v libx264 -r 10 -preset ultrafast -tune zerolatency -crf 25 -pix_fmt yuv420p -c:a aac -strict -2 -ac 2 -b:a 128k -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" {echo} "{filename}" """)


# filename = "rtmp://youtube_stream_url/03r8-71q2-yrvm-bbe7"
filepath, root = ask()
print(root)




def mk_bat(cmd):
    command = "py -m " + cmd
    with open(root + cmd + ".bat", "w") as file:
        file.write(command)


# # Create the join.bat program
# command = "py -m joinmp4"
# with open(root + "join.bat", "w") as file:
#     file.write(command)

# create the frame rate program
mk_bat("joinmp4")
mk_bat("framerate")



with open("c:\\users\\giova\\desktop\\ffmpeg_open.bat", "w") as file:
    file.write("start " + root)


record(filepath)
# os.startfile("timer.py")

# TO OPEN THE FOLDER 
os.system("start " + root)

 


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.