summaryrefslogtreecommitdiff
path: root/bin/stream
blob: 4c684eb738792536ef8b12b721efac889739d939 (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#*=}; 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=24000
  71. ABITRATE=16000
  72. # FIXME: support multiple heights
  73. HEIGHTS_WEBM=234
  74. HEIGHTS_MPEG=234
  75. VBITRATE=145000
  76. # shellcheck disable=SC2048,SC2059
  77. echo_n() {
  78. printf "$*"
  79. }
  80. # shellcheck disable=SC2048,SC2059
  81. printf_each() {
  82. skel=$1; shift
  83. for string in $*; do
  84. printf "$skel" "$string"
  85. done
  86. }
  87. uniqwords() {
  88. echo "$@" | tr ' ' '\012' | sort -u
  89. }
  90. valuedargcount() {
  91. nonempty=
  92. while [ $# -gt 0 ]; do
  93. [ -z "$1" ] || nonempty=$((nonempty+1))
  94. shift
  95. done
  96. echo_n "$nonempty"
  97. }
  98. HEIGHTS=$(uniqwords "$HEIGHTS_WEBM $HEIGHTS_MPEG")
  99. [ -z "${SAVEDIR:-}" ] || SAVESTEM="${SAVEDIR:-}/$(date +%Y%m%d-%H%M%S)"
  100. [ -z "${SAVEDIR:-}" ] || export FFREPORT=file="$SAVESTEM.log"
  101. filter_split_height() {
  102. heightcount=$(echo "$HEIGHTS" | wc --words)
  103. echo_n "[$VSTREAMINDEX:v]split=$heightcount"
  104. printf_each '[s%s]' "$HEIGHTS"
  105. }
  106. filter_scale() { outstem=${1:-v};
  107. for height in $HEIGHTS; do
  108. echo_n "[s$height]scale=-2:$height[$outstem$height]"
  109. done
  110. }
  111. filter_watermark() {
  112. for height in $HEIGHTS; do
  113. echo_n "[bg$height][$((VSTREAMINDEX+1)):v]overlay=main_w-overlay_w-20:main_h-overlay_h-20[v$height]"
  114. done
  115. }
  116. filter_split_codec() { heights_webm=$1; heights_mpeg=$2;
  117. codeccount=$(valuedargcount "$@")
  118. printf_each "[v%s]split=$codeccount$(printf_each '[v%swebm]' "$heights_webm")$(printf_each '[v%smpeg]' "$heights_mpeg")" "$HEIGHTS"
  119. }
  120. tee_rtp() { stream=$1; pt=$2; port=$3;
  121. echo_n "[select=\'$stream\':f=rtp:payload_type=$pt]rtp://$IP:$port?pkt_size=1200"
  122. }
  123. # * scale+watermark trick based on http://stackoverflow.com/a/10937357
  124. # * routing based on http://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs#Teepseudo-muxer
  125. # * VP8 encoding based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  126. # + Add 1s latency (deadline)
  127. # * Predictive GOP size preserving scene-change GOP based on https://superuser.com/a/1098329
  128. # * Use same RTP payload types as GStreamer
  129. ffmpeg -hide_banner -threads auto \
  130. ${ALSA:+-f alsa -sample_rate "$AFRAMERATE" -channels "$ACHANNELS" -thread_queue_size 2048 -i "$ALSA"} \
  131. ${DVCAM:+-f iec61883 -thread_queue_size 64 -i $DVCAM} \
  132. ${XFILE:+-re${SEEK:+ -ss $SEEK} -i "$XFILE"} \
  133. ${IIDC:+-f libdc1394 -video_size 640x480 -framerate 15 -thread_queue_size 256 -i "$IIDC"} \
  134. ${VFILE:+-re${SEEK:+ -ss $SEEK} -i "$VFILE"} \
  135. ${LOGO:+-i "$LOGO"} \
  136. ${HASVIDEO:+-filter_complex \
  137. "$(filter_split_height);
  138. $(filter_scale "${WINPUT:+bg}")${WINPUT:+${LOGO:+;
  139. $(filter_watermark)}};
  140. $(filter_split_codec "$HEIGHTS_WEBM" "$HEIGHTS_MPEG")" } \
  141. ${SAVEDIR:+\
  142. ${DVCAM:+-map $((0${ALSA:++1})) \
  143. -codec copy -f dv "$SAVESTEM.dv" }} \
  144. ${HEIGHTS_WEBM:+\
  145. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%swebm]' "$HEIGHTS_WEBM") } -shortest \
  146. ${HASAUDIO:+\
  147. -codec:a libopus -ac "$ACHANNELS" -ar "$AFRAMERATE" -b:a "$ABITRATE" } \
  148. ${HASVIDEO:+\
  149. -pix_fmt yuv420p \
  150. -codec:v vp8 -quality realtime -deadline 1000000 -cpu-used 15 \
  151. -b:v "$VBITRATE" -minrate "$VBITRATE" -maxrate "$VBITRATE" \
  152. -undershoot-pct 95 -bufsize $((6000*VBITRATE/1000)) -rc_init_occupancy $((4000*VBITRATE/1000)) \
  153. -max-intra-rate 0 \
  154. -qmin 4 -qmax 56 } \
  155. -f tee \
  156. "${HASAUDIO:+\
  157. $(tee_rtp a 111 "$FIRSTPORT")|\
  158. }${HASVIDEO:+\
  159. $(tee_rtp v 100 $((FIRSTPORT+2)))}" } \
  160. ${HEIGHTS_MPEG:+\
  161. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%smpeg]' "$HEIGHTS_MPEG") } -shortest \
  162. ${HASAUDIO:+\
  163. -codec:a aac -strict experimental -ac "$ACHANNELS" -ar 44100 -b:a $((ACHANNELS*64))k } \
  164. ${HASVIDEO:+\
  165. -pix_fmt yuv420p \
  166. -codec:v libx264 -tune zerolatency -preset ultrafast \
  167. -maxrate "$VBITRATE" -bufsize "$((VBITRATE*2))" -crf 23 } \
  168. -force_key_frames 'expr:gte(t,n_forced*2)' \
  169. -f rtp_mpegts rtp://$IP:10000 }