Skip to content

Instantly share code, notes, and snippets.

@vpalmisano
Last active May 18, 2020 17:26
Show Gist options
  • Save vpalmisano/17c3647e497629da5ec4aa9c15410a42 to your computer and use it in GitHub Desktop.
Save vpalmisano/17c3647e497629da5ec4aa9c15410a42 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Usage:
# ./v4l2loopback_script start <videosource> [<total fake sources> <width> <height> <framerate>]
#
set -ex
function start() {
local SRC=${1}
local TOTAL=${2:-1}
local WIDTH=${3:-1280}
local HEIGHT=${4:-720}
local FRAMERATE=${5:-25}
local VIDEO_NRS=""
local CARD_LABELS=""
local EXCLUSIVE_CAPS=""
local SET_FPS_COMMANDS=""
local FFMPEG_DEST=""
local START_NR=$(ls /dev/video* | wc -l)
local DRAWTEXT="drawtext=fontfile=/usr/share/fonts/truetype/freefont/FreeSans.ttf:fontcolor=white:fontsize=60:x=0:y=0:box=1:boxcolor=black"
for (( i=${START_NR}; i < $[START_NR + TOTAL]; i++ )); do
if [ -z "${VIDEO_NRS}" ]; then
VIDEO_NRS=${i}
else
VIDEO_NRS=${VIDEO_NRS},${i}
fi
if [ -z "${CARD_LABELS}" ]; then
CARD_LABELS="Fake Video #${i}"
else
CARD_LABELS="${CARD_LABELS},Fake Video #${i}"
fi
if [ -z "${EXCLUSIVE_CAPS}" ]; then
EXCLUSIVE_CAPS=1
else
EXCLUSIVE_CAPS=${EXCLUSIVE_CAPS},1
fi
if [ -z "${SET_FPS_COMMANDS}" ]; then
SET_FPS_COMMANDS="sudo v4l2loopback-ctl set-fps ${FRAMERATE} /dev/video${i}"
else
SET_FPS_COMMANDS="${SET_FPS_COMMANDS} && sudo v4l2loopback-ctl set-fps ${FRAMERATE} /dev/video${i}"
fi
FFMPEG_DEST="${FFMPEG_DEST} -vf ${DRAWTEXT}:text=${i} -s ${WIDTH}:${HEIGHT} -r ${FRAMERATE} -pix_fmt yuv420p -vcodec rawvideo -f v4l2 /dev/video${i}"
done
sudo /sbin/modprobe v4l2loopback max_buffers=2 \
video_nr=${VIDEO_NRS} \
card_label="${CARD_LABELS}" \
exclusive_caps="${EXCLUSIVE_CAPS}"
sleep 1
${SET_FPS_COMMANDS}
ffmpeg -re -stream_loop -1 -i "${SRC}" ${FFMPEG_DEST}
sudo rmmod v4l2loopback
}
function stop() {
sudo rmmod v4l2loopback
}
$@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment