// SafeAudit: reports every visible word (and any image you name) that leaves the safe area of a vertical film.
// From AI & Things (https://aiandthings-film-method.vercel.app). Use it in a Remotion film.
//
// 1. Put at the top of your film component, switched on by an input prop:
// {Boolean((getInputProps() as { safeAudit?: boolean }).safeAudit) && }
// 2. Render every frame small, with the prop on, and read the log:
// npx remotion render src/index.ts out/safe.mp4 --props '{"safeAudit":true}' --scale 0.25 --muted --log verbose | grep SAFEOUT
// No SAFEOUT line means nothing left the area. Each line names the text, the frame and its box in film pixels.
//
// The default area is for Instagram Reels at 1080x1920: clear of the top bar, the buttons on the right and the
// caption from y 1490 down. Pass your own for another format.
// Note: a swash or a long tail can draw past its text box. Look at those words at full size as well.
import React, { useLayoutEffect } from 'react';
import { useCurrentFrame, useVideoConfig } from 'remotion';
export type Safe = { left: number; right: number; top: number; bottom: number };
export const REELS_SAFE: Safe = { left: 90, right: 990, top: 230, bottom: 1490 };
export const SafeAudit: React.FC<{ safe?: Safe; images?: string }> = ({ safe = REELS_SAFE, images }) => {
const f = useCurrentFrame();
const { width } = useVideoConfig();
useLayoutEffect(() => {
const k = window.innerWidth / width; // the page may be scaled for the render
const report = (what: string, r: DOMRect) => {
const b = { l: r.left / k, r: r.right / k, t: r.top / k, b: r.bottom / k };
if (b.l < safe.left || b.r > safe.right || b.t < safe.top || b.b > safe.bottom)
console.log('SAFEOUT ' + JSON.stringify({ f, what, l: Math.round(b.l), r: Math.round(b.r), t: Math.round(b.t), b: Math.round(b.b) }));
};
const seen = (el: Element) => {
for (let a: Element | null = el; a; a = a.parentElement) {
const cs = getComputedStyle(a);
if (cs.display === 'none' || cs.visibility === 'hidden' || Number(cs.opacity) < 0.05) return false;
}
return true;
};
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
const range = document.createRange();
for (let n = walker.nextNode(); n; n = walker.nextNode()) {
const text = (n.textContent ?? '').trim();
const el = n.parentElement;
if (!text || !el || el.closest('style, script') || !seen(el)) continue;
range.selectNodeContents(n);
for (const r of Array.from(range.getClientRects())) if (r.width > 1) report(text.slice(0, 40), r);
}
if (images) document.querySelectorAll(images).forEach((im) => seen(im) && report('image', im.getBoundingClientRect()));
});
return null;
};