summaryrefslogtreecommitdiff
path: root/bin/decode+edit+loop+encode+stream
blob: d0888fb7661fe18665a55d1098f5de8c4186e477 (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. # TODO: Vary vpx quality based on height
  17. SPEED_VP8=15
  18. # inspired by Apple HLS recommendations
  19. if [ $HEIGHT -le 234 ]; then VBITRATE=145000;
  20. elif [ $HEIGHT -le 270 ]; then VBITRATE=365000;
  21. elif [ $HEIGHT -le 360 ]; then VBITRATE=730000;
  22. elif [ $HEIGHT -le 432 ]; then VBITRATE=1100000;
  23. elif [ $HEIGHT -le 540 ]; then VBITRATE=2000000;
  24. elif [ $HEIGHT -le 720 ]; then VBITRATE=3000000;
  25. fi
  26. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  27. # + loop-in-filter trick based on http://video.stackexchange.com/a/16933
  28. # + scale+watermark trick based on http://stackoverflow.com/a/10937357
  29. # + scale to square pixels as needed
  30. # + Add 1s latency (deadline)
  31. # + Use same RTP payload types as GStreamer
  32. ffmpeg -hide_banner -threads auto -re \
  33. -f lavfi -i "movie=filename=$INPUT:loop=$ITERATIONS, setpts=N/(FRAME_RATE*TB)" \
  34. -i "$LOGO" \
  35. -filter_complex \
  36. "[0:v]scale=iw*sar*$HEIGHT/ih:$HEIGHT,setsar=1[bg];
  37. [bg][1:v]overlay=main_w-overlay_w-20:main_h-overlay_h-20[v]" \
  38. -map '[v]' \
  39. -pix_fmt yuv420p \
  40. -codec:v vp8 -quality realtime -deadline 1000000 -cpu-used "$SPEED_VP8" \
  41. -b:v "$VBITRATE" -minrate "$VBITRATE" -maxrate "$VBITRATE" \
  42. -undershoot-pct 95 -bufsize $((6000*VBITRATE/1000)) -rc_init_occupancy $((4000*VBITRATE/1000)) \
  43. -max-intra-rate 0 \
  44. -qmin 4 -qmax 56 \
  45. -f rtp -payload_type 100 "rtp://$IP:$((FIRSTPORT+2))?pkt_size=1200"