Featured image of post Creating a Virtual Camera for Conferences

Creating a Virtual Camera for Conferences

Instead of showing yourself in the conference, we can show video or static images

We don’t always want to include a camera during the conference, so we found a way to display video or static images instead. The means of OS or browser doesn’t provide this out of the box. I found this article and, indeed, created a virtual camera module that is recognized by browsers as an ordinary camera 😄

Below is a script with detailed comments. The only argument to it is the path to the file or image that should be transmitted in the stream.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env sh
set -e

# label for the new webcam as it will be seen in browser
LABEL="VirtualCam #0"

# module insertion in case if it was not inserted during this boot
if ! lsmod | grep -qE '^v4l2loopback'; then
  # camera is being created during the module loading
  sudo modprobe v4l2loopback devices=1 exclusive_caps=1 max_buffers=2 card_label="$LABEL"
  # wait for a while to finish module initialization
  sleep 1
fi

# path to media file
F="${1:?File is not passed}"
# getting path to newly created camera device
DEV="$(v4l2-ctl --list-devices 2>/dev/null | grep -A1 "$LABEL" | grep -oE '/dev/video[0-9]+')"

if [ ! -f "$F" ]; then
  # check in case if media file path does not exists
  echo "error: file $F does not exists" >&2
  exit 1
fi

# get file type and use proper streaming command
case "$(file --mime-type --brief "$F" | cut -d/ -f1)" in
  video)
    # in case if file is video
    ffmpeg -re -i "$F" -f v4l2 -vcodec rawvideo -pix_fmt yuv420p "$DEV"
    ;;
  image)
    # in case if file is picture
    ffmpeg -loop 1 -re -i "$F" -f v4l2 -vcodec rawvideo -pix_fmt yuv420p "$DEV"
    ;;
  *)
    # in case if file is something else
    echo 'error: unknown mime type' >&2
    exit 2
    ;;
esac

So, in the end, we were able to stream Rick Ashley - Never Gonna Give You Up.

One notable drawback is that FFmpeg consumes almost an entire core. Additionally, we are, of course, only transmitting the image, without any sound.

Licensed under Apache License, Version 2.0
Last updated on Nov 19, 2024 10:58 +0200
All rights reserved
Built with Hugo
Theme Stack designed by Jimmy