summaryrefslogtreecommitdiff
path: root/bin/stream
blob: 48b4c92ac8820b4105ccae85ec0bf15f1c7aa97d (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=270
  77. HEIGHTS_WEBM="$HEIGHT"
  78. HEIGHTS_MPEG="$HEIGHT"
  79. # TODO: Vary vpx quality based on height
  80. SPEED_VPX=15
  81. # inspired by Apple HLS recommendations
  82. # TODO: Externalize to site-specific configfile
  83. if [ $HEIGHT -le 234 ]; then VBITRATE=145000; SPEED_X264=slow; SPEED_X264_ALONE=fast;
  84. elif [ $HEIGHT -le 270 ]; then VBITRATE=365000; SPEED_X264=faster; SPEED_X264_ALONE=fast;
  85. elif [ $HEIGHT -le 360 ]; then VBITRATE=730000; SPEED_X264=faster; SPEED_X264_ALONE=fast;
  86. elif [ $HEIGHT -le 432 ]; then VBITRATE=1100000; SPEED_X264=veryfast; SPEED_X264_ALONE=fast;
  87. elif [ $HEIGHT -le 540 ]; then VBITRATE=2000000; SPEED_X264=toofast; SPEED_X264_ALONE=veryfast;
  88. elif [ $HEIGHT -le 720 ]; then VBITRATE=3000000; SPEED_X264=toofast; SPEED_X264_ALONE=ultrafast;
  89. fi
  90. [ -n "$HEIGHTS_WEBM" ] || SPEED_X264="$SPEED_X264_ALONE"
  91. [ toofast != "$SPEED_X264" ] || exit1 "Not enough CPU - reduce size or streams"
  92. # shellcheck disable=SC2048,SC2059
  93. echo_n() {
  94. printf "$*"
  95. }
  96. # shellcheck disable=SC2048,SC2059
  97. printf_each() {
  98. skel=$1; shift
  99. for string in $*; do
  100. printf "$skel" "$string"
  101. done
  102. }
  103. uniqwords() {
  104. echo "$@" | tr ' ' '\012' | sort -u
  105. }
  106. valuedargcount() {
  107. nonempty=
  108. while [ $# -gt 0 ]; do
  109. [ -z "$1" ] || nonempty=$((nonempty+1))
  110. shift
  111. done
  112. echo_n "$nonempty"
  113. }
  114. HEIGHTS=$(uniqwords "$HEIGHTS_WEBM $HEIGHTS_MPEG")
  115. [ -z "${SAVEDIR:-}" ] || SAVESTEM="${SAVEDIR:-}/$(date +%Y%m%d-%H%M%S)"
  116. [ -z "${SAVEDIR:-}" ] || export FFREPORT=file="$SAVESTEM.log"
  117. filter_split_height() {
  118. heightcount=$(echo "$HEIGHTS" | wc --words)
  119. echo_n "[$VSTREAMINDEX:v]${DEINT:+$DEINT,}split=$heightcount"
  120. printf_each '[s%s]' "$HEIGHTS"
  121. }
  122. filter_scale() { outstem=${1:-v};
  123. for height in $HEIGHTS; do
  124. echo_n "[s$height]scale=iw*sar*$height/ih:$height,setsar=1[$outstem$height]"
  125. done
  126. }
  127. filter_watermark() {
  128. for height in $HEIGHTS; do
  129. echo_n "[bg$height][$((VSTREAMINDEX+1)):v]overlay=main_w-overlay_w-20:main_h-overlay_h-20[v$height]"
  130. done
  131. }
  132. filter_split_codec() { heights_webm=$1; heights_mpeg=$2;
  133. codeccount=$(valuedargcount "$@")
  134. printf_each "[v%s]split=$codeccount$(printf_each '[v%swebm]' "$heights_webm")$(printf_each '[v%smpeg]' "$heights_mpeg")" "$HEIGHTS"
  135. }
  136. tee_rtp() { stream=$1; pt=$2; port=$3;
  137. echo_n "[select=\'$stream\':f=rtp:payload_type=$pt]rtp://$IP:$port?pkt_size=1200"
  138. }
  139. # * scale+watermark trick based on http://stackoverflow.com/a/10937357
  140. # + scale to square pixels as needed
  141. # * routing based on http://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs#Teepseudo-muxer
  142. # * VP8 encoding based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  143. # + Add 1s latency (deadline)
  144. # * Predictive GOP size preserving scene-change GOP based on https://superuser.com/a/1098329
  145. # * Use same RTP payload types as GStreamer
  146. ffmpeg -hide_banner -threads auto \
  147. ${ALSA:+-f alsa -sample_rate "$AFRAMERATE_SRC" -channels "$ACHANNELS" -thread_queue_size 2048 -i "$ALSA"} \
  148. ${DVCAM:+-f iec61883 -thread_queue_size 64 -i $DVCAM} \
  149. ${XFILE:+-re${SEEK:+ -ss $SEEK} -i "$XFILE"} \
  150. ${IIDC:+-f libdc1394 -video_size 640x480 -framerate 15 -thread_queue_size 256 -i "$IIDC"} \
  151. ${VFILE:+-re${SEEK:+ -ss $SEEK} -i "$VFILE"} \
  152. ${LOGO:+-i "$LOGO"} \
  153. ${HASVIDEO:+-filter_complex \
  154. "$(filter_split_height);
  155. $(filter_scale "${WINPUT:+bg}")${WINPUT:+${LOGO:+;
  156. $(filter_watermark)}};
  157. $(filter_split_codec "$HEIGHTS_WEBM" "$HEIGHTS_MPEG")" } \
  158. ${SAVEDIR:+\
  159. ${DVCAM:+-map $((0${ALSA:++1})) \
  160. -codec copy -f dv "$SAVESTEM.dv" }} \
  161. ${HEIGHTS_WEBM:+\
  162. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%swebm]' "$HEIGHTS_WEBM") } -shortest \
  163. ${HASAUDIO:+\
  164. -codec:a libopus -ac "$ACHANNELS" -ar "$AFRAMERATE_OPUS" -b:a "$ABITRATE_OPUS" } \
  165. ${HASVIDEO:+\
  166. -pix_fmt yuv420p \
  167. -codec:v vp8 -quality realtime -deadline 1000000 -cpu-used "$SPEED_VPX" \
  168. -b:v "$VBITRATE" -minrate "$VBITRATE" -maxrate "$VBITRATE" \
  169. -undershoot-pct 95 -bufsize $((6000*VBITRATE/1000)) -rc_init_occupancy $((4000*VBITRATE/1000)) \
  170. -max-intra-rate 0 \
  171. -qmin 4 -qmax 56 } \
  172. -f tee \
  173. "${HASAUDIO:+\
  174. $(tee_rtp a 111 "$FIRSTPORT")|\
  175. }${HASVIDEO:+\
  176. $(tee_rtp v 100 $((FIRSTPORT+2)))}" } \
  177. ${HEIGHTS_MPEG:+\
  178. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%smpeg]' "$HEIGHTS_MPEG") } -shortest \
  179. ${HASAUDIO:+\
  180. -codec:a aac -strict experimental -ac "$ACHANNELS" -ar "$AFRAMERATE_AAC" -b:a $((ACHANNELS*ABITRATE_AAC)) } \
  181. ${HASVIDEO:+\
  182. -pix_fmt yuv420p \
  183. -codec:v libx264 -preset "$SPEED_X264" -tune zerolatency \
  184. -maxrate "$VBITRATE" -bufsize "$((VBITRATE*2))" -crf 23 } \
  185. -force_key_frames 'expr:gte(t,n_forced*2)' \
  186. -f rtp_mpegts rtp://$IP:10000 }