summaryrefslogtreecommitdiff
path: root/bin/decode+edit+loop+encode+stream
blob: c91ac96c93da87c167cbf548f6d938dfec633282 (plain)
  1. #!/bin/sh
  2. # FIXME: Handle audio track in main input
  3. # TODO: Handle audio as secondary input
  4. set -e
  5. INPUT=../src/omni/show0/earth.mp4
  6. LOGO=../../content/icon_small.png
  7. HOST=${1:-morla}
  8. if [ "$HOST" = "$(hostname --short)" ]; then
  9. IP=127.0.0.1
  10. else
  11. IP=$(host "$HOST" | grep -Po 'address \K\S+')
  12. fi
  13. FIRSTPORT=${2:-5002} # even number - next 3 ports used too
  14. ITERATIONS=${3-0} # endless by default
  15. HEIGHT=360
  16. # * height steps and bitrates based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  17. # * speeds tuned to just below 100% cpu usage for each combination on a multi-core computer
  18. # TODO: adjust height steps or bitrates for 4:3 aspect ratio
  19. # TODO: resolve steps from source height using http://aarmstrong.org/tutorials/aspect-ratios-and-h264
  20. # TODO: Externalize speeds to site-specific configfile
  21. if [ $HEIGHT -le 234 ]; then
  22. VBITRATE=145000
  23. SPEED_VP8=2
  24. elif [ $HEIGHT -le 270 ]; then
  25. VBITRATE=365000
  26. SPEED_VP8=2
  27. elif [ $HEIGHT -le 360 ]; then
  28. VBITRATE=730000
  29. SPEED_VP8=3
  30. elif [ $HEIGHT -le 432 ]; then
  31. VBITRATE=1100000
  32. SPEED_VP8=4
  33. elif [ $HEIGHT -le 540 ]; then
  34. VBITRATE=2000000
  35. SPEED_VP8=5
  36. elif [ $HEIGHT -le 720 ]; then
  37. VBITRATE=3000000
  38. SPEED_VP8=15
  39. fi
  40. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  41. # + loop-in-filter trick based on http://video.stackexchange.com/a/16933
  42. # + scale+watermark trick based on http://stackoverflow.com/a/10937357
  43. # + scale to square pixels as needed
  44. # + Add 1s latency (deadline)
  45. # + Use same RTP payload types as GStreamer
  46. ffmpeg -hide_banner -threads auto -re \
  47. -f lavfi -i "movie=filename=$INPUT:loop=$ITERATIONS, setpts=N/(FRAME_RATE*TB)" \
  48. -i "$LOGO" \
  49. -filter_complex \
  50. "[0:v]scale=iw*sar*$HEIGHT/ih:$HEIGHT,setsar=1[bg];
  51. [bg][1:v]overlay=main_w-overlay_w-20:main_h-overlay_h-20[v]" \
  52. -map '[v]' \
  53. -pix_fmt yuv420p \
  54. -codec:v vp8 -quality realtime -deadline 1000000 -cpu-used "$SPEED_VP8" \
  55. -b:v "$VBITRATE" -minrate "$VBITRATE" -maxrate "$VBITRATE" \
  56. -undershoot-pct 95 -bufsize $((6000*VBITRATE/1000)) -rc_init_occupancy $((4000*VBITRATE/1000)) \
  57. -max-intra-rate 0 \
  58. -qmin 4 -qmax 56 \
  59. -f rtp -payload_type 100 "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1200"