summaryrefslogtreecommitdiff
path: root/bin/loop+encode+stream
blob: ca0b6b97d2f0af209449bf914f6c2ef15cec4396 (plain)
  1. #!/bin/sh
  2. # TODO: Handle audio from secondary input
  3. set -e
  4. INPUT=../tmp/omni/show0/earth.yuv
  5. HOST=${1:-morla}
  6. if [ "$HOST" = "$(hostname --short)" ]; then
  7. IP=127.0.0.1
  8. else
  9. IP=$(host "$HOST" | grep -Po 'address \K\S+')
  10. fi
  11. FIRSTPORT=${2:-5002} # even number - next 3 ports used too
  12. ACHANNELS=2
  13. AFRAMERATE_OPUS=48000
  14. ABITRATE_OPUS=48000
  15. # TODO
  16. HEIGHT=384
  17. RATIO_NUM=4
  18. RATIO_DEN=3
  19. # * bitrates and bits in parens based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  20. # + bits rounded up to include nearby modulo 16 formats
  21. # + best (i.e. high-modulo) 16:9 heights: 216 288 360 432 576 720
  22. # + best (i.e. high-modulo) 4:3 heights: 288 312 384 480 624 816
  23. # * speeds tuned to just below 100% cpu usage for each combination on a multi-core computer
  24. # TODO: Externalize speeds to site-specific configfile
  25. RATIO_NUM=${RATION_NUM:-16}
  26. RATIO_DEN=${RATION_DEN:-9}
  27. WIDTH=${WIDTH:-$((HEIGHT*RATIO_NUM/RATIO_DEN))}
  28. BITS=$((WIDTH*HEIGHT))
  29. if [ $BITS -le 110592 ]; then # 234p → 97344
  30. VBITRATE=145000
  31. SPEED_VP8=2
  32. elif [ $BITS -le 150528 ]; then # 270p → 129600
  33. VBITRATE=365000
  34. SPEED_VP8=2
  35. elif [ $BITS -le 196608 ]; then # 360p → 172800
  36. VBITRATE=730000
  37. SPEED_VP8=3
  38. elif [ $BITS -le 331776 ]; then # 432p → 331776
  39. VBITRATE=1100000
  40. SPEED_VP8=4
  41. elif [ $BITS -le 589824 ]; then # 540p → 518400
  42. VBITRATE=2000000
  43. SPEED_VP8=5
  44. elif [ $BITS -le 921600 ]; then # 720p → 921600
  45. VBITRATE=3000000
  46. SPEED_VP8=15
  47. fi
  48. # shellcheck disable=SC2048,SC2059
  49. echo_n() {
  50. printf -- "$*"
  51. }
  52. encode_opus() {
  53. echo_n "-codec:a libopus -ac $ACHANNELS -ar $AFRAMERATE_OPUS -b:a $ABITRATE_OPUS"
  54. }
  55. encode_vp8() { bitrate=$1; speed=$2;
  56. echo_n "-codec:v vp8 -quality realtime -deadline 1000000 -cpu-used $speed \
  57. -b:v $bitrate -minrate $bitrate -maxrate $bitrate \
  58. -undershoot-pct 95 -bufsize $((6000*bitrate/1000)) -rc_init_occupancy $((4000*bitrate/1000)) \
  59. -max-intra-rate 0 \
  60. -qmin 4 -qmax 56"
  61. }
  62. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  63. # + Add 1s latency (deadline)
  64. # + Use same RTP payload types as GStreamer
  65. ffmpeg -hide_banner -threads auto -re \
  66. -stream_loop -1 -i "$INPUT" \
  67. -vf format=pix_fmts=yuv420p \
  68. -map '0:a' \
  69. $(encode_opus) \
  70. -f rtp -payload_type 111 "rtp://$IP:$FIRSTPORT?pkt_size=1200" \
  71. -map '0:v' \
  72. $(encode_vp8 "$VBITRATE" "$SPEED_VP8") \
  73. -f rtp -payload_type 100 "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1200"