summaryrefslogtreecommitdiff
path: root/bin/loop+encode+stream
blob: 5beef2d3260c90c4615cad2a52e076543b09fb29 (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. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  49. # + Add 1s latency (deadline)
  50. # + Use same RTP payload types as GStreamer
  51. ffmpeg -hide_banner -threads auto -re \
  52. -stream_loop -1 -i "$INPUT" \
  53. -map '0:a' \
  54. -codec:a libopus -ac "$ACHANNELS" -ar "$AFRAMERATE_OPUS" -b:a "$ABITRATE_OPUS" \
  55. -f rtp -payload_type 111 "rtp://$IP:$FIRSTPORT?pkt_size=1200" \
  56. -map '0:v' \
  57. -pix_fmt yuv420p \
  58. -codec:v vp8 -quality realtime -deadline 1000000 -cpu-used "$SPEED_VP8" \
  59. -b:v "$VBITRATE" -minrate "$VBITRATE" -maxrate "$VBITRATE" \
  60. -undershoot-pct 95 -bufsize $((6000*VBITRATE/1000)) -rc_init_occupancy $((4000*VBITRATE/1000)) \
  61. -max-intra-rate 0 \
  62. -qmin 4 -qmax 56 \
  63. -f rtp -payload_type 100 "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1200"