summaryrefslogtreecommitdiff
path: root/bin/decode+edit+encode
blob: fd8869f0e3bb2cc0a45e542871ec79e752be3b17 (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. videosize() { height=$1; width_or_ratio=$2;
  21. case $width_or_ratio in
  22. *:*) num=${width_or_ratio%:*}; den=${width_or_ratio#*:}; width=$((height*num/den));;
  23. *) width=$width_or_ratio;;
  24. esac
  25. echo_n $((height*width))
  26. }
  27. BITS=$(videosize "$HEIGHT" "$RATIO")
  28. # * bitrates and bits in parens based on https://developer.apple.com/library/content/documentation/General/Reference/HLSAuthoringSpec/Requirements.html
  29. # + bits rounded up to include nearby modulo 16 formats
  30. # + best (i.e. high-modulo) 16:9 heights: 216 288 360 432 576 720
  31. # + best (i.e. high-modulo) 4:3 heights: 288 312 384 480 624 816
  32. if [ $BITS -le 110592 ]; then # 234p → 97344
  33. VBITRATE=145000
  34. elif [ $BITS -le 150528 ]; then # 270p → 129600
  35. VBITRATE=365000
  36. elif [ $BITS -le 196608 ]; then # 360p → 172800
  37. VBITRATE=730000
  38. elif [ $BITS -le 331776 ]; then # 432p → 331776
  39. VBITRATE=1100000
  40. elif [ $BITS -le 589824 ]; then # 540p → 518400
  41. VBITRATE=2000000
  42. elif [ $BITS -le 921600 ]; then # 720p → 921600
  43. VBITRATE=3000000
  44. fi
  45. encode_vp8() { bitrate=$1; speed=$2;
  46. echo_n "-codec:v vp8 -quality realtime -deadline 1000000 -cpu-used $speed \
  47. -b:v $bitrate -minrate $bitrate -maxrate $bitrate \
  48. -undershoot-pct 95 -bufsize $((6000*bitrate/1000)) -rc_init_occupancy $((4000*bitrate/1000)) \
  49. -max-intra-rate 0 \
  50. -qmin 4 -qmax 56"
  51. }
  52. mkdir -p $(dirname "$OUTPUT")
  53. # based on http://www.webmproject.org/docs/encoder-parameters/#real-time-cbr-encoding-and-streaming
  54. # + scale+watermark trick based on http://stackoverflow.com/a/10937357
  55. # + scale to square pixels as needed
  56. # + Add 1s latency (deadline)
  57. ffmpeg -hide_banner -threads auto -y -re \
  58. -i "$INPUT" \
  59. -i "$LOGO" \
  60. -filter_complex \
  61. "[0:v]format=pix_fmts=yuv420p,scale=iw*sar*$HEIGHT/ih:$HEIGHT,setsar=1[bg];
  62. [bg][1:v]overlay=main_w-overlay_w-20:main_h-overlay_h-20[v]" \
  63. -map '[v]' \
  64. $(encode_vp8 "$VBITRATE" "$SPEED_VP8") \
  65. -f webm "$OUTPUT"