dots

Personal dotfiles
git clone git://git.gormless.xyz/dots.git
Log | Files | Refs

rec (4122B)


      1 #!/bin/sh
      2 
      3 # Copyright (c) 2018-2019 Thomas Frohwein <11335318+rfht@users.noreply.github.com>
      4 # Permission to use, copy, modify, and distribute this software for any
      5 # purpose with or without fee is hereby granted, provided that the above
      6 # copyright notice and this permission notice appear in all copies.
      7 
      8 USAGE="Usage: `basename $0` [-m] [-d mic_device] [-a audiooffset] [-n window_name] [-c codec] [-b bitrate] [-r resolution] [-o offset] [-fullscreen] [-s <scale_x>:<scale_y>] [-f framerate] [-v volume_adjustment] [-vmic mic_volume] [-vmon monitor_volume] [-t threads ] [-noaudio] [-crf crf] <rtmp url>"
      9 
     10 audiooffset=0.0
     11 bitrate=3500
     12 bufsize=
     13 container=mp4
     14 filename=
     15 framerate=30
     16 fullscreen=0
     17 gop=
     18 heightline=
     19 heightval=
     20 hilopass=",lowpass=3000,highpass=200,afftdn"
     21 mic=0
     22 mic_device=snd/0
     23 noaudio=0
     24 offset=
     25 resolution="$(xdpyinfo | grep dimensions | awk '{print $2;}')"
     26 scaleres=
     27 threads=0	# -t 0 is the default and uses as many threads as available/cores
     28 videocodec=libx264
     29 volume=1.0
     30 volume_mic=1.0
     31 volume_mon=1.0
     32 windowname=
     33 widthline=
     34 widthval=
     35 wininfoargs=
     36 xline=
     37 xval=0
     38 yline=
     39 yval=0
     40 
     41 if [[ $# -eq 0 ]];then
     42 	echo $USAGE; exit 0
     43 fi
     44 
     45 while [[ $# -gt 0 ]]
     46 do
     47 	case "$1" in
     48 		-a) audiooffset="$2"; shift;;
     49 		-b) bitrate="$2"; shift;;
     50 		-c) videocodec="$2"; shift;;
     51 		-container) container="$2"; shift;;
     52 		-crf) crf="$2"; shift;;
     53 		-d) mic_device="$2"; shift;;
     54 		-f) framerate="$2"; shift;;
     55 		-fullscreen) fullscreen=1;;
     56 		-h) echo $USAGE; exit 0;;
     57 		-hl) hilopass="";;
     58 		-m) mic=1;;
     59 		-n) windowname="$2"; shift;;
     60 		-noaudio) noaudio=1;;
     61 		-o) offset="$2"; shift;;
     62 		-v) volume="$2"; shift;;
     63 		-r) resolution="$2"; shift;;
     64 		-s) scaleres="$2"; shift;;
     65 		-t) threads="$2"; shift;;
     66 		-vmic) volume_mic="$2"; shift;;
     67 		-vmon) volume_mon="$2"; shift;;
     68 		-*) echo $USAGE; exit 0;;
     69 		*) filename="$1"; break;;
     70 	esac
     71 	shift
     72 done
     73 
     74 if [[ -z "$filename" ]];then
     75 	echo $USAGE; exit 0
     76 fi
     77 
     78 bufsize=`expr $bitrate \* 2`
     79 gop=`expr $framerate \* 2`
     80 
     81 if [[ -n "$scaleres" ]];then
     82 	scaleres="scale=${scaleres},"
     83 fi
     84 
     85 # if -n was used, overwrite resolution with window parameters
     86 if [[ -n "$windowname" ]];then
     87 	xline=`xwininfo -name "$windowname" | grep "Absolute upper-left X"`
     88 	yline=`xwininfo -name "$windowname" | grep "Absolute upper-left Y"`
     89 	widthline=`xwininfo -name "$windowname" | grep "Width:"`
     90 	heightline=`xwininfo -name "$windowname" | grep "Height:"`
     91 	xval=`echo "$xline" | cut -f 7 -d " "`
     92 	yval=`echo "$yline" | cut -f 7 -d " "`
     93 	# width and height need to be multiples of 2
     94 	widthval=`echo "$widthline" | cut -f 4 -d " "`
     95 	if [[ `expr $widthval % 2` -eq 1 ]]; then
     96 		widthval=`expr $widthval + 1`
     97 	fi
     98 	heightval=`echo "$heightline" | cut -f 4 -d " "`
     99 	if [[ `expr $heightval % 2` -eq 1 ]]; then
    100 		heightval=`expr $heightval + 1`
    101 	fi
    102 	resolution="${widthval}x${heightval}"
    103 	offset="+${xval},${yval}"
    104 fi
    105 
    106 echo "Press Ctrl+C to stop recording\n"
    107 
    108 BASE="\
    109 -hide_banner \
    110 -loglevel error \
    111 -thread_queue_size 512 \
    112 -threads $threads"
    113 
    114 VIDEO="-video_size $resolution \
    115 -thread_queue_size 512 \
    116 -f x11grab \
    117 -i $DISPLAY$offset \
    118 -r $framerate \
    119 -c:v $videocodec \
    120 -vb ${bitrate}k \
    121 -minrate ${bitrate}k \
    122 -maxrate ${bitrate}k \
    123 -bufsize ${bufsize}k \
    124 -preset ultrafast \
    125 -vf "${scaleres}format=yuv420p" \
    126 -g $gop \
    127 -keyint_min $framerate"
    128 
    129 AUDIOMERGE="volume=$volume_mic,aformat=channel_layouts=stereo$hilopass[l];[1]volume=$volume_mon,aformat=channel_layouts=stereo[m];[l][m]amerge=inputs=2[a]"
    130 
    131 # if no mic (= no -m), only record from snd/0.mon
    132 #	and the only filter is aresample=async=1
    133 
    134 if [ $noaudio -lt 1 -a $mic -lt 1 ]; then
    135 	#only monitoring stream
    136 	ffmpeg $BASE -f sndio -i snd/0.mon -c:a aac -f nut pipe:1 | \
    137 	ffmpeg $BASE -f nut -itsoffset $audiooffset -i pipe:0 $VIDEO -c:a copy -f mp4 "$filename"
    138 elif [ $noaudio -lt 1 ]; then
    139 	#mon + mic stream
    140 	ffmpeg $BASE -thread_queue_size 512 -f sndio -i "$mic_device" \
    141 	-thread_queue_size 512 -f sndio -i snd/0.mon \
    142 	-filter_complex "[0]${AUDIOMERGE}" -map '[a]' -c:a aac -f nut pipe:1 | \
    143 	ffmpeg $BASE -thread_queue_size 512 -f nut -itsoffset $audiooffset -i pipe:0 $VIDEO -c:a copy -f mp4 "$filename"
    144 else
    145 	#no audio
    146 	ffmpeg $BASE $VIDEO -f mp4 "$filename"
    147 fi