Skip to content

Instantly share code, notes, and snippets.

@kkavod
Last active May 14, 2021 21:07
Show Gist options
  • Save kkavod/3cd3145e9547e6f18852 to your computer and use it in GitHub Desktop.
Save kkavod/3cd3145e9547e6f18852 to your computer and use it in GitHub Desktop.
Captures a window and creates a virtual camera from it, uses GStreamer 1.0 on Linux
#!/bin/bash
# This pipeline is based on https://gist.github.com/lifning/1857803
# but is updated for GStreamer 1.0 and also crops the window
# Tested on Chrome with https://github.com/bluetiger9/v4l2loopback
# with exclusive_caps enabled:
# $ sudo modprobe v4l2loopback devices=8 exclusive_caps=1
DEV=$(zenity --file-selection \
--filename=/dev/video0 \
--file-filter='V4L2 loopback devices | video*')
echo "Select a window to capture..."
# Could be refactored to use one xwininfo call instead of 3
XID=$(xwininfo -int | grep 'Window id' | awk '{print $4}')
WW=$(xwininfo -id $XID | grep 'Width' | awk '{print $2}')
WH=$(xwininfo -id $XID | grep 'Height' | awk '{print $2}')
# camera width/height
CW=640
CH=360
# start x/y padding
SX=20
SY=40
# end x/y padding
EX=20
EY=55
echo endx=$[$CW-$EX] endy=$[$CH-$EY]
gst-launch-1.0 --eos-on-shutdown \
\
ximagesrc use-damage=false show-pointer=false xid=$XID startx=$SX starty=$SY endx=$[$WW-$EX] endy=$[$WH-$EY] \
! videoconvert \
! videoscale method=1 add-borders=true \
! video/x-raw,width=$CW,height=$CH \
! videorate \
! tee name=displaypipe \
! tee name=recordpipe \
! queue \
! v4l2sink device=$DEV \
\
displaypipe. \
! queue \
! autovideosink \
exit
@kkavod
Copy link
Author

kkavod commented Mar 19, 2016

Screenshot:
screencamera

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