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