Compare commits

...
2 Commits
Author SHA1 Message Date
shinyobjectz 9c89470125 One size is one size however it's told: a name's 9x16, a 1080x1920 video, never a still
Told by pixels alone, an ad named _9x16 beside its own 1080x1920 renders and a 540x960 still read as three sizes, and an agent with one size made couldn't stop for its sign-off.
2026-12-01 18:28:58 -08:00
shaneandshinyobjectz d0f09511fe A file of another codec is refused from its moov box, not after the whole file is read (#294)
On Linux every open of an HEVC file tried the portable backend first, which read the whole file into memory to learn
it wasn't H.264 (0.1.16-rc4 on a Sprite: a cold probe of a 1.1 GB interview still took minutes, warm 1.8 s). The
sample entry is read from the moov box first. rotation_of read the whole file for a matrix: the moov box alone.

Co-authored-by: shinyobjectz <[email protected]>
2026-12-01 12:38:36 -08:00
4 changed files with 84 additions and 2 deletions

No files matched your search

+22 -1
View File
@@ -58,7 +58,23 @@ pub struct Track {
/// A file's first video track's display rotation (clockwise degrees; 0 when it has none or isn't an MP4/MOV).
pub fn rotation_of(path: &Path) -> i32 {
std::fs::read(path).map(|b| mp4_rotation(&b)).unwrap_or(0)
// the moov box alone, as a file of that one box (the whole file was read for it: gigabytes, for a matrix)
let Some(moov) = raea_io_codec::mp4box::moov_of(path) else { return 0 };
let mut b = ((moov.len() + 8) as u32).to_be_bytes().to_vec();
b.extend_from_slice(b"moov");
b.extend_from_slice(&moov);
mp4_rotation(&b)
}
/// The first video track's sample entry (avc1, hvc1, av01, ...), from the moov box alone.
fn video_fourcc(path: &Path) -> Option<[u8; 4]> {
use raea_io_codec::mp4box::{boxes, child, moov_of};
let moov = moov_of(path)?;
boxes(&moov).into_iter().filter(|(k, _)| k == b"trak").find_map(|(_, trak)| {
let mdia = child(trak, b"mdia")?;
(child(mdia, b"hdlr")?.get(8..12)? == b"vide").then_some(())?;
child(child(child(mdia, b"minf")?, b"stbl")?, b"stsd")?.get(12..16)?.try_into().ok()
})
}
/// The first video track's display rotation, from its tkhd matrix (QuickTime's 16.16 a, b, c, d).
@@ -107,6 +123,11 @@ fn mp4_rotation(b: &[u8]) -> i32 {
}
pub fn demux(path: &Path, want: &str) -> R<Track> {
// another codec's file is refused before it is read: every open of a 4K HEVC interview on Linux read its whole
// gigabyte here to learn it wasn't H.264, then went to the backend that reads it (two minutes on a cloud disk)
if let Some(is) = video_fourcc(path).filter(|f| want.len() >= 3 && f[..3] != want.as_bytes()[..3]) {
return Err(format!("{}: {}, not {want} (this backend reads {want} mezzanines)", path.display(), String::from_utf8_lossy(&is)));
}
let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?;
let mp4 = re_mp4::Mp4::read_bytes(&bytes).map_err(|e| format!("{}: {e}", path.display()))?;
let (_, t) = mp4
+37
View File
@@ -0,0 +1,37 @@
//! A file of another codec is refused from its moov box alone, and a rotation is read from it, without the file's
//! pictures being read (a 1 GB HEVC interview was read whole on every open to learn it wasn't H.264).
use std::path::{Path, PathBuf};
use std::process::Command;
fn made(dir: &Path, name: &str, args: &[&str]) -> Option<PathBuf> {
let p = dir.join(name);
Command::new("ffmpeg").args(["-v", "error", "-y"]).args(args).arg(&p).status().is_ok_and(|s| s.success()).then_some(p)
}
#[test]
fn another_codec_is_refused_by_its_tables_and_a_rotation_read_from_them() {
let dir = std::env::temp_dir().join(format!("raea-soft-refuse-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let src = ["-f", "lavfi", "-i", "testsrc2=s=128x72:r=30:d=1", "-pix_fmt", "yuv420p"];
let Some(other) = made(&dir, "other.mp4", &[&src[..], &["-c:v", "mpeg4"]].concat()) else { return eprintln!("skipped: no ffmpeg here") };
let e = raea_io_soft::demux(&other, "avc1").err().expect("an MPEG-4 file isn't H.264");
assert!(e.contains("mp4v, not avc1"), "{e}");
// cut short after its moov, the refusal is the same: nothing past the tables was needed
if let Some(front) = made(&dir, "front.mp4", &[&src[..], &["-c:v", "mpeg4", "-movflags", "+faststart"]].concat()) {
let bytes = std::fs::read(&front).unwrap();
let mdat = bytes.windows(4).position(|w| w == b"mdat").unwrap();
std::fs::write(&front, &bytes[..mdat + 4]).unwrap();
assert!(raea_io_soft::demux(&front, "avc1").err().unwrap().contains("mp4v, not avc1"));
assert_eq!(raea_io_soft::rotation_of(&front), 0);
}
// an H.264 file is read as before, and one turned on its side says so
if let Some(h264) = made(&dir, "h264.mp4", &[&src[..], &["-c:v", "libx264"]].concat()) {
assert!(raea_io_soft::demux(&h264, "avc1").is_ok());
assert_eq!(raea_io_soft::rotation_of(&h264), 0);
let h = h264.to_string_lossy().to_string();
if let Some(turned) = made(&dir, "turned.mp4", &["-display_rotation:v:0", "90", "-i", &h, "-c", "copy"]) {
assert_eq!(raea_io_soft::rotation_of(&turned).rem_euclid(180), 90);
}
}
let _ = std::fs::remove_dir_all(&dir);
}
+14 -1
View File
@@ -11,11 +11,24 @@ import { check, found } from "raea/check"
import type { Job } from "../facts.ts"
import { adOf, bookkeeping, sizeOf } from "./naming.ts"
/** The size a made file is in, as a name says one (9x16): its name's, else a video's own shape (1080x1920 is 9x16).
* A picture with no size in its name is a still of the work, a sheet or a strip, not a size of an ad. Told by pixels
* alone, an ad named _9x16 beside its own 1080x1920 renders and one 540x960 still read as three sizes, and an agent
* with one size made couldn't stop for its sign-off (12-01). */
export function shapeOf(f: { path: string; kind?: string; width?: number; height?: number }): string | null {
const named = sizeOf(f.path)
if (named) return named
if (f.kind !== "video" || !f.width || !f.height) return null
const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a)
const g = gcd(f.width, f.height)
return `${f.width / g}x${f.height / g}`
}
export default check<Job>({ id: "sizes", text: "one size first: no other size before the first is approved", over: "job", at: ["render", "publish", "done"] }, async (j) => {
const made = j.files.filter((f) => f.made && !bookkeeping(f.path) && (f.kind === "video" || f.kind === "image"))
const sizes = new Map<string, string[]>()
for (const f of made) {
const s = sizeOf(f.path) ?? (f.width && f.height ? `${f.width}x${f.height}` : null)
const s = shapeOf(f)
if (s) sizes.set(s, [...(sizes.get(s) ?? []), f.path])
}
if (sizes.size < 2) return
+11
View File
@@ -4,6 +4,7 @@ import { beforeAll, expect, test } from "bun:test"
import { goalCheck } from "../src/run.ts"
import { CLIP, project, put } from "./project.ts"
import { readFileSync } from "node:fs"
import { shapeOf } from "../src/packs/sizes.ts"
beforeAll(() => {
process.env.RAEA_REAL = "/usr/bin/false"
@@ -31,3 +32,13 @@ test("every size of the newest version passes; a person's answer can say fewer",
expect((await all(answered, sizes)).filter((x) => x.check === "sizes-all")).toEqual([])
expect((await all(answered, ["out/cuts/dutch_vetbill_hooka_v2_9x16.mp4"])).filter((x) => x.check === "sizes-all")).toEqual([])
})
test("one size is one size however it's told: a name's 9x16, a 1080x1920 video, never a still", () => {
expect(shapeOf({ path: "out/cuts/dutch_vetbill_hooka_v1_9x16.mp4", kind: "video", width: 1080, height: 1920 })).toBe("9x16")
expect(shapeOf({ path: "out/cuts/hooka.mp4", kind: "video", width: 1080, height: 1920 })).toBe("9x16")
expect(shapeOf({ path: "out/cuts/hooka_small.mp4", kind: "video", width: 540, height: 960 })).toBe("9x16")
expect(shapeOf({ path: "out/cuts/hooka.mp4", kind: "video", width: 1080, height: 1350 })).toBe("4x5")
expect(shapeOf({ path: "out/stills/a1_38.jpg", kind: "image", width: 540, height: 960 })).toBe(null)
expect(shapeOf({ path: "out/stills/strip.png", kind: "image", width: 1080, height: 480 })).toBe(null)
expect(shapeOf({ path: "out/stills/dutch_vetbill_end_v1_1x1.png", kind: "image", width: 1080, height: 1080 })).toBe("1x1")
})