summaryrefslogtreecommitdiff
path: root/bin/stream
blob: 63952258bbdf6a246f11a6d14a1346f627b34e44 (plain)
  1. #!/bin/sh
  2. #
  3. # TODO: Set predictive GOP size for WebM (when tested that it works)
  4. set -eu
  5. # TODO: Externalize to site-specific configfile
  6. [ $# -gt 0 ] || set -- morla 5002 -- dvcam ../../content/icon_small.png
  7. ##
  8. ## defaults
  9. ##
  10. RATIO=4:3
  11. ##
  12. ## generic functions
  13. ##
  14. exit1() {
  15. echo >&2 "ERROR: $1"
  16. exit 1
  17. }
  18. # shellcheck disable=SC2048,SC2059
  19. echo_n() {
  20. printf -- "$*"
  21. }
  22. # shellcheck disable=SC2048,SC2059
  23. printf_each() {
  24. skel=$1; shift
  25. for string in $*; do
  26. printf -- "$skel" "$string"
  27. done
  28. }
  29. uniqwords() {
  30. echo_n "$@" | tr ' ' '\012' | sort -u
  31. }
  32. valuedargcount() {
  33. nonempty=
  34. while [ $# -gt 0 ]; do
  35. [ -z "$1" ] || nonempty=$((nonempty+1))
  36. shift
  37. done
  38. echo_n "$nonempty"
  39. }
  40. while [ $# -gt 0 ]; do
  41. case $1 in
  42. --)
  43. shift; break;;
  44. *)
  45. if [ -z "${HOST:-}" ]; then
  46. HOST=$1
  47. elif [ -z "${FIRSTPORT:-}" ]; then
  48. FIRSTPORT=$1
  49. else
  50. exit1 "Too many arguments: Max. 2 about target"
  51. fi
  52. ;;
  53. esac
  54. shift
  55. done
  56. # TODO: Externalize to site-specific configfile
  57. [ $# -gt 0 ] || set -- dvcam ../../content/icon_small.png
  58. AINPUT=
  59. VINPUT=
  60. WINPUT=
  61. XINPUT=
  62. while [ $# -gt 0 ]; do
  63. case $1 in
  64. alsa=*) ALSA=${1#*=}; AINPUT=$((AINPUT+1));;
  65. alsa) ALSA=default; AINPUT=$((AINPUT+1));;
  66. dvcam=*) DVCAM=${1#*=}; DEINT=${DEINT:-yadif}; XINPUT=$((XINPUT+1));;
  67. dvcam) DVCAM=auto; XINPUT=$((XINPUT+1));;
  68. dc=*) IIDC=${1#*=}; VINPUT=$((VINPUT+1));;
  69. dc) IIDC=/dev/fw1; VINPUT=$((VINPUT+1));;
  70. videofile=*) VFILE=${1#*=}; VINPUT=$((VINPUT+1));;
  71. *.ffv1|*.yuv|*.vp8|*.vp9) VFILE=$1; VINPUT=$((VINPUT+1));;
  72. container=*) XFILE=${1#*=}; XINPUT=$((XINPUT+1));;
  73. *.avi|*.dv|*.mkv|*.mov|*.mp4|*.nut|*.ogg|*.ogv|*.webm) XFILE=$1; XINPUT=$((XINPUT+1));;
  74. *.png) LOGO=$1; WINPUT=$((WINPUT+1));;
  75. --) shift; break;;
  76. *) exit1 "Unsupported input: $1";;
  77. esac
  78. shift
  79. done
  80. HOST=${HOST:-127.0.0.1}
  81. if [ "$HOST" = "$(hostname --short)" ]; then
  82. IP=127.0.0.1
  83. else
  84. IP=$(host "$HOST" | grep -Po 'address \K\S+')
  85. fi
  86. [ -n "$AINPUT$VINPUT$XINPUT" ] || exit1 "Too few arguments: Min. 1 A/V source"
  87. [ -z "$AINPUT" ] || [ -z "$VINPUT" ] || [ -z "$XINPUT" ] || exit1 "Too many arguments: Max. 2 A/V sources"
  88. [ -z "$AINPUT" ] || [ $AINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 audio source"
  89. [ -z "$VINPUT" ] || [ $VINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 video source"
  90. [ -z "$WINPUT" ] || [ $WINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 watermark source"
  91. [ -z "$XINPUT" ] || [ $XINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 multimedia source"
  92. [ -n "${NOAUDIO:-}" ] || [ -z "$AINPUT$XINPUT" ] || HASAUDIO=1
  93. [ -n "${NOVIDEO:-}" ] || [ -z "$VINPUT$XINPUT" ] || HASVIDEO=1
  94. [ "$AINPUT$VINPUT$XINPUT" = "1" ] || TWOSOURCES=1
  95. VSTREAMINDEX=1
  96. [ -n "$AINPUT" ] || VSTREAMINDEX=0
  97. FIRSTPORT=${FIRSTPORT:-5002} # even number - next 7 ports used too
  98. ACHANNELS=1
  99. AFRAMERATE_SRC=48000
  100. AFRAMERATE_OPUS=48000
  101. AFRAMERATE_AAC=44100
  102. ABITRATE_OPUS=48000
  103. ABITRATE_AAC=64000
  104. # FIXME: support multiple heights
  105. HEIGHT=288
  106. HEIGHTS_WEBM="$HEIGHT"
  107. HEIGHTS_MPEG="$HEIGHT"
  108. videosize() { height=$1; width_or_ratio=$2;
  109. case $width_or_ratio in
  110. *:*) num=${width_or_ratio%:*}; den=${width_or_ratio#*:}; width=$((height*num/den));;
  111. *) width=$width_or_ratio;;
  112. esac
  113. echo_n $((height*width))
  114. }
  115. BITS=$(videosize "$HEIGHT" "$RATIO")
  116. # * bitrates and bits in parens based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  117. # + bits rounded up to include nearby modulo 16 formats
  118. # + best (i.e. high-modulo) 16:9 heights: 216 288 360 432 576 720
  119. # + best (i.e. high-modulo) 4:3 heights: 288 312 384 480 624 816
  120. # * speeds tuned to just below 100% cpu usage for each combination on a multi-core computer
  121. # TODO: Externalize speeds to site-specific configfile
  122. if [ $BITS -le 110592 ]; then # 234p → 97344
  123. VBITRATE=145000
  124. SPEED_X264=fast; SPEED_X264_ALONE=fast
  125. SPEED_VP8=3; SPEED_VP8_ALONE=2
  126. elif [ $BITS -le 150528 ]; then # 270p → 129600
  127. VBITRATE=365000
  128. SPEED_X264=faster; SPEED_X264_ALONE=fast
  129. SPEED_VP8=4; SPEED_VP8_ALONE=2
  130. elif [ $BITS -le 196608 ]; then # 360p → 172800
  131. VBITRATE=730000
  132. SPEED_X264=veryfast; SPEED_X264_ALONE=fast
  133. SPEED_VP8=5; SPEED_VP8_ALONE=3
  134. elif [ $BITS -le 331776 ]; then # 432p → 331776
  135. VBITRATE=1100000
  136. SPEED_X264=ultrafast; SPEED_X264_ALONE=fast
  137. SPEED_VP8=8; SPEED_VP8_ALONE=4
  138. elif [ $BITS -le 589824 ]; then # 540p → 518400
  139. VBITRATE=2000000
  140. SPEED_X264=toofast; SPEED_X264_ALONE=veryfast
  141. SPEED_VP8_ALONE=5
  142. elif [ $BITS -le 921600 ]; then # 720p → 921600
  143. VBITRATE=3000000
  144. SPEED_X264=toofast; SPEED_X264_ALONE=ultrafast
  145. SPEED_VP8_ALONE=15
  146. fi
  147. [ toofast != "$SPEED_X264" ] || HEIGHTS_MPEG=
  148. [ -n "$HEIGHTS_MPEG" ] || SPEED_VP8="$SPEED_VP8_ALONE"
  149. [ -n "$HEIGHTS_WEBM" ] || SPEED_X264="$SPEED_X264_ALONE"
  150. HEIGHTS=$(uniqwords "$HEIGHTS_WEBM $HEIGHTS_MPEG")
  151. [ -z "${SAVEDIR:-}" ] || SAVESTEM="${SAVEDIR:-}/$(date +%Y%m%d-%H%M%S)"
  152. [ -z "${SAVEDIR:-}" ] || export FFREPORT=file="$SAVESTEM.log"
  153. filter_split_height() {
  154. heightcount=$(echo "$HEIGHTS" | wc --words)
  155. echo_n "[$VSTREAMINDEX:v]${DEINT:+$DEINT,}format=pix_fmts=yuv420p,split=$heightcount"
  156. printf_each '[s%s]' "$HEIGHTS"
  157. }
  158. filter_scale() { outstem=${1:-v};
  159. for height in $HEIGHTS; do
  160. echo_n "[s$height]scale=iw*sar*$height/ih:$height,setsar=1[$outstem$height]"
  161. done
  162. }
  163. filter_watermark() {
  164. for height in $HEIGHTS; do
  165. echo_n "[bg$height][$((VSTREAMINDEX+1)):v]overlay=main_w-overlay_w-20:main_h-overlay_h-20[v$height]"
  166. done
  167. }
  168. filter_split_codec() { heights_webm=$1; heights_mpeg=$2;
  169. codeccount=$(valuedargcount "$@")
  170. printf_each "[v%s]split=$codeccount$(printf_each '[v%swebm]' "$heights_webm")$(printf_each '[v%smpeg]' "$heights_mpeg")" "$HEIGHTS"
  171. }
  172. encode_opus() {
  173. echo_n "-codec:a libopus -ac $ACHANNELS -ar $AFRAMERATE_OPUS -b:a $ABITRATE_OPUS"
  174. }
  175. encode_aac() {
  176. echo_n "-codec:a aac -strict experimental -ac $ACHANNELS -ar $AFRAMERATE_AAC -b:a $((ACHANNELS*ABITRATE_AAC))"
  177. }
  178. encode_vp8() { bitrate=$1; speed=$2;
  179. echo_n "-codec:v vp8 -quality realtime -deadline 1000000 -cpu-used $speed \
  180. -b:v $bitrate -minrate $bitrate -maxrate $bitrate \
  181. -undershoot-pct 95 -bufsize $((6000*bitrate/1000)) -rc_init_occupancy $((4000*bitrate/1000)) \
  182. -max-intra-rate 0 \
  183. -qmin 4 -qmax 56 \
  184. -force_key_frames expr:gte(t,n_forced*2)"
  185. }
  186. encode_x264() { bitrate=$1; speed=$2;
  187. echo_n "-codec:v libx264 -preset $speed -tune zerolatency \
  188. -maxrate $bitrate -bufsize $((bitrate*2)) -crf 23 \
  189. -force_key_frames expr:gte(t,n_forced*2)"
  190. }
  191. tee_rtp() { stream=$1; pt=$2; port=$3;
  192. echo_n "[select=\'$stream\':f=rtp:payload_type=$pt]rtp://$IP:$port?pkt_size=1440"
  193. }
  194. # * scale+watermark trick based on http://stackoverflow.com/a/10937357
  195. # + scale to square pixels as needed
  196. # * routing based on http://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs#Teepseudo-muxer
  197. # * VP8 encoding based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  198. # + Add 1s latency (deadline)
  199. # * Predictive GOP size preserving scene-change GOP based on https://superuser.com/a/1098329
  200. # * Use same RTP payload types as GStreamer
  201. ffmpeg -hide_banner -threads auto \
  202. ${ALSA:+-f alsa -sample_rate "$AFRAMERATE_SRC" -channels "$ACHANNELS" -thread_queue_size 2048 -i "$ALSA"} \
  203. ${DVCAM:+-f iec61883 -thread_queue_size 64 -i $DVCAM} \
  204. ${XFILE:+-re${SEEK:+ -ss $SEEK} -i "$XFILE"} \
  205. ${IIDC:+-f libdc1394 -video_size 640x480 -framerate 15 -thread_queue_size 256 -i "$IIDC"} \
  206. ${VFILE:+-re${SEEK:+ -ss $SEEK} -i "$VFILE"} \
  207. ${LOGO:+-i "$LOGO"} \
  208. ${HASVIDEO:+-filter_complex \
  209. "$(filter_split_height);
  210. $(filter_scale "${WINPUT:+bg}")${WINPUT:+${LOGO:+;
  211. $(filter_watermark)}};
  212. $(filter_split_codec "$HEIGHTS_WEBM" "$HEIGHTS_MPEG")" } \
  213. ${SAVEDIR:+\
  214. ${DVCAM:+-map $((0${ALSA:++1})) \
  215. -codec copy -f dv "$SAVESTEM.dv" }} \
  216. ${HEIGHTS_WEBM:+\
  217. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%swebm]' "$HEIGHTS_WEBM") } -shortest \
  218. ${HASAUDIO:+\
  219. $(encode_opus) } \
  220. ${HASVIDEO:+\
  221. $(encode_vp8 "$VBITRATE" "$SPEED_VP8") } \
  222. -f tee \
  223. "${HASAUDIO:+\
  224. $(tee_rtp a 111 "$FIRSTPORT")|\
  225. }${HASVIDEO:+\
  226. $(tee_rtp v 100 $((FIRSTPORT+2)))}" } \
  227. ${HEIGHTS_MPEG:+\
  228. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%smpeg]' "$HEIGHTS_MPEG") } -shortest \
  229. ${HASAUDIO:+\
  230. $(encode_aac) } \
  231. ${HASVIDEO:+\
  232. $(encode_x264 "$VBITRATE" "$SPEED_X264") } \
  233. -f rtp_mpegts rtp://$IP:10000 }