File size: 4,204 Bytes
dd56ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bash
#
# VSTAT — Apply privacy redactions (black boxes) to YouTube clips.
#
# Coordinates are in 640x360 (post-scale) space, matching the official
# VSTAT release. After this script runs, every redaction-target chunk
# lives at the canonical `videos/youtube/<cat>/<id>_pt*.mp4` path with
# the black boxes baked in. Any leftover `*_redacted.mp4` siblings from
# earlier runs are removed.
#
# Usage (run from vstat/ root):
#   ./scripts/redact.sh
#   ./scripts/redact.sh --dry-run
#

set -eo pipefail

DRY=0
for arg in "$@"; do
  case "$arg" in
    --dry-run) DRY=1 ;;
    -h|--help) sed -n '2,15p' "$0"; exit 0 ;;
    *) echo "Unknown arg: $arg" >&2; exit 1 ;;
  esac
done

# Format:  "<canonical-glob>|<ffmpeg drawbox filter>"
RULES=(
  # tennis 0001
  "videos/youtube/tennis/0001_pt*.mp4|drawbox=x=32:y=306:w=134:h=38:color=black:t=fill"
  # tennis 0002
  "videos/youtube/tennis/0002_pt*.mp4|drawbox=x=32:y=306:w=134:h=38:color=black:t=fill"
  # basketball 0002
  "videos/youtube/basketball/0002_pt*.mp4|drawbox=x=98:y=323:w=443:h=37:color=black:t=fill"
  # basketball 0003
  "videos/youtube/basketball/0003_pt*.mp4|drawbox=x=22:y=285:w=125:h=63:color=black:t=fill"
  # soccer 0002 (two boxes)
  "videos/youtube/soccer/0002_pt*.mp4|drawbox=x=0:y=0:w=122:h=19:color=black:t=fill,drawbox=x=140:y=320:w=360:h=40:color=black:t=fill"
)

if ! command -v ffmpeg >/dev/null 2>&1; then
  echo "ERROR: ffmpeg not installed. brew install ffmpeg / apt install ffmpeg" >&2
  exit 1
fi

# Expand a canonical glob to the union of canonical paths it covers,
# accounting for files that currently live with `_redacted` suffix.
# Compatible with bash 3.2 (macOS default) — no associative arrays.
canonical_paths_for_glob() {
  glob="$1"
  dir=$(dirname "$glob")
  pat=$(basename "$glob")                          # 0001_pt*.mp4
  shopt -s nullglob
  results=""
  for f in "$dir"/$pat "$dir"/${pat%.mp4}_redacted.mp4; do
    [ -e "$f" ] || continue
    case "$f" in
      *_redacted.mp4) base="${f%_redacted.mp4}.mp4" ;;
      *)              base="$f" ;;
    esac
    results="${results}${base}"$'\n'
  done
  shopt -u nullglob
  printf '%s' "$results" | sort -u | sed '/^$/d'
}

total=0; ok=0; fail=0; missing=0

for rule in "${RULES[@]}"; do
  glob="${rule%%|*}"
  filt="${rule#*|}"
  echo
  echo "=== $glob ==="
  echo "    filter: $filt"

  paths=$(canonical_paths_for_glob "$glob")
  if [ -z "$paths" ]; then
    echo "    (no matching files)"
    continue
  fi

  while IFS= read -r canon; do
    [ -z "$canon" ] && continue
    total=$((total+1))
    redacted_sibling="${canon%.mp4}_redacted.mp4"

    # Pick input: prefer existing _redacted sibling, fall back to canonical.
    if [ -f "$redacted_sibling" ]; then
      input="$redacted_sibling"
    elif [ -f "$canon" ]; then
      input="$canon"
    else
      echo "  ! missing both $canon and $redacted_sibling"
      missing=$((missing+1))
      continue
    fi

    if [ "$DRY" -eq 1 ]; then
      echo "  [DRY] $input  =[$filt]=>  $canon"
      ok=$((ok+1))
      continue
    fi

    # Write to a temp file then atomically swap.
    # `-nostdin` is REQUIRED here: without it ffmpeg consumes characters
    # from the surrounding `while read` loop's stdin, mangling later paths.
    tmp="${canon%.mp4}.redact.tmp.mp4"
    if ! ffmpeg -y -loglevel error -nostdin -i "$input" -vf "$filt" -c:a copy "$tmp"; then
      echo "  ! ffmpeg failed: $input"
      fail=$((fail+1))
      continue
    fi
    if cp -f "$tmp" "$canon" && rm -f "$tmp"; then
      :
    else
      echo "  ! could not write $canon (kept tmp at $tmp)"
      fail=$((fail+1))
      continue
    fi
    # Drop misleading `_redacted.mp4` sibling now that the canonical file
    # holds the actually-redacted content.
    if [ -f "$redacted_sibling" ] && [ "$redacted_sibling" != "$canon" ]; then
      rm -f "$redacted_sibling" 2>/dev/null || true
    fi
    ok=$((ok+1))
    echo "  -> $canon"
  done <<< "$paths"
done

echo
echo "============================================================"
echo "  total: $total   ok: $ok   fail: $fail   missing: $missing"
echo "============================================================"
[ "$fail" -eq 0 ] && [ "$missing" -eq 0 ]