Skip to content

Instantly share code, notes, and snippets.

@telamon
Last active October 2, 2023 02:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telamon/23e5362757339bc8f22e9d40f9f1bd8e to your computer and use it in GitHub Desktop.
Save telamon/23e5362757339bc8f22e9d40f9f1bd8e to your computer and use it in GitHub Desktop.
Wayland screenshare script via wf-recorder and v4l2loopback
#!/bin/bash
# Preliminary steps (void linux):
# sudo xbps-install -Su v4l2loopback wf-recorder
# (don't forget to reboot if new Kernel version was upgraded)
#
# Too lazy to autodetect active monitor resolution
WIDTH=2560
HEIGHT=1440
# Detect and load kernel module if needed
lsmod | grep v4l2loopback \
&& echo "Kernel module detected" \
|| ( \
echo "Need to modprobe v4l2loopback kernel module, please authorize:" \
&& sudo modprobe v4l2loopback \
)
# List devices and exit when run without args
if [ -z "$1" ]; then
v4l2-ctl --list-devices
echo "Please rerun sharescreen with the device device path of the Dummy listed above"
echo "Ex: sharescreen /dev/video4"
exit 1
fi
device=$1
# Let device timout to prevent device-busy errors
# v4l2-ctl -d $device -c timeout=3000 # unverified
# Set capabilities for device, so chrom*/webrtcvideocapturer.cc
# based software like discord is happy.
#
# https://gstreamer.freedesktop.org/documentation/video/video-format.html?gi-language=c#GstVideoFormat
# GST_VIDEO_FORMAT_YUY2 (4) – packed 4:2:2 YUV (Y0-U0-Y1-V0 Y2-U2-Y3-V2 Y4 ...)
# Suspicion that matching GST format is `YUY2` is further backed
# by output of `gst-device-monitor-1.0` and finding section of webcam hardware
# that's works in chosen software
v4l2loopback-ctl set-caps \
"video/x-raw, format=YUY2, width=$WIDTH, height=$HEIGHT" \
$device || \
echo "WARN; Failed to set format, rmmmod / modprobe to free device?"
##
# In case your v4l2loopback utility is a different version, from what I can
# dump, the significant commands my version executes is:
#
# v4l2-ctl -d /dev/video4 -c keep_format=1
# v4l2-ctl -d /dev/video4 -c sustain_framerate=1
# gst-launch-1.0 videotestsrc num-buffers=1 '!' video/x-raw,format=YUY2,width=2560,height=1440 '!' v4l2sink device=/dev/video4
#
# Start screen-capture and dump the frames into chosen loopback-devices
wf-recorder --muxer=v4l2 --file=$device \
-c rawvideo -x yuyv422
@ahmadraniri
Copy link

Thanks. It works.

@telamon
Copy link
Author

telamon commented Oct 2, 2023

cheers

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