summaryrefslogtreecommitdiff
path: root/bin/loop+encode+stream
blob: a0a2b1d6a1a130c1f592cf406f18f05b77116e21 (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=360
  17. # * height steps and bitrates based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  18. # * speeds tuned to just below 100% cpu usage for each combination on a multi-core computer
  19. # TODO: adjust height steps or bitrates for 4:3 aspect ratio
  20. # TODO: resolve steps from source height using http://aarmstrong.org/tutorials/aspect-ratios-and-h264
  21. # TODO: Externalize speeds to site-specific configfile
  22. if [ $HEIGHT -le 234 ]; then
  23. VBITRATE=145000
  24. SPEED_VP8=2
  25. elif [ $HEIGHT -le 270 ]; then
  26. VBITRATE=365000
  27. SPEED_VP8=2
  28. elif [ $HEIGHT -le 360 ]; then
  29. VBITRATE=730000
  30. SPEED_VP8=3
  31. elif [ $HEIGHT -le 432 ]; then
  32. VBITRATE=1100000
  33. SPEED_VP8=4
  34. elif [ $HEIGHT -le 540 ]; then
  35. VBITRATE=2000000
  36. SPEED_VP8=5
  37. elif [ $HEIGHT -le 720 ]; then
  38. VBITRATE=3000000
  39. SPEED_VP8=15
  40. fi
  41. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  42. # + Add 1s latency (deadline)
  43. # + Use same RTP payload types as GStreamer
  44. ffmpeg -hide_banner -threads auto -re \
  45. -stream_loop -1 -i "$INPUT" \
  46. -map '0:a' \
  47. -codec:a libopus -ac "$ACHANNELS" -ar "$AFRAMERATE_OPUS" -b:a "$ABITRATE_OPUS" \
  48. -f rtp -payload_type 111 "rtp://$IP:$FIRSTPORT?pkt_size=1200" \
  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"