summaryrefslogtreecommitdiff
path: root/bin/loop+encode+stream
blob: 5331121b577dd7e170d7d40fc7352b46fd4ee002 (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 VBITRATE=145000; SPEED_VP8=2;
  23. elif [ $HEIGHT -le 270 ]; then VBITRATE=365000; SPEED_VP8=2;
  24. elif [ $HEIGHT -le 360 ]; then VBITRATE=730000; SPEED_VP8=3;
  25. elif [ $HEIGHT -le 432 ]; then VBITRATE=1100000; SPEED_VP8=4;
  26. elif [ $HEIGHT -le 540 ]; then VBITRATE=2000000; SPEED_VP8=5;
  27. elif [ $HEIGHT -le 720 ]; then VBITRATE=3000000; SPEED_VP8=15;
  28. fi
  29. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  30. # + Add 1s latency (deadline)
  31. # + Use same RTP payload types as GStreamer
  32. ffmpeg -hide_banner -threads auto -re \
  33. -stream_loop -1 -i "$INPUT" \
  34. -map '0:a' \
  35. -codec:a libopus -ac "$ACHANNELS" -ar "$AFRAMERATE_OPUS" -b:a "$ABITRATE_OPUS" \
  36. -f rtp -payload_type 111 "rtp://$IP:$FIRSTPORT?pkt_size=1200" \
  37. -map '0:v' \
  38. -pix_fmt yuv420p \
  39. -codec:v vp8 -quality realtime -deadline 1000000 -cpu-used "$SPEED_VP8" \
  40. -b:v "$VBITRATE" -minrate "$VBITRATE" -maxrate "$VBITRATE" \
  41. -undershoot-pct 95 -bufsize $((6000*VBITRATE/1000)) -rc_init_occupancy $((4000*VBITRATE/1000)) \
  42. -max-intra-rate 0 \
  43. -qmin 4 -qmax 56 \
  44. -f rtp -payload_type 100 "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1200"