summaryrefslogtreecommitdiff
path: root/bin/stream
blob: 4ad152d8ecee3be8859ac55ac403ceb4414d1f35 (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. dv-stream=*) AVISTREAM="-f avi -i ${1#*=}"; XINPUT=$((XINPUT+1));;
  71. dv-stream) AVISTREAM="-f avi -i udp://localhost:10000"; XINPUT=$((XINPUT+1));;
  72. videofile=*) VFILE=${1#*=}; VINPUT=$((VINPUT+1));;
  73. *.ffv1|*.yuv|*.vp8|*.vp9) VFILE=$1; VINPUT=$((VINPUT+1));;
  74. container=*) XFILE=${1#*=}; XINPUT=$((XINPUT+1));;
  75. *.avi|*.dv|*.mkv|*.mov|*.mp4|*.nut|*.ogg|*.ogv|*.webm) XFILE=$1; XINPUT=$((XINPUT+1));;
  76. *.png) LOGO=$1; WINPUT=$((WINPUT+1));;
  77. --) shift; break;;
  78. *) exit1 "Unsupported input: $1";;
  79. esac
  80. shift
  81. done
  82. HOST=${HOST:-127.0.0.1}
  83. if [ "$HOST" = "$(hostname --short)" ]; then
  84. IP=127.0.0.1
  85. else
  86. IP=$(host "$HOST" | grep -Po 'address \K\S+')
  87. fi
  88. [ -n "$AINPUT$VINPUT$XINPUT" ] || exit1 "Too few arguments: Min. 1 A/V source"
  89. [ -z "$AINPUT" ] || [ -z "$VINPUT" ] || [ -z "$XINPUT" ] || exit1 "Too many arguments: Max. 2 A/V sources"
  90. [ -z "$AINPUT" ] || [ $AINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 audio source"
  91. [ -z "$VINPUT" ] || [ $VINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 video source"
  92. [ -z "$WINPUT" ] || [ $WINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 watermark source"
  93. [ -z "$XINPUT" ] || [ $XINPUT -eq 1 ] || exit1 "Too many arguments: Max. 1 multimedia source"
  94. [ -n "${NOAUDIO:-}" ] || [ -z "$AINPUT$XINPUT" ] || HASAUDIO=1
  95. [ -n "${NOVIDEO:-}" ] || [ -z "$VINPUT$XINPUT" ] || HASVIDEO=1
  96. VSTREAMINDEX=1
  97. [ -n "$AINPUT" ] || VSTREAMINDEX=0
  98. FIRSTPORT=${FIRSTPORT:-5002} # even number - next 7 ports used too
  99. ACHANNELS=1
  100. AFRAMERATE_SRC=48000
  101. AFRAMERATE_OPUS=48000
  102. AFRAMERATE_AAC=44100
  103. ABITRATE_OPUS=48000
  104. ABITRATE_AAC=64000
  105. # FIXME: support multiple heights
  106. HEIGHT=264
  107. ENCODINGS_WEBM="$HEIGHT"
  108. ENCODINGS_MPEG="$HEIGHT"
  109. videosize() { height=$1; width_or_ratio=$2;
  110. case $width_or_ratio in
  111. *:*) num=${width_or_ratio%:*}; den=${width_or_ratio#*:}; width=$((height*num/den));;
  112. *) width=$width_or_ratio;;
  113. esac
  114. echo_n $((height*width))
  115. }
  116. BITS=$(videosize "$HEIGHT" "$RATIO")
  117. # * bitrates and bits in parens based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  118. # + bits rounded up to include nearby modulo 16 or 8 sizes
  119. # * Recommended modulo 16 or 8 (or 4) sizes:
  120. # + 4:3: [96 216] 264 312 408 480 624 816
  121. # + 16:9: [72 160] 216 288 360 432 496(540) 720
  122. # + 2.40:1: [80 160] 200 240 320 360 480 600(620)
  123. # (highest size not "pumping" in bridge scene 20s into Tears of Steel)
  124. # quantified using http://aarmstrong.org/content/aspect_ratio_calc.php
  125. # with ratio as modulo 4 integers, and size a multiplum of that
  126. # * speeds tuned to just below 100% cpu usage for each combination on a multi-core computer
  127. # TODO: Externalize speeds to site-specific configfile
  128. if [ -z "$BITS" ] || [ $BITS -eq 0 ]; then # 192x80 → 15360
  129. AFRAMERATE_AAC=22050
  130. ABITRATE_OPUS=16000
  131. ABITRATE_AAC=32000
  132. HASVIDEO=
  133. elif [ $BITS -le 15360 ]; then # 4:3'96→16²*8*6=12288, 16:9'72→8²*16*9=9216, 2.40:1'80→16²*12*5=15360
  134. AFRAMERATE_AAC=22050
  135. ABITRATE_OPUS=16000
  136. ABITRATE_AAC=32000
  137. VFRAMERATE=10
  138. VBITRATE=32000
  139. SPEED_X264=fast; SPEED_X264_ALONE=fast
  140. SPEED_VP8=3; SPEED_VP8_ALONE=2
  141. elif [ $BITS -le 62208 ]; then # 4:3'216→8²*27*36=62208, 16:9'160→16²*24*10=61440, 2.40:1'160→16²*24*10=61440
  142. AFRAMERATE_AAC=22050
  143. ABITRATE_OPUS=16000
  144. ABITRATE_AAC=32000
  145. VFRAMERATE=10
  146. VBITRATE=64000
  147. SPEED_X264=fast; SPEED_X264_ALONE=fast
  148. SPEED_VP8=3; SPEED_VP8_ALONE=2
  149. elif [ $BITS -le 110592 ]; then # 4:3'264→8²*44*33=92928, 16:9'216→8²*48*27=82944, 2.40:1'200→8²*60*25=96000 (234p→97344)
  150. VBITRATE=145000
  151. SPEED_X264=fast; SPEED_X264_ALONE=fast
  152. SPEED_VP8=3; SPEED_VP8_ALONE=2
  153. elif [ $BITS -le 150528 ]; then # 4:3'312→8²*52*39=129792, 16:9'288→16²*32*18=147456, 2.40:1'240→16²*36*15=138240 (270p→129600)
  154. VBITRATE=365000
  155. SPEED_X264=faster; SPEED_X264_ALONE=fast
  156. SPEED_VP8=4; SPEED_VP8_ALONE=2
  157. elif [ $BITS -le 245760 ]; then # 4:3'408→8²*68*51=221952, 16:9'360→8²*80*45=230400, 2.40:1'320→16²*48*20=245760 (360p→230400)
  158. VBITRATE=730000
  159. SPEED_X264=veryfast; SPEED_X264_ALONE=fast
  160. SPEED_VP8=5; SPEED_VP8_ALONE=3
  161. elif [ $BITS -le 331776 ]; then # 4:3'480→16²*40*30=307200, 16:9'432→16²*48*27=331776, 2.40:1'360→8²*108*45=311040 (432p→331776)
  162. VBITRATE=1100000
  163. SPEED_X264=ultrafast; SPEED_X264_ALONE=fast
  164. SPEED_VP8=8; SPEED_VP8_ALONE=4
  165. elif [ $BITS -le 552960 ]; then # 4:3'624→16²*52*39=519168, 16:9'496→16²*56*31=444416(540→4²*240*135=518400), 2.40:1'480→16²*72*30=552960 (540p→518400)
  166. VBITRATE=2000000
  167. SPEED_X264=toofast; SPEED_X264_ALONE=veryfast
  168. SPEED_VP8_ALONE=5
  169. elif [ $BITS -le 922560 ]; then # 4:3'816→16²*68*51=887808, 16:9'720→16²*80*45=921600, 2.40:1'600→8²*180*75=864000(620→4²*372*155=922560) (720p→921600)
  170. VBITRATE=3000000
  171. SPEED_X264=toofast; SPEED_X264_ALONE=ultrafast
  172. SPEED_VP8_ALONE=15
  173. fi
  174. [ toofast != "${SPEED_X264:-}" ] || ENCODINGS_MPEG=
  175. [ -n "$ENCODINGS_MPEG" ] || SPEED_VP8="${SPEED_VP8_ALONE:-}"
  176. [ -n "$ENCODINGS_WEBM" ] || SPEED_X264="${SPEED_X264_ALONE:-}"
  177. TARGETS_WEBM="${HASAUDIO:+rtp_opus} ${HASVIDEO:+rtp_vp8}"
  178. TARGETS_MPEG="rtp_mpegts"
  179. ENCODINGS=$(uniqwords "$ENCODINGS_WEBM $ENCODINGS_MPEG")
  180. [ -z "${SAVEDIR:-}" ] || SAVESTEM="${SAVEDIR:-}/$(date +%Y%m%d-%H%M%S)"
  181. [ -z "${SAVEDIR:-}" ] || export FFREPORT=file="$SAVESTEM.log"
  182. # * scale+watermark trick based on http://stackoverflow.com/a/10937357
  183. # + scale to square pixels as needed
  184. filters_compose() { logo=$1;
  185. heightcount=$(echo "$ENCODINGS" | wc --words)
  186. echo_n "[$VSTREAMINDEX:v]${DEINT:+$DEINT,}${VFRAMERATE:+framerate=$VFRAMERATE,}format=pix_fmts=yuv420p,split=$heightcount"
  187. printf_each '[s%s]' "$ENCODINGS"
  188. echo_n ';'
  189. for height in $ENCODINGS; do
  190. echo_n "[s$height]scale=trunc(iw*sar*$height/ih/2)*2:$height,setsar=1"
  191. [ -z "$logo" ] || echo_n "[bg$height];[bg$height][$logo:v]overlay=main_w-overlay_w-20:main_h-overlay_h-20"
  192. echo_n "[v$height]"
  193. done
  194. }
  195. filter_split_codec() { heights_webm=$1; heights_mpeg=$2;
  196. codeccount=$(valuedargcount "$@")
  197. for height in $ENCODINGS; do
  198. echo_n "[v$height]split=$codeccount"
  199. printf_each '[v%swebm]' "$heights_webm"
  200. printf_each '[v%smpeg]' "$heights_mpeg"
  201. done
  202. }
  203. # * VP8 encoding based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  204. # + Add 1s latency (deadline)
  205. # * Predictive GOP size preserving scene-change GOP based on https://superuser.com/a/1098329
  206. encode_opus() {
  207. echo_n "-codec:a libopus -ac $ACHANNELS -ar $AFRAMERATE_OPUS -b:a $ABITRATE_OPUS"
  208. }
  209. encode_aac() {
  210. echo_n "-codec:a aac -strict experimental -ac $ACHANNELS -ar $AFRAMERATE_AAC -b:a $((ACHANNELS*ABITRATE_AAC))"
  211. }
  212. encode_vp8() { bitrate=$1; speed=$2;
  213. echo_n "-codec:v vp8 -quality realtime -deadline 1000000 -cpu-used $speed \
  214. -b:v $bitrate -minrate $bitrate -maxrate $bitrate \
  215. -undershoot-pct 95 -bufsize $((6000*bitrate/1000)) -rc_init_occupancy $((4000*bitrate/1000)) \
  216. -max-intra-rate 0 \
  217. -qmin 4 -qmax 56 \
  218. -force_key_frames expr:gte(t,n_forced*2)"
  219. }
  220. encode_x264() { bitrate=$1; speed=$2;
  221. echo_n "-codec:v libx264 -preset $speed -tune zerolatency \
  222. -maxrate $bitrate -bufsize $((bitrate*2)) -crf 23 \
  223. -force_key_frames expr:gte(t,n_forced*2)"
  224. }
  225. # * routing based on http://trac.ffmpeg.org/wiki/Creating%20multiple%20outputs#Teepseudo-muxer
  226. # * Use same RTP payload types as GStreamer
  227. mux() { targets="$1";
  228. targetcount=$(echo "$targets" | wc --words)
  229. if [ $targetcount -lt 2 ]; then
  230. case "$targets" in
  231. hls_mpeg) echo_n "-f hls -hls_time 6 -hls_flags delete_segments"
  232. echo_n " -hls_flags discont_start -hls_flags append_list"
  233. echo_n " -use_localtime 1 -hls_segment_filename file-%Y%m%d-%s.ts"
  234. echo_n " index.m3u8";;
  235. rtp_opus) echo_n "-f rtp -payload_type 111"
  236. echo_n " rtp://$IP:$FIRSTPORT?pkt_size=1440";;
  237. rtp_vp8) echo_n "-f rtp -payload_type 100"
  238. echo_n " rtp://$IP:$((FIRSTPORT+2))?pkt_size=1440";;
  239. rtp_mpegts) echo_n "-f rtp_mpegts"
  240. echo_n " rtp://$IP:10000?pkt_size=1440";;
  241. *) exit1 "Unknown target $targets";;
  242. esac
  243. else
  244. echo_n "-f tee "
  245. for target in $targets; do
  246. case "$target" in
  247. hls_mpeg) echo_n "[f=hls:hls_time=6:hls_flags=delete_segments"
  248. echo_n ":hls_flags=discont_start:hls_flags=append_list"
  249. echo_n ":use_localtime=1:hls_segment_filename=file-%Y%m%d-%s.ts]"
  250. echo_n "index.m3u8";;
  251. rtp_opus) echo_n "[select=\'a\':f=rtp:payload_type=111]"
  252. echo_n "rtp://$IP:$FIRSTPORT?pkt_size=1440";;
  253. rtp_vp8) echo_n "[select=\'v\':f=rtp:payload_type=100]"
  254. echo_n "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1440";;
  255. rtp_mpegts) echo_n "[f=rtp_mpegts]"
  256. echo_n "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1440";;
  257. *) exit1 "Unknown target $target";;
  258. esac
  259. targetcount=$((targetcount-1))
  260. [ $targetcount -lt 1 ] || echo_n '|'
  261. done
  262. fi
  263. }
  264. ffmpeg -hide_banner -threads auto \
  265. ${ALSA:+-f alsa -sample_rate "$AFRAMERATE_SRC" -channels "$ACHANNELS" -thread_queue_size 2048 -i "$ALSA"} \
  266. ${DVCAM:+-f iec61883 -thread_queue_size 64 -i $DVCAM} \
  267. ${AVISTREAM:-} \
  268. ${XFILE:+-re${SEEK:+ -ss $SEEK} -i "$XFILE"} \
  269. ${IIDC:+-f libdc1394 -video_size 640x480 -framerate 15 -thread_queue_size 256 -i "$IIDC"} \
  270. ${VFILE:+-re${SEEK:+ -ss $SEEK} -i "$VFILE"} \
  271. ${LOGO:+-i "$LOGO"} \
  272. ${ENCODINGS:+-filter_complex "\
  273. ${HASVIDEO:+
  274. $(filters_compose "${LOGO:+$((VSTREAMINDEX+1))}");
  275. $(filter_split_codec "$ENCODINGS_WEBM" "$ENCODINGS_MPEG") }"} \
  276. ${SAVEDIR:+\
  277. ${DVCAM:+-map $((0${ALSA:++1})) \
  278. -codec copy -f dv "$SAVESTEM.dv" }} \
  279. ${ENCODINGS_WEBM:+\
  280. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%swebm]' "$ENCODINGS_WEBM") } -shortest \
  281. ${HASAUDIO:+\
  282. $(encode_opus) } \
  283. ${HASVIDEO:+\
  284. $(encode_vp8 "$VBITRATE" "$SPEED_VP8") } \
  285. $(mux "$TARGETS_WEBM") } \
  286. ${ENCODINGS_MPEG:+\
  287. ${HASAUDIO:+-map '0:a' }${HASVIDEO:+$(printf_each ' -map [v%smpeg]' "$ENCODINGS_MPEG") } -shortest \
  288. ${HASAUDIO:+\
  289. $(encode_aac) } \
  290. ${HASVIDEO:+\
  291. $(encode_x264 "$VBITRATE" "$SPEED_X264") } \
  292. $(mux "$TARGETS_MPEG") }