mp4-startup-check

An offline delivery gate for progressive MP4 files. Check that initialization
metadata precedes media, fits your byte budget, and that complete sample chunks
are inside declared media data. No codecs, network, or runtime dependencies needed.
A video can play on your workstation while its moov metadata sits at the end of
the file. A bad offset rewrite can also leave chunk starts—or just their ends—
outside mdat. Run this check before uploading static lessons, demos, or previews.
Install
Python 3.11+ on Windows, macOS, or Linux. Published on GitHub, not PyPI:
python -m pip install "git+https://github.com/KanadeK/[email protected]"
mp4-startup-check video.mp4
mp4-startup-check video.mp4 --max-init-bytes 524288
Or clone and run python -m pip install .. python -m mp4_startup_check works when
the scripts directory is not on PATH. Normal operation does not require FFmpeg.
A runnable producer-to-check demo
From a clone, install optional development dependencies to generate real test
media. This downloads imageio-ffmpeg and its platform binary:
python -m pip install -e ".[dev]"
python examples/demo.py
mp4-startup-check .demo/fast.mp4
mp4-startup-check .demo/tail.mp4
mp4-startup-check .demo/broken-offset.mp4
The generated video is a tiny test pattern with a synthetic tone. fast.mp4 exits
0; tail.mp4 exits 1 with moov_after_media; broken-offset.mp4 exits 1
with chunk_outside_mdat. The generator refuses an existing .demo directory.
Tests independently decode both valid files with FFmpeg: a policy failure is not
automatically a broken codec stream.
What is checked
- Top-level box bounds, normal and 64-bit extended sizes, final size-zero
mdat.
- One
ftyp, one moov, at least one mdat, 1–128 nonempty tracks.
ftyp before moov, and the entire moov before the first mdat.
- A sequential-prefix budget through the end of
moov: default 1,048,576 bytes.
This includes preceding free space. It measures metadata placement, not
network time, first-frame size, or the actual HTTP range requests of a player.
- Each track’s
stsc, stsz, and stco or co64: counts, mapping order,
description indices, exact sample accounting, and complete per-chunk byte spans.
- Each chunk must fit within one
mdat payload. Being somewhere between the
first and last media boxes is insufficient. Box headers are not media payload.
- Sample-description data references must be self-contained
url entries.
Tables are read in bounded blocks. Media payloads are skipped, so a multi-gigabyte
file need not be decoded or loaded into memory. There is no repair or rewrite step.
Output and CI
JSON reports initialization bytes, media ranges, per-track sample/chunk counts,
and byte-range witnesses for failures. Track numbers are one-based moov order,
not embedded track IDs. No title metadata or absolute path is included in reports.
| Exit |
Meaning |
| 0 |
All supported delivery checks pass |
| 1 |
Placement/budget policy failed or chunks extend outside media payloads |
| 2 |
Malformed selected structure, unsupported format, unreadable input, or limits |
Errors go to stderr. Unsupported inputs never receive a successful partial report.
If more than 1,000 findings exist, the report includes an exact total and omitted
count; processing does not silently turn them into a pass.
Example in a workflow after producing the file:
mp4-startup-check build/lesson.mp4 --max-init-bytes 1048576 > delivery-report.json
The process status is the gate. Do not redirect the report over your media file.
Limits of v0.1.0
This is not a full MP4 conformance, codec, corruption, or playback validator.
It does not parse timing/edit lists, decode samples, check sync-frame timing,
validate codec configuration, validate every unknown child box, detect corrupt
payload bytes, measure CDN/HTTP behavior, or reject overlapping byte reuse.
init_prefix_bytes is specifically the end of moov, not bytes to first playback.
Fragmented MP4 (moof/mvex), compressed cmov, encrypted sample entries,
external data references, compact stz2 sizes and empty tracks are explicitly
unsupported. Use ordinary self-contained, unfragmented MP4 with stsz tables.
Generic sample-entry payloads remain opaque; the presence of a particular codec
does not imply support by any browser.
Resource limits: 64 MiB moov, 100,000 visited boxes, one million samples/entries
per table and four million counted entries across the file. The box and table
limits bound memory/work. Files should be immutable during the scan; size/mtime
checks are useful diagnostics, not an atomic snapshot or adversarial-write defense.
For rewriting metadata placement use FFmpeg’s faststart option.
For general structural inspection use Bento4 mp4dump.
This project supplies a small, deterministic delivery policy and byte-range report
instead of editing files or dumping all metadata. Its table interpretation follows
Apple’s chunk-offset
and sample-to-chunk descriptions.
Development and cleanup
See CONTRIBUTING.md. Run python -m unittest discover -s tests -v
after installing .[dev]; FFmpeg integration tests are required, not silently
skipped. Structural fixtures are original minimal byte layouts; the separate
FFmpeg fixtures are real encoded/decoded audio/video.
Uninstall: python -m pip uninstall mp4-startup-check. Remove your generated demo
and reports when no longer needed. If installed solely for testing, the optional
imageio-ffmpeg, build, coverage and ruff packages can also be uninstalled.
No background services or application caches are created. Runtime code is MIT;
FFmpeg and development tools keep their upstream licenses and are not bundled in
this repository or its release archives.
中文说明
这是普通渐进下载 MP4 的离线交付检查:验证 moov 在媒体数据之前、初始化前缀满足
字节预算,并根据样本表计算完整块范围,检查是否落在单个 mdat 有效载荷内。
不读取媒体内容、不转码、不联网、不修改原文件。当前不支持分片、加密、外部媒体引用
和 stz2;这些输入会报错而非误报通过。通过只代表列出的封装与交付条件成立,
不代表视频可解码或浏览器一定能够播放。
评论