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