forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB06bf080Tahinli2 months ago2200 commits

scan-real-library.sh99 lines, 3.8 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# scan-real-library.sh — ffprobe sweep of the user's real media library.
3#
4# READ-ONLY on the scanned directories; the only thing written is the manifest
5# under fixtures/ (gitignored — regenerable). The manifest is the T3 gate input:
6# every codec/container claim in this repo is checked against these files, not
7# only against synthetic fixtures.
8#
9# Usage: scripts/scan-real-library.sh [dir ...] (default: ~/Videos ~/Downloads)
10# Env: EC_FIXTURES=<dir> (default <repo>/fixtures)
11# Out: fixtures/real-library-manifest.tsv
12# columns: path container vcodec width height pix_fmt bit_depth acodecs duration size
13
14set -uo pipefail
15
16ROOT=$(cd -- "$(dirname -- "$0")/.." && pwd)
17FIXTURES=${EC_FIXTURES:-$ROOT/fixtures}
18OUT=$FIXTURES/real-library-manifest.tsv
19
20command -v ffprobe >/dev/null || { echo "scan-real-library: ffprobe not found" >&2; exit 2; }
21
22DIRS=("$@")
23[ "${#DIRS[@]}" -eq 0 ] && DIRS=("$HOME/Videos" "$HOME/Downloads" "$HOME/Music")
24
25EXTS=(mp4 m4v mov mkv webm avi flv ts m2ts mts wmv ogv
26 mp3 m4a aac flac wav ogg oga opus wma ac3 eac3 alac aif aiff)
27
28mkdir -p -- "$FIXTURES"
29tmp=$(mktemp) || exit 2
30trap 'rm -f "$tmp"' EXIT
31
32# Build the -iname predicate list once.
33find_args=()
34for e in "${EXTS[@]}"; do find_args+=(-iname "*.$e" -o); done
35unset 'find_args[${#find_args[@]}-1]'
36
37# field <key> <ffprobe-kv-block> — "" when absent or N/A.
38field() {
39 local v
40 v=$(printf '%s\n' "$2" | sed -n "s/^$1=//p" | head -n1)
41 [ "$v" = "N/A" ] && v=""
42 printf '%s' "$v"
43}
44
45n=0
46nerr=0
47{
48 printf 'path\tcontainer\tvcodec\twidth\theight\tpix_fmt\tbit_depth\tacodecs\tduration\tsize\n'
49 while IFS= read -r -d '' f; do
50 # Key=value form (not nk=1): a field ffprobe omits must not shift the others.
51 fmt=$(ffprobe -v error -show_entries format=format_name,duration \
52 -of default=nw=1 "$f" 2>/dev/null)
53 if [ -z "$fmt" ]; then
54 nerr=$((nerr + 1))
55 printf '%s\tUNREADABLE\t-\t-\t-\t-\t-\t-\t-\t%s\n' "$f" "$(stat -c %s -- "$f")"
56 continue
57 fi
58 container=$(field format_name "$fmt")
59 duration=$(field duration "$fmt")
60 v=$(ffprobe -v error -select_streams v:0 \
61 -show_entries stream=codec_name,width,height,pix_fmt,bits_per_raw_sample \
62 -of default=nw=1 "$f" 2>/dev/null)
63 vcodec=$(field codec_name "$v")
64 vw=$(field width "$v")
65 vh=$(field height "$v")
66 vpix=$(field pix_fmt "$v")
67 vbits=$(field bits_per_raw_sample "$v")
68 # bits_per_raw_sample is often absent/0 — fall back to the pix_fmt suffix.
69 if [ -z "$vbits" ] || [ "$vbits" = "N/A" ] || [ "$vbits" = 0 ]; then
70 case $vpix in
71 *12le | *12be) vbits=12 ;;
72 *10le | *10be) vbits=10 ;;
73 "") vbits="" ;;
74 *) vbits=8 ;;
75 esac
76 fi
77 acodecs=$(ffprobe -v error -select_streams a -show_entries stream=codec_name \
78 -of default=nw=1:nk=1 "$f" 2>/dev/null | paste -sd, -)
79 printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
80 "$f" "${container:--}" "${vcodec:--}" "${vw:--}" "${vh:--}" \
81 "${vpix:--}" "${vbits:--}" "${acodecs:--}" "${duration:--}" \
82 "$(stat -c %s -- "$f")"
83 n=$((n + 1))
84 done < <(find "${DIRS[@]}" -type f \( "${find_args[@]}" \) ! -name "*.export*.mp4" -print0 2>/dev/null)
85} >"$tmp"
86
87mv -- "$tmp" "$OUT"
88trap - EXIT
89
90printf '%s\n' "== $OUT =="
91printf 'scanned=%s unreadable=%s\n\n' "$((n + nerr))" "$nerr"
92printf 'containers:\n'
93tail -n +2 "$OUT" | cut -f2 | sort | uniq -c | sort -rn
94printf '\nvideo codecs:\n'
95tail -n +2 "$OUT" | cut -f3 | sort | uniq -c | sort -rn
96printf '\naudio codecs (per file, comma-joined):\n'
97tail -n +2 "$OUT" | cut -f8 | tr ',' '\n' | sort | uniq -c | sort -rn
98printf '\nbit depths:\n'
99tail -n +2 "$OUT" | cut -f7 | sort | uniq -c | sort -rn