add script to stream a v4l2 device

This commit is contained in:
douzeb 2023-03-01 11:38:10 +01:00
parent d0a5fa5f91
commit f0278ea602

96
stream_v4l_device.sh Executable file
View file

@ -0,0 +1,96 @@
#!/bin/bash
DURATION=300
RESOLUTION=1280x720
VBITRATE=2048
ABITRATE=128
V4LDEVICE=/dev/video0
if [[ $# -lt 3 ]] ; then
echo "Not enough arguments supplied" >&2
echo "You need to pass at least the PeerTube server name as the " >&2
echo "first parameter the rtmp port as the second and the streaming key as the third" >&2
echo " " >&2
echo "syntax: " >&2
echo " $(basename $0) <peertube-server> <rtmp port> <streaming-key> <duration-in-sec> <resolution> <video-bitrate-kbps> <audio-bitrate-kbps> <v4ldevice>" >&2
echo " " >&2
echo "other parameters are optional and will default to the " >&2
echo "values in the following example :" >&2
echo " `basename $0` tube.example.com 1935 69ac2d29-eba4-4d5f-a3a6-6047e6768ffb $DURATION $RESOLUTION $VBITRATE $ABITRATE $V4LDEVICE" >&2
exit 1
fi
URL=rtmp://$1:$2/live/$3
if [[ ! -z "$4" ]]
then
DURATION="$4"
fi
if [[ ! -z "$5" ]]
then
RESOLUTION="$5"
fi
if [[ ! -z "$6" ]]
then
VBITRATE="$6"
fi
BUFFERSIZE=$(echo "2*${VBITRATE}" | bc)
if [[ ! -z "$7" ]]
then
ABITRATE="$7"
fi
if [[ ! -z "$8" ]]
then
V4LDEVICE="$8"
fi
FILENAME=live-v4l-${1}-${V4LDEVICE}-${RESOLUTION}p-${DURATION}s-${VBITRATE}kbps-${ABITRATE}kbps-${2}.mkv
echo " "
echo Will stream to $URL for $DURATION seconds
echo and save a copy of the data sent to the peertube server to file :
echo $FILENAME
echo with :
echo "- $RESOLUTION resolution, "
echo "- $VBITRATE kbit/s constant video bitrate, "
echo "- $BUFFERSIZE kbit video buffer and "
echo "- $ABITRATE kbit/s audio bitrate"
echo " "
$
#-x264opts nal-hrd=cbr:force-cfr=1:vbv-bufsize=${BUFFERSIZE}:bitrate=${VBITRATE}:vbv-maxrate=${VBITRATE} \
#-shortest \
ffmpeg \
-loglevel level \
#-s ${RESOLUTION} \
#-loop 1 \
#-r 25 \
-y \
#-ac 2 \
-f v4l2 -framerate 25 -video_size ${RESOLUTION} -i ${V4LDEVICE}
#-re \
#-f lavfi \
#-i aevalsrc="(1-min(1\,mod(floor(5*t)\,50)))*0.1*sin(8*PI*(360-2.5/2)*t)+0.1*sin(2*PI*(360-2.5/2)*t)|(1-min(1\,mod(floor(5*t)\,50)))*0.1*sin(8*PI*(360+2.5/2)*t)+0.1*sin(2*PI*(360+2.5/2)*t):sample_rate=48000" \
-f alsa -ac 2 -i hw:CARD=CAMERA,DEV=0
-channel_layout stereo \
-ar 48000 \
-pixel_format yuv420p \
-filter_complex "[1:a]volume=-10dB;[0:v]scale='size=${RESOLUTION}',drawtext='font=DejaVuSansMono:text=%{pts}: fontcolor=white: fontsize=h/7: box=1: boxcolor=black@0.73: boxborderw=5: x=mod(floor(t/10+1)\,2)*mod(t*(w-text_w)/10\,w-text_w)+mod(floor(t/10)\,2)*(w-text_w-mod(t*(w-text_w)/10\,w-text_w)): y=h/2-3*text_h/2', drawtext='font=DejaVuSansMono:text=%{localtime}: fontcolor=white: fontsize=h/7: box=1: boxcolor=black@0.73: boxborderw=5: x=(w-text_w)/2: y=h/2+3*text_h/2'" \
-codec:v libx264 \
-preset superfast \
-x264opts nal-hrd=cbr:force-cfr=1:vbv-bufsize=${BUFFERSIZE}:bitrate=${VBITRATE}:vbv-maxrate=${VBITRATE} \
-c:a aac -b:a ${ABITRATE}k \
-t ${DURATION} \
-flags +global_header \
-f tee -map 0:v -map 1:a "[f=matroska]${FILENAME}|[f=flv]${URL}"