diff --git a/scripts/library_run.py b/scripts/library_run.py index d396b89..b38a4c5 100644 --- a/scripts/library_run.py +++ b/scripts/library_run.py @@ -210,6 +210,21 @@ SIZE_CEIL = 1.0 # output video bitrate must not exceed the source's CAP_BOUND_SHARE = 0.97 # an encode that spent >= 97% of the ceiling is no longer gear-limited: # below the ceiling a lower CRF buys bits, AT it the encoder is throwing # bits away, so the quality ladder stops being a ladder (owner 2026-10-25) +# THE FLOOR: a verified encode that saves less than this share of the CONTAINER is not worth +# replacing the original for. The ceiling above answers "did it grow?"; this answers "was it worth +# it?" - and until 2026-11-27 only the first question was asked. Measured on five real clips (drone +# 4K, night 4K, an already-delivered 1080p): the savings were 0.9 / 3.1 / 7.7 / 32.1 / 43.7%, and +# the three under 8% were published anyway - 203 s of CPU and 68.5 MB of read to produce 67.9 MB. +# 10.0 is the product promise in one sentence («if it is not at least 10% smaller on disk, your +# original stays where it is»), not an information-theory constant: 5% would still swap the 7.7% +# case, 15% would throw away real 10-14% wins on multi-terabyte archives. Container bytes, NOT +# `growth` (video bitrate), because that is the number the dashboard, the certificate and Explorer +# all show - an audio-heavy carry-over can shrink the video 20% and the container 7%. +# `copy`/remux is exempt at the call sites: it changes the container, not the picture, and its +# ratio is ~100% by construction. +MIN_SAVING_PCT = 10.0 +assert 0.0 <= MIN_SAVING_PCT < 100.0, "MIN_SAVING_PCT is a percentage, not a ratio" + QUALITY_STEPS = 2 # on a quality-gate failure, retry with the quality raised this many # steps (cq -2 / crf -2) before giving up; the retry only fires on a # file the cheaper setting could not carry @@ -1062,6 +1077,50 @@ def tier_for_bpp(b): return "nvenc" if b <= 0.040 else "x265" +def saving_pct(info, out_path): + """(saved_pct, out_bytes, src_bytes) measured on CONTAINER bytes - or None if a size is unknown. + + Container bytes and not `growth` (video bitrate) is deliberate: the dashboard, the printed + certificate and Windows Explorer all show container size, so a floor measured on the video + stream alone can refuse nothing while the user sees a 3% saving - or accept a file whose audio + carry-over left the container bigger. The two numbers are printed side by side in every verdict + line, so neither is hidden. + """ + try: + src_bytes = int(info.get("size") or 0) + out_bytes = os.path.getsize(out_path) if out_path and os.path.exists(out_path) else 0 + except (OSError, TypeError, ValueError): + return None + if src_bytes <= 0 or out_bytes <= 0: + return None + return 100.0 * (src_bytes - out_bytes) / float(src_bytes), out_bytes, src_bytes + + +def floor_refusal(info, loc_out, growth=None, ssim_note=""): + """The publish-worthiness floor: None to publish, or the (status, meta) of a SKIP. + + NOT a `FAIL`: nothing about this attempt was wrong, so the control plane must not count a retry, + park the item or charge the trial quota - see `rt/attempts.py` (`BELOW_FLOOR`) and the `SKIP gain` + branch in the supervisor. `kept_source` is in the meta so the caller cannot mistake it for a + publish, and the staging file is swept like any other attempt's. + """ + got = saving_pct(info, loc_out) + if got is None: + return None + saved, out_bytes, src_bytes = got + if saved >= MIN_SAVING_PCT: + return None + log(" %s saved %.1f%% on disk < %.1f%% floor - keeping the original" + % (ssim_note, saved, MIN_SAVING_PCT)) + meta = {"ratio": round(100.0 * out_bytes / src_bytes, 1), + "saved_pct": round(saved, 1), + "kept_source": True} + if growth is not None: + meta["video_pct"] = round(growth, 1) + return ("SKIP gain: saved %.1f%% < %.1f%% floor (%.1f%% of source)" + % (saved, MIN_SAVING_PCT, 100.0 * out_bytes / src_bytes)), meta + + def out_video_bps(path, dur): r = run([FFPROBE, "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=bit_rate", "-of", "csv=p=0", path], timeout=180) @@ -1903,6 +1962,11 @@ def process(src, args, dest=None, origin=None): if vbps and obps > vbps * SIZE_CEIL: return "FAIL size: output %.0f kbps > source %.0f kbps (%.1f%%)" % ( obps / 1000, vbps / 1000, growth), None + # No picture verdict here, but the floor still applies: a source with a hole in + # its timeline can be perfectly good to convert and still not worth converting. + fr = floor_refusal(info, loc_out, growth, ssim_note) + if fr: + return fr break if not nfr: derr = (sstat or {}).get("decode_error") @@ -1955,6 +2019,12 @@ def process(src, args, dest=None, origin=None): # sources whose playability is the thing being fixed. The frame-share test above is # unchanged: a real defect is still refused at any tier. capped = bool(vbps) and obps >= vbps * SIZE_CEIL * CAP_BOUND_SHARE + # ...and the same "no ladder left" logic for the FLOOR: an attempt that already + # landed above (100 - MIN_SAVING_PCT)% of the source cannot be rescued by a higher + # quality gear, which only spends more bits. Without this, a doomed clip burns a + # second full pass (measured: 203 s for a 23 s 4K clip) to be floor-refused twice. + _sv = saving_pct(info, loc_out) + floor_capped = bool(_sv and _sv[0] < MIN_SAVING_PCT) second_tier = False if (systematic and mean is not None and bad_share <= MAX_BAD_SHARE and mean >= SSIM_MEAN_MIN_FALLBACK and space_reclaim_source(info, src)): @@ -1980,7 +2050,7 @@ def process(src, args, dest=None, origin=None): ", " if why else "", 100.0 * bad_share, BAD_FRAME_SSIM, sstat.get("below", 0), nfr) if systematic: - if q_off < QUALITY_STEPS and (attempt == 1) and not capped: + if q_off < QUALITY_STEPS and (attempt == 1) and not capped and not floor_capped: q_off = QUALITY_STEPS log(" quality gate: %s - retrying at quality+%d" % (why, q_off)) continue @@ -2005,6 +2075,13 @@ def process(src, args, dest=None, origin=None): if vbps and obps > vbps * SIZE_CEIL: return "FAIL size: output %.0f kbps > source %.0f kbps (%.1f%%)" % ( obps / 1000, vbps / 1000, growth), None + # 3c. The publish-worthiness floor. Deliberately AFTER the quality gates: an item is + # refused for being pointless only once it is known to be GOOD (a bad encode is a + # FAIL and answers to retry/park; a pointless one is a SKIP and answers to nobody). + # `tier != "copy"` is the enclosing branch - a remux is never floor-refused. + fr = floor_refusal(info, loc_out, growth, ssim_note) + if fr: + return fr log(" %s %.1f%% of source video" % (ssim_note, growth)) break # 4. upload under a temp name