4

How to create a virtual pulseaudio microphone with ffmpeg?

I have a mkv file and with v4l2 I am able to redirect the video stream to a virtual webcam device, here /dev/video0.

ffmpeg -i myfile.mkv -f v4l2 /dev/video0

Now, in the same time I want to redirect the audio stream to a virtual pulseaudio microphone (and not to an output device). How can I achieve this?

azmeuk
  • 4,026
  • 3
  • 37
  • 64

1 Answers1

12

Create a virtual output device, and a virtual source from it's monitor.

pactl load-module module-null-sink sink_name="virtual_speaker" sink_properties=device.description="virtual_speaker"
pactl load-module module-remap-source master="virtual_speaker.monitor" source_name="virtual_mic" source_properties=device.description="virtual_mic"

Any sound played to "virtual_speaker" will be sent to "virtual_mic".

You can play sound directly from ffmpeg with -f pulse, but using this together with -re (which you want for video playback to a virtual webcam) seems to add a couple of seconds of delay to the audio...

In my testing, this also made the video output laggy (still showing it was running at 1x). The only way I've been able to get smooth audio and video is by running two ffmpeg instances in parallel... Not a great solution.

ffmpeg -nostdin -re -i myfile.mkv -f v4l2 /dev/video0 &
PULSE_SINK=virtual_speaker ffmpeg -i myfile.mkv -f pulse "stream name"
kill $!
  • -nostdin is needed for it to run in background.
  • PULSE_SINK=virtual_speaker can be used with any audio player.
  • "stream name" is just a display name the audio stream will get.
  • kill $! will kill the video encoder, if the audio encoder exits.
alex232
  • 121
  • 1
  • 3