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