finalize_delivery.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. usage() {
  4. printf 'Usage: %s rendered.mp4 output-dir output-stem\n' "$(basename "$0")" >&2
  5. }
  6. die() {
  7. printf 'ERROR: %s\n' "$*" >&2
  8. exit 1
  9. }
  10. [[ "$#" -eq 3 ]] || { usage; exit 64; }
  11. INPUT="$1"
  12. OUTPUT_DIR="$2"
  13. STEM="$3"
  14. [[ -f "$INPUT" && -s "$INPUT" ]] || die "input render is missing or empty: $INPUT"
  15. [[ "$STEM" =~ ^[A-Za-z0-9._-]+$ ]] || die "output stem contains unsupported characters: $STEM"
  16. for command in ffmpeg ffprobe jq mktemp awk sed; do
  17. command -v "$command" >/dev/null 2>&1 || die "required command unavailable: $command"
  18. done
  19. mkdir -p "$OUTPUT_DIR"
  20. OUTPUT_DIR="$(cd "$OUTPUT_DIR" && pwd -P)"
  21. INPUT="$(cd "$(dirname "$INPUT")" && pwd -P)/$(basename "$INPUT")"
  22. MASTER="$OUTPUT_DIR/${STEM}-master.mp4"
  23. SHARE="$OUTPUT_DIR/${STEM}-share.mp4"
  24. REPORT="$OUTPUT_DIR/${STEM}-delivery-report.json"
  25. CONTACT="$OUTPUT_DIR/${STEM}-contact-sheet.png"
  26. # Keep shared reports portable and avoid exposing the developer machine path.
  27. INPUT_REPORT="$(basename "$INPUT")"
  28. MASTER_REPORT="$(basename "$MASTER")"
  29. SHARE_REPORT="$(basename "$SHARE")"
  30. CONTACT_REPORT="$(basename "$CONTACT")"
  31. for output in "$MASTER" "$SHARE" "$REPORT" "$CONTACT"; do
  32. [[ ! -e "$output" ]] || die "refusing to overwrite existing output: $output"
  33. done
  34. TMP_ROOT="${TMPDIR:-${HERMES_HOME:-$HOME/.hermes}/cache/scratch}"
  35. mkdir -p "$TMP_ROOT"
  36. TMP_DIR="$(mktemp -d "${TMP_ROOT%/}/presenter-finalize.XXXXXX")"
  37. trap 'rm -rf -- "$TMP_DIR"' EXIT INT TERM
  38. STREAM_JSON="$TMP_DIR/source-probe.json"
  39. ffprobe -v error -show_streams -show_format -of json "$INPUT" >"$STREAM_JSON"
  40. jq -e '.streams | any(.codec_type == "video") and any(.codec_type == "audio")' "$STREAM_JSON" >/dev/null \
  41. || die "input must contain decodable video and audio streams"
  42. WIDTH="$(jq -r '[.streams[] | select(.codec_type == "video")][0].width' "$STREAM_JSON")"
  43. HEIGHT="$(jq -r '[.streams[] | select(.codec_type == "video")][0].height' "$STREAM_JSON")"
  44. FPS="$(jq -r '[.streams[] | select(.codec_type == "video")][0].avg_frame_rate' "$STREAM_JSON")"
  45. [[ "$FPS" != "0/0" && -n "$FPS" ]] || FPS="30"
  46. MEASURE_LOG="$TMP_DIR/loudnorm-pass1.log"
  47. ffmpeg -hide_banner -nostdin -nostats -i "$INPUT" \
  48. -map 0:a:0 -vn \
  49. -af 'loudnorm=I=-16:TP=-1.5:LRA=9:print_format=json' \
  50. -f null - >/dev/null 2>"$MEASURE_LOG"
  51. MEASURE_JSON="$TMP_DIR/measure.json"
  52. sed -n '/^{/,/^}/p' "$MEASURE_LOG" >"$MEASURE_JSON"
  53. jq -e . "$MEASURE_JSON" >/dev/null || die "could not parse loudness measurement"
  54. MEASURED_I="$(jq -r .input_i "$MEASURE_JSON")"
  55. MEASURED_TP="$(jq -r .input_tp "$MEASURE_JSON")"
  56. MEASURED_LRA="$(jq -r .input_lra "$MEASURE_JSON")"
  57. MEASURED_THRESH="$(jq -r .input_thresh "$MEASURE_JSON")"
  58. OFFSET="$(jq -r .target_offset "$MEASURE_JSON")"
  59. LOUDNORM="loudnorm=I=-16:TP=-1.5:LRA=9:measured_I=${MEASURED_I}:measured_TP=${MEASURED_TP}:measured_LRA=${MEASURED_LRA}:measured_thresh=${MEASURED_THRESH}:offset=${OFFSET}:linear=true:print_format=summary"
  60. VIDEO_FILTER="scale=trunc(iw/2)*2:trunc(ih/2)*2:flags=lanczos,setsar=1,fps=${FPS},format=yuv420p"
  61. encode() {
  62. local crf="$1"
  63. local preset="$2"
  64. local audio_bitrate="$3"
  65. local destination="$4"
  66. local temporary="$5"
  67. ffmpeg -hide_banner -nostdin -loglevel warning -stats -i "$INPUT" \
  68. -map 0:v:0 -map 0:a:0 -sn -dn \
  69. -vf "$VIDEO_FILTER" -af "$LOUDNORM" \
  70. -c:v libx264 -preset "$preset" -crf "$crf" -profile:v high \
  71. -pix_fmt yuv420p -tag:v avc1 -fps_mode cfr \
  72. -color_primaries bt709 -color_trc bt709 -colorspace bt709 \
  73. -c:a aac -b:a "$audio_bitrate" -ar 48000 -ac 2 \
  74. -map_metadata -1 -map_chapters -1 -movflags +faststart \
  75. "$temporary"
  76. [[ -s "$temporary" ]] || die "encoder produced an empty output"
  77. mv -- "$temporary" "$destination"
  78. }
  79. encode 16 slow 256k "$MASTER" "$TMP_DIR/master.mp4"
  80. encode 24 medium 160k "$SHARE" "$TMP_DIR/share.mp4"
  81. for file in "$MASTER" "$SHARE"; do
  82. ffmpeg -hide_banner -nostdin -v error -xerror -i "$file" \
  83. -map 0:v:0 -map 0:a:0 -f null - >/dev/null
  84. done
  85. ffprobe -v error -show_streams -show_format -of json "$MASTER" >"$TMP_DIR/master-probe.json"
  86. ffprobe -v error -show_streams -show_format -of json "$SHARE" >"$TMP_DIR/share-probe.json"
  87. for probe_file in "$STREAM_JSON" "$TMP_DIR/master-probe.json" "$TMP_DIR/share-probe.json"; do
  88. jq 'if .format.filename? then .format.filename = (.format.filename | split("/") | last) else . end' \
  89. "$probe_file" >"${probe_file}.portable"
  90. mv -- "${probe_file}.portable" "$probe_file"
  91. done
  92. ffmpeg -hide_banner -nostdin -i "$MASTER" \
  93. -vf 'blackdetect=d=0.10:pix_th=0.02' -an -f null - \
  94. >/dev/null 2>"$TMP_DIR/blackdetect.log"
  95. BLACK_EVENTS="$(grep -c 'black_start:' "$TMP_DIR/blackdetect.log" || true)"
  96. DURATION="$(ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 "$MASTER")"
  97. awk -v duration="$DURATION" 'BEGIN {
  98. p[1]=0.2; p[2]=duration*0.125; p[3]=duration*0.25; p[4]=duration*0.375;
  99. p[5]=duration*0.5; p[6]=duration*0.625; p[7]=duration*0.75;
  100. p[8]=duration*0.875; p[9]=duration-0.2;
  101. for(i=1;i<=9;i++) printf "%.6f\n", p[i]
  102. }' >"$TMP_DIR/timestamps.txt"
  103. if (( WIDTH > HEIGHT )); then
  104. CONTACT_FRAME_FILTER='scale=480:270:force_original_aspect_ratio=decrease:flags=lanczos,pad=480:270:(ow-iw)/2:(oh-ih)/2:color=black'
  105. elif (( HEIGHT > WIDTH )); then
  106. CONTACT_FRAME_FILTER='scale=270:480:force_original_aspect_ratio=decrease:flags=lanczos,pad=270:480:(ow-iw)/2:(oh-ih)/2:color=black'
  107. else
  108. CONTACT_FRAME_FILTER='scale=360:360:force_original_aspect_ratio=decrease:flags=lanczos,pad=360:360:(ow-iw)/2:(oh-ih)/2:color=black'
  109. fi
  110. index=0
  111. while IFS= read -r timestamp; do
  112. index=$((index + 1))
  113. printf -v frame '%s/frame-%02d.png' "$TMP_DIR" "$index"
  114. ffmpeg -hide_banner -nostdin -loglevel error -ss "$timestamp" -i "$MASTER" \
  115. -frames:v 1 -vf "$CONTACT_FRAME_FILTER" -update 1 "$frame"
  116. done <"$TMP_DIR/timestamps.txt"
  117. ffmpeg -hide_banner -nostdin -loglevel error -framerate 1 -start_number 1 \
  118. -i "$TMP_DIR/frame-%02d.png" -frames:v 1 \
  119. -vf 'tile=3x3:padding=12:margin=12:color=0x101218' -update 1 "$TMP_DIR/contact.png"
  120. mv -- "$TMP_DIR/contact.png" "$CONTACT"
  121. jq -n \
  122. --arg status verified \
  123. --arg input "$INPUT_REPORT" \
  124. --arg master "$MASTER_REPORT" \
  125. --arg share "$SHARE_REPORT" \
  126. --arg contact "$CONTACT_REPORT" \
  127. --arg duration "$DURATION" \
  128. --argjson black_events "$BLACK_EVENTS" \
  129. --slurpfile measured "$MEASURE_JSON" \
  130. --slurpfile source_probe "$STREAM_JSON" \
  131. --slurpfile master_probe "$TMP_DIR/master-probe.json" \
  132. --slurpfile share_probe "$TMP_DIR/share-probe.json" \
  133. '{status:$status,input:$input,master:$master,share:$share,contact_sheet:$contact,duration_s:($duration|tonumber),source_loudness:$measured[0],source_probe:$source_probe[0],master_probe:$master_probe[0],share_probe:$share_probe[0],full_decode_passed:true,black_frame_events:$black_events}' \
  134. >"$TMP_DIR/report.json"
  135. mv -- "$TMP_DIR/report.json" "$REPORT"
  136. printf 'Master: %s\nShare: %s\nReport: %s\nContact: %s\n' "$MASTER" "$SHARE" "$REPORT" "$CONTACT"