video-shrink
Shrink videos to a target file size with a quality-first ladder (stream copy, then same-resolution NVENC/x264 at low CQ, then downscale only as a last resort). Use when a video exceeds an upload or chat attachment limit (Feishu/Lark 28MB, Discord 25MB, Telegram 50MB, email), when asked to "compress video", "make it smaller", "fit under X MB", "压到28M以内", "视频太大发不出去", or when preparing 4K/HDR phone footage for delivery.
Latest: v1.0.0
Adds a new video-shrink skill for fitting videos under a target file size while preserving quality where possible.
- Tries lossless stream copy first, then same-resolution encodes, and downscales only as a last resort.
- Supports NVENC with an x264 fallback, optional HEVC, audio stripping, HDR-to-SDR tonemapping, and a 1080p preference.
- Provides single-file and batch usage, with text or JSON results and an exit code indicating whether files fit.
Video Shrink
Reduce a video to a hard size limit without throwing away resolution unless it is the only way to fit. Encodes with NVIDIA NVENC when available and falls back to libx264.
Why this order matters
Re-encoding always loses something; only -c copy is lossless. So the ladder
tries, in order:
- Stream copy (remux / strip audio) — truly lossless, instant.
- Same resolution, CQ 19 (visually lossless; SSIM ~0.986 on 4K phone footage).
- Same resolution, CQ 22, then CQ 24 — still same pixel count.
- Short edge 1080, CQ 20 then CQ 24 — first real quality drop.
- Short edge 720, CQ 24 — last resort.
Each rung computes a maxrate ceiling from the size budget and duration, so a
single pass lands close to the target instead of overshooting.
Usage
python scripts/video_shrink.py input.mp4 --limit-mb 28 --out output.mp4
Common flags:
# keep the audio track (default: keep; --strip-audio removes it)
python scripts/video_shrink.py in.mp4 --limit-mb 28 --out out.mp4
# drop audio entirely (silent clips, GIF-like loops)
python scripts/video_shrink.py in.mp4 --limit-mb 28 --out out.mp4 --strip-audio
# force H.265 for archives (smaller, worse compatibility)
python scripts/video_shrink.py in.mp4 --limit-mb 28 --out out.mp4 --codec hevc
# prefer 1080 short edge even if 4K would fit (smoother phone playback)
python scripts/video_shrink.py in.mp4 --limit-mb 28 --out out.mp4 --prefer-1080
# machine-readable result for automation
python scripts/video_shrink.py in.mp4 --limit-mb 28 --out out.mp4 --json
Batch:
python scripts/video_shrink.py "clips/*.mp4" --limit-mb 28 --out-dir shrunk/
Output contract
Prints one [SHRINK] line per file and a final [SUMMARY]:
[SHRINK] in.mp4 2160x3840 29.3s 109.7MB -> 24.9MB tier=cq19@orig 2160x3840
[SUMMARY] ok=1 over=0 total_in=109.7MB total_out=24.9MB
Exit code is 0 when every file fits, 1 when at least one still exceeds the limit after the whole ladder.
With --json, emits a JSON object per file:
{"input":"in.mp4","output":"out.mp4","limit_mb":28,"input_mb":109.7,
"output_mb":24.9,"tier":"cq19@orig","width":2160,"height":3840,
"duration":29.3,"fits":true,"encoder":"h264_nvenc"}
Rules of thumb
- Feishu/Lark caps video attachments at 28MB, so use
--limit-mb 26to leave headroom; a file at 27.9MB can still be rejected server-side. - Deliver H.264 High, not HEVC: HEVC is 35-40% smaller at equal quality but
breaks on some Feishu/iOS/Android clients. Use
--codec hevconly for archives or platforms that accept it. - Never hardcode a resolution. The script reads the real display size (including rotation) and keeps aspect ratio, even dimensions.
- HDR sources (HLG/PQ, 10-bit) should be tonemapped to SDR before shrinking for
chat delivery, otherwise players show washed-out or blown-out color. The
script detects HDR and tonemaps with zscale+hable automatically; pass
--keep-hdrto skip it. - Keep the pre-shrink master. Shrinking a shrunk file compounds artifacts.
- For long videos (5+ minutes) at 4K, even CQ 19 will not fit 28MB; the ladder will reach 1080p or 720p, which is the correct trade-off.
Requirements
ffmpegandffprobeon PATH withh264_nvenc/hevc_nvencfor GPU speed; pure-software x264 fallback works everywhere.- Python 3.9+ (stdlib only).
