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