We have all been there, being prepared for your project presentation, and when the panel sees you, you end up looking terrible even in your formal attire. I’m looking at you, freaking webcam! This guide will walk you through the steps required to turn your phone into a webcam. If an old phone is lying around, this solution makes more sense than investing in a better webcam which would be still worse than your average smartphone.
This guide is targeted to Ubuntu 20.04 and would work with most Linux systems.
We'll be making use of scrcpy along with V4L2. Scrcpy is a free and open-source application that provides display and control of Android devices connected on USB (or over TCP/IP). It does not require any root access.
Steps
1. Install scrcpy (Version >= 0.18),
V0.18 added support for V4L2 so that the screen can be mirrored and can be used as a webcam. Scrcpy can be installed through the snap store, but currently, it is still on v0.17. So we can compile from the source. If v0.18 is available from the Snap store while you read this guide, better make use of it.
-
Install dependencies,
sudo apt install ffmpeg libsdl2-2.0-0 adb wget gcc git pkg-config meson ninja-build libsdl2-dev libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev
-
Clone the repository and install scrcpy,
git clone https://github.com/Genymobile/scrcpy cd scrcpy ./install_release.sh
-
Run scrcpy,
scrcpy
Note: You have to enable USB Debugging from the Developer Options and authorize connection while connecting your phone to the desktop, else scrcpy won't run
For more info about the build process, check out their simplified instructions.
2. Create a dummy device to stream the mirrored screen to v4l2-sink.
-
Install dependencies,
sudo apt install v4l2loopback-dkms v4l2loopback-utils
-
Now create a Dummy Video Device,
sudo modprobe v4l2loopback
-
Check for the created device,
v4l2-ctl --list-devices
Note: /dev/video0 and /dev/video1 is my laptop's webcam.
-
/dev/video2 is the added node, so let’s run scrcpy with that node as the sink.
scrcpy --v4l2-sink=/dev/video2
Use something like Google Meet or Nextcloud Talk to select the created webcam and check whether everything is working as intended.
3. Crop the mirrored screen
The live camera feed on your phone also has additional settings on top and bottom, this can be cropped by using some parameters showcased in the docs.
scrcpy --lock-video-orientation=3 --v4l2-sink=/dev/video2 --turn-screen-off --stay-awake --power-off-on-close --crop 900:1440:0:115
This may change according to your device and the orientation of your phone, So feel free to tinkle around with the parameters, especially the crop. To avoid the creation of mirrored window, --no-display flag can be used.
Miscellaneous
-
Using scrcpy just for screen mirroring.
scrcpy --turn-screen-off --stay-awake --show-touches --power-off-on-close --window-x 1480 --window-y 200 --window-height 640
-
If required, the dummy device can be removed by,
sudo modprobe v4l2loopback -r
Verify using,
v4l2-ctl --list-devices
Take it one notch further by making a phone stand and adding keyboard shortcuts for ease of use.
-
Added keyboard shortcut in Gnome Control Center (Settings) to run scrcpy as a webcam.
- This adjustable stand from Thingiverse made by Murray Clark, felt simple and worked well.
Phew! That was kind of easy. I wish there was a GUI to do all of these, but still, scrcpy is so cool. Feel free to share your thoughts about the same, and do share if there're issues, in the comment section below.