summaryrefslogtreecommitdiff
path: root/bin/decode+edit+encode
blob: 5fb0b20eb787443fc290db1fe24cae3f98d9b7f8 (plain)
  1. #!/bin/sh
  2. # FIXME: Handle audio
  3. set -e
  4. ##
  5. ## defaults
  6. ##
  7. INPUT=../src/omni/show0/earth.mp4
  8. LOGO=../../content/icon_small.png
  9. OUTPUT=../tmp/omni/show0/earth.webm
  10. RATIO=4:3
  11. ##
  12. ## generic functions
  13. ##
  14. # shellcheck disable=SC2048,SC2059
  15. echo_n() {
  16. printf -- "$*"
  17. }
  18. HEIGHT=360
  19. SPEED_VP8=4
  20. # * bitrates and bits in parens based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  21. # + bits rounded up to include nearby modulo 16 formats
  22. # + best (i.e. high-modulo) 16:9 heights: 216 288 360 432 576 720
  23. # + best (i.e. high-modulo) 4:3 heights: 288 312 384 480 624 816
  24. RATIO_NUM=${RATION_NUM:-16}
  25. RATIO_DEN=${RATION_DEN:-9}
  26. WIDTH=${WIDTH:-$((HEIGHT*RATIO_NUM/RATIO_DEN))}
  27. BITS=$((WIDTH*HEIGHT))
  28. if [ $BITS -le 110592 ]; then # 234p → 97344
  29. VBITRATE=145000
  30. elif [ $BITS -le 150528 ]; then # 270p → 129600
  31. VBITRATE=365000
  32. elif [ $BITS -le 196608 ]; then # 360p → 172800
  33. VBITRATE=730000
  34. elif [ $BITS -le 331776 ]; then # 432p → 331776
  35. VBITRATE=1100000
  36. elif [ $BITS -le 589824 ]; then # 540p → 518400
  37. VBITRATE=2000000
  38. elif [ $BITS -le 921600 ]; then # 720p → 921600
  39. VBITRATE=3000000
  40. fi
  41. encode_vp8() { bitrate=$1; speed=$2;
  42. echo_n "-codec:v vp8 -quality realtime -deadline 1000000 -cpu-used $speed \
  43. -b:v $bitrate -minrate $bitrate -maxrate $bitrate \
  44. -undershoot-pct 95 -bufsize $((6000*bitrate/1000)) -rc_init_occupancy $((4000*bitrate/1000)) \
  45. -max-intra-rate 0 \
  46. -qmin 4 -qmax 56"
  47. }
  48. mkdir -p $(dirname "$OUTPUT")
  49. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  50. # + scale+watermark trick based on http://stackoverflow.com/a/10937357
  51. # + scale to square pixels as needed
  52. # + Add 1s latency (deadline)
  53. ffmpeg -hide_banner -threads auto -y -re \
  54. -i "$INPUT" \
  55. -i "$LOGO" \
  56. -filter_complex \
  57. "[0:v]format=pix_fmts=yuv420p,scale=iw*sar*$HEIGHT/ih:$HEIGHT,setsar=1[bg];
  58. [bg][1:v]overlay=main_w-overlay_w-20:main_h-overlay_h-20[v]" \
  59. -map '[v]' \
  60. $(encode_vp8 "$VBITRATE" "$SPEED_VP8") \
  61. -f webm "$OUTPUT"