Skip to content

Instantly share code, notes, and snippets.

@SotchNam
Created November 26, 2023 18:03
Show Gist options
  • Save SotchNam/25e70930629cf69c509b87ff58e60514 to your computer and use it in GitHub Desktop.
Save SotchNam/25e70930629cf69c509b87ff58e60514 to your computer and use it in GitHub Desktop.
Turning your android phone into webcam on linux:

Using a combination of open-source software tools like adb, v4l2, and scrcpy, it is possible to utilize an Android phone's camera as a webcam for various applications.

Installing dependencies:

Since I am using arch with pacman as package manager, my command would be:

sudo pacman -S scrcpy v4l2loopback-dkms ffmpeg android-tools

This will vary depending on your distrobution.

Connecting your phone using adb:

You can either do this wirelessly or using usb cable: First of all make sure to enable usb debugging in developpers settings.

  • Using usb cable: Straight forward method, plug your phone with your usb cable. There should be a popup asking for permision for usb debugging, accept it.
  • Using wifi: First with the phone connnected using the usb cable write:
adb tcpip 5555  #port number is optional

Then have your computer and phone connected on the same network (common wifi or hotspot). Find your phone's ip address on the network then do:

adb connect {YOUR PHONE IP}:5555  #change port to what you choose earlier

Loading v4l2:

Simply load the module to the kernel by:

sudo modprobe v4l2loopback

Find out your device ID:

Find the last number presented in the available devicecs using:

ls /dev/video*

Or be fancy and use v4l2-utils:

v4l2-ctl --list-devices

Send camera output to stream:

Using scrcpy, we can stream our camera output (or display ) to v4l2 by:

scrcpy --video-source=camera --v4l2-sink=/dev/videoN  # replace N with the ID you found earlier
scrcpy --video-source=camera --no-audio --camera-size=640x480 --no-video-playback --v4l2-sink=/dev/videoN -b 2M # You can use lots of options, check scrcpy docs

View result:

We will be using ffmpeg command ffplay to preview our camera output:

ffplay /dev/videoN

Have fun!

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