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