Skip to content

Instantly share code, notes, and snippets.

@frank-dspeed
Last active August 30, 2023 23:39
Show Gist options
  • Save frank-dspeed/8d29e6b2808c636e86c3150b2997ccbf to your computer and use it in GitHub Desktop.
Save frank-dspeed/8d29e6b2808c636e86c3150b2997ccbf to your computer and use it in GitHub Desktop.
Cheatsheet Conten Production Streaming Linux Virtual Desktop Webcam

Linux Video

ffmpeg - is the default standard for video operations of all kind

drawtext filter

If you just want to update some text on the screen the easiest method is to use the drawtext filter with the textfile and reload options.

ffmpeg -i input -vf "drawtext=textfile=songs.txt:reload=1" output songs.txt will be reloaded once per frame. Be sure to update it atomically, or it may be read partially, or even fail.

overlay filter

If you prefer an image instead of text you have to manually select the image demuxer, loop the image, and replace overlay.png atomically whenever you want it to be updated.

ffmpeg -i input -f image2 -loop 1 -i overlay.png -filter_complex overlay output

Virtual Webcam

You can do this with v4l2loopback. First you need to install it:

Install v4l2loopback

Method 1: Install v4l2loopback from the repository

sudo apt install v4l2loopback-dkms
sudo modprobe v4l2loopback

This is easy but older versions of v4l2loopback have some known bugs, so consider compiling it instead if you encounter any issues.

Method 2: Compile v4l2loopback

If it's not in the repository for your Ubuntu version, or you want the latest version, you can compile it:

sudo apt-get install build-essential checkinstall
wget https://github.com/umlaeute/v4l2loopback/archive/master.zip
unzip master.zip
cd v4l2loopback-master
make
sudo checkinstall --pkgname=v4l2loopback --pkgversion="$(date +%Y%m%d%H%M)-git" --default
sudo modprobe v4l2loopback

Uninstalling

If you want to remove the package you compiled:

sudo apt-get remove v4l2loopback

Examples

Note that the actual video number may vary depending if an existing device is already using /dev/video0. Check output of ls /dev/video* or v4l2-ctl --list-devices.

Desktop to virtual camera

Now run ffmpeg. Example for desktop using x11grab:

ffmpeg -f x11grab -framerate 15 -video_size 1280x720 -i :0.0 -f v4l2 /dev/video0

Video file (MP4) to virtual camera

ffmpeg -re -i input.mp4 -map 0:v -f v4l2 /dev/video0

Image to virtual camera

ffmpeg -re -loop 1 -i input.jpg -vf format=yuv420p -f v4l2 /dev/video0

Webcam → ffmpeg → Virtual webcam

Such as if you want to do some filtering. This example will flip the image vertically.

ffmpeg -f v4l2 -i /dev/video0 -vf vflip -f v4l2 /dev/video1

If you get error Unknown V4L2 pixel format equivalent then add the output option -vf format=yuv420p.

Preview with ffplay

ffplay /dev/video0

Common errors

  • Unable to open V4L2 device '/dev/video0'
  • Failed to open /dev/video0: No such file or directory
  • Unknown V4L2 pixel format equivalent for yuvj422p

See this answer for solutions.

Virtual Desktops

You can create a virtual desktop with Xvfb and stream that. To create a virtual X client and server:

$ Xvfb :100 -screen 0 800x600x24 & To open an X applications inside of the virtual client, set the $DISPLAY variable:

$ DISPLAY=:100 feh ~/pictures/stallman.jpg Now you can stream a video feed of the virtual X client to a server using ffmpeg and netcat:

$ ffmpeg -f x11grab -s 800x600 -r 30 -i :100 -an -q 10 -f mjpeg - | nc -lp 5000 And receive the video on another device on the LAN and play it back with ffplay:

$ nc 5000 | ffplay -

You can send keyboard events to the virtual X client with xdotool or xmacro. xmacro has a flag that allows it to receive stdin, as well as a recording tool that spits stdout. This can be combined with netcat to send key and mouse events.

$ xmacrorec | nc -lp 9002 and nc 192.168.100.100 9002 | xmacroplay :0

Linux Audio

pavucontrol - gui client that can route audio

snd-aloop - is the kernel module for setting up virtual audio loopback devices.

$ sudo modprobe snd-aloop

creates two devices 0 & 1 under a new “Loopback” card for both playback & capturing, as shown below, respectively:

$ sudo aplay -l $ sudo arecord -l

Start recording audio from hw:2,1,4:

$ arecord -D hw:2,1,4 -f S16_LE -c 2 -r 48000 recorded.wav

Note that providing the sample format, channel count, frame rate in recording ensures that playback picks up the same settings – this is because there is no real hardware underneath it is just a virtual loopback connection.

And in parallel (from another shell) play an audio from audio.wav into hw:2,0,4:

$ aplay -D hw:2,0,4 audio.wav And you’d find that recorded audio contains the played one – a loopback in action. You may play the recorded audio as follows:

$ aplay recorded.wav This would play on your system’s default speaker.

Also, note that there may be problem in just playing any audio.wav file because of the mismatched audio format etc support. For that, just record a new wave file with your speech using the following command:

$ arecord -f S16_LE -c 2 -r 48000 audio.wav This would record from your system’s default mic.

Interestingly, audio loopback could also be achieved in user space using alsaloop from alsa-utils package. Here is a demo of the same. From the output of aplay -l, hw:1,0 is the analog out (speaker). Note that hw:1,0 is same as hw:1,0,0. Find the equivalent on your system. And, now let’s loopback the virtual audio capture device hw:2,1,4 to this:

alsaloop -C hw:2,1,4 -P hw:1,0 On another shell, do the earlier playing:

$ aplay -D hw:2,0,4 audio.wav

This time you should be able to hear the audio.wav directly through system’s default speaker – again a loopback in action – rather two loopbacks in action: audio.wav -> hw:2,0,4 -> (loopback through snd-aloop driver) -> hw:2,1,4 -> (loopback through alsaloop app) -> hw:1,0 -> heard on speaker.

virtual monitor

https://askubuntu.com/questions/453109/add-fake-display-when-no-monitor-is-plugged-ingist

Interristing obs addons that could be used more direclty with ffmpeg

exporting a obs source to diffrent devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment