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