forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh73 lines, 3.1 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: fill fuzz/corpus/<target>/ from the repo's mp4 fixtures (root fixtures/).
3#
4# demux_mp4 gets every mp4/m4a/mov fixture at 1 MiB or less as-is. Larger ones are
5# re-muxed with `ffmpeg -t 0.2 -c copy` (a few frames, still a real moov
6# with its sample tables), so the picture tracks are seeded too.
7# esds_parse gets the payload of the first `esds` box in each seed, after its
8# version/flags word, found by a plain byte search for the box type.
9# is_mp4 gets the first 64 bytes of each seed.
10#
11# Corpus lives under fuzz/corpus, which is git-ignored.
12set -euo pipefail
13
14FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
15FIXTURES=$(cd -- "$FUZZ_DIR/../../.." && pwd)/fixtures
16[ -d "$FIXTURES" ] || { echo "seed.sh: $FIXTURES missing; run scripts/link-fixtures.sh" >&2; exit 2; }
17command -v ffmpeg >/dev/null || { echo "seed.sh: ffmpeg needed for the small video seeds" >&2; exit 2; }
18
19MP4_DIR="$FUZZ_DIR/corpus/demux_mp4"
20ESDS_DIR="$FUZZ_DIR/corpus/esds_parse"
21HEAD_DIR="$FUZZ_DIR/corpus/is_mp4"
22TMP=$(mktemp -d "${TMPDIR:-/tmp}/ec-mp4-seed.XXXXXX")
23trap 'rm -rf "$TMP"' EXIT
24mkdir -p "$MP4_DIR" "$ESDS_DIR" "$HEAD_DIR"
25
26LIMIT=$((1024 * 1024))
27for src in "$FIXTURES"/audio/*.mp4 "$FIXTURES"/audio/*.m4a "$FIXTURES"/audio/*.mov \
28 "$FIXTURES"/video/*.mp4 "$FIXTURES"/video/*.mov "$FIXTURES"/video/*.m4v; do
29 [ -e "$src" ] || continue
30 stem=$(basename "$src"); stem=${stem%.*}
31 if [ "$(stat -c %s "$src")" -le "$LIMIT" ]; then
32 cp "$src" "$MP4_DIR/$stem"
33 else
34 # Shorten until it fits: a 4K intra frame alone can be most of a megabyte.
35 cut=""
36 for secs in 0.2 0.05 0.01; do
37 ffmpeg -v error -y -t "$secs" -i "$src" -c copy -map 0 -movflags +faststart "$TMP/$stem.mp4" \
38 || { echo "seed.sh: ffmpeg could not cut $src" >&2; exit 2; }
39 if [ "$(stat -c %s "$TMP/$stem.mp4")" -le "$LIMIT" ]; then cut=1; break; fi
40 done
41 [ -n "$cut" ] || { echo "seed.sh: $stem does not cut under 1 MiB" >&2; exit 2; }
42 cp "$TMP/$stem.mp4" "$MP4_DIR/$stem"
43 fi
44done
45
46python3 -I - "$MP4_DIR" "$ESDS_DIR" "$HEAD_DIR" <<'PY'
47import os, sys
48
49mp4_dir, esds_dir, head_dir = sys.argv[1], sys.argv[2], sys.argv[3]
50
51def find_esds(data):
52 """Payload after version/flags of the first `esds` box, or None."""
53 at = data.find(b"esds")
54 while at != -1:
55 start = at - 4
56 if start >= 0:
57 size = int.from_bytes(data[start:start + 4], "big")
58 if 12 <= size <= len(data) - start:
59 return data[start + 12:start + size]
60 at = data.find(b"esds", at + 1)
61 return None
62
63count = {"esds": 0, "head": 0}
64for name in sorted(os.listdir(mp4_dir)):
65 data = open(os.path.join(mp4_dir, name), "rb").read()
66 open(os.path.join(head_dir, name), "wb").write(data[:64])
67 count["head"] += 1
68 payload = find_esds(data)
69 if payload is not None:
70 open(os.path.join(esds_dir, name), "wb").write(payload)
71 count["esds"] += 1
72print(f"seeded {len(os.listdir(mp4_dir))} demux_mp4, {count['esds']} esds_parse, {count['head']} is_mp4 seeds")
73PY