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