home-assistant-libs / ha-ffmpeg

A python library that handling with ffmpeg for home-assistant
BSD 3-Clause "New" or "Revised" License
17 stars 13 forks source link

Support for microphone direct input #30

Open zhulik opened 4 years ago

zhulik commented 4 years ago

This is how I can easily use ffmpeg to detect noise using a microphone directly: ffmpeg -f alsa -i hw:0,0 -af silencedetect=n=-40dB:d=1 -f null -

It seems like it is impossible to build such command using ffmpeg_noise HA integration, because haffpeg always puts input to the first place.

This is what I tried:

- platform: ffmpeg_noise
  input: "hw:0,0"
  extra_arguments: -f alsa
  initial_state: true
  duration: 1
  reset: 20
  peak: -40

And it didn't work because the command haffmpeg built was 'ffmpeg', '-i', 'hw:0,0', '-vn', '-f', 'alsa', '-filter:a', 'silencedetect=n=-40dB:d=1', '-f', 'null', '-']

As you can see, -f alsa follows after -i hw:0,0, so ffmpeg can't recognize the input.

I know that it's possible to stream data from microphone using rtp and then process it as described here, but I also know that it adds a lot of complications to the systemd configuration and it is not very efficient because the streaming constantly uses 10-15% of my CPU.

How difficult it would be to add support for direct microphone input?

black-roland commented 4 years ago

@zhulik I'm not sure if this is still relevant :slightly_smiling_face: but you can try passing alsa related arguments to the input option. For example, I'm using this config with my mic:

- platform: ffmpeg_noise
  input: -f alsa -ac 1 -i hw:1,0
  extra_arguments: '-hide_banner -nostats -filter:a highpass=f=6000'
  peak: -22
  reset: 20
zhulik commented 4 years ago

@black-roland Thanks, I'll try it

stskrp commented 3 years ago

Works for me with such configuration:

 - platform: ffmpeg_noise
    input: -f alsa -channels 1 -i hw:1  
    extra_arguments: -filter:a highpass=f=99,lowpass=f=250  
    initial_state: true  
    duration: 2  
    reset: 3  
    peak: -45

But there is an issue, it stops working after some time (~1h), nothing in logs, binary_sensor is all time in "Clear" state, and microphone itself is in busy state (means process is running, but not piped to Home Assistant?) This is output when I try to run ffmpeg command in HASS container cmd: [alsa @ 0x7f8ddd7940] cannot open audio device hw:1 (Resource busy) hw:1: I/O error