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