summaryrefslogtreecommitdiff
path: root/bin/encodevideo+loop+stream
blob: a5027e2dca5c38e44089c2bd7ea3dd6296965d87 (plain)
  1. #!/bin/sh
  2. set -e
  3. INPUT=../tmp/omni/show0/earth.yuv
  4. ##
  5. ## generic functions
  6. ##
  7. # shellcheck disable=SC2048,SC2059
  8. echo_n() {
  9. printf -- "$*"
  10. }
  11. HOST=${1:-morla}
  12. if [ "$HOST" = "$(hostname --short)" ]; then
  13. IP=127.0.0.1
  14. else
  15. IP=$(host "$HOST" | grep -Po 'address \K\S+')
  16. fi
  17. FIRSTPORT=${2:-5002} # even number - next 3 ports used too
  18. # TODO: resolve this or resize based on it
  19. HEIGHT=384
  20. RATIO_NUM=4
  21. RATIO_DEN=3
  22. # * bitrates and bits in parens based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  23. # + bits rounded up to include nearby modulo 16 formats
  24. # + best (i.e. high-modulo) 16:9 heights: 216 288 360 432 576 720
  25. # + best (i.e. high-modulo) 4:3 heights: 288 312 384 480 624 816
  26. # * speeds tuned to just below 100% cpu usage for each combination on a multi-core computer
  27. # TODO: Externalize speeds to site-specific configfile
  28. RATIO_NUM=${RATION_NUM:-16}
  29. RATIO_DEN=${RATION_DEN:-9}
  30. WIDTH=${WIDTH:-$((HEIGHT*RATIO_NUM/RATIO_DEN))}
  31. BITS=$((WIDTH*HEIGHT))
  32. if [ $BITS -le 110592 ]; then # 234p → 97344
  33. VBITRATE=145000
  34. SPEED_VP8=2
  35. elif [ $BITS -le 150528 ]; then # 270p → 129600
  36. VBITRATE=365000
  37. SPEED_VP8=2
  38. elif [ $BITS -le 196608 ]; then # 360p → 172800
  39. VBITRATE=730000
  40. SPEED_VP8=3
  41. elif [ $BITS -le 331776 ]; then # 432p → 331776
  42. VBITRATE=1100000
  43. SPEED_VP8=4
  44. elif [ $BITS -le 589824 ]; then # 540p → 518400
  45. VBITRATE=2000000
  46. SPEED_VP8=5
  47. elif [ $BITS -le 921600 ]; then # 720p → 921600
  48. VBITRATE=3000000
  49. SPEED_VP8=15
  50. fi
  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:v' \
  65. $(encode_vp8 "$VBITRATE" "$SPEED_VP8") \
  66. -f rtp -payload_type 100 "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1200"