diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b93a34977..f80377f61 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -482,6 +482,14 @@ jobs: run: | cargo test --locked -p coreav-cli --features native-h264 --test transcode ffmpeg_libx264_crop_matches_the_oracle -- --exact --nocapture --color never 2>&1 | tee "$RUNNER_TEMP/literal-crop-macos.log" grep -F 'test ffmpeg_libx264_crop_matches_the_oracle ... ok' "$RUNNER_TEMP/literal-crop-macos.log" + - name: Default MP4 video encoder options on macOS + if: env.COREAV_FFMPEG != '' && env.COREAV_FFPROBE != '' + env: + COREAV_REQUIRE_FFMPEG: '1' + COREAV_X264: ${{ runner.temp }}/coreav-test-oracle/x264 + run: | + cargo test --locked -p coreav-cli --features native-h264 --test transcode ffmpeg_default_video_encoder_applies_options -- --exact --nocapture --color never 2>&1 | tee "$RUNNER_TEMP/default-video-options-macos.log" + grep -F 'test ffmpeg_default_video_encoder_applies_options ... ok' "$RUNNER_TEMP/default-video-options-macos.log" - name: Ignored H264 video quality on macOS if: env.COREAV_FFMPEG != '' && env.COREAV_FFPROBE != '' env: diff --git a/crates/coreav-cli/src/ffmpeg/args.rs b/crates/coreav-cli/src/ffmpeg/args.rs index a9f8a905d..a4bb78925 100644 --- a/crates/coreav-cli/src/ffmpeg/args.rs +++ b/crates/coreav-cli/src/ffmpeg/args.rs @@ -1221,6 +1221,13 @@ pub fn parse(args: &[String]) -> Result { } if let Some(option) = encoders.first() && !command.codecs.iter().any(|(_, c)| *c == Codec::Libx264) + && !(command.muxer == Muxer::Mp4 + && command.encoding.bitrate.is_none() + && command + .encoding + .filter + .as_deref() + .is_none_or(|filter| filter.starts_with("crop="))) && !command.muxer.frames() && command.muxer != Muxer::Matroska { @@ -1791,7 +1798,7 @@ mod tests { }; let named = |name: &str, value: Option<&str>| (name.to_owned(), value.map(str::to_owned)); assert_eq!( - option("-i in.mp4 -vf scale=2:2 out.mp4"), + option("-i in.mp4 -vf scale=2:2 out.m4a"), named("-vf", None) ); assert_eq!( @@ -1799,7 +1806,7 @@ mod tests { named("-c:v", Some("libx265")) ); assert_eq!( - option("-i in.mp4 -c copy -crf 23 out.mp4"), + option("-i in.mp4 -c copy -crf 23 out.m4a"), named("-crf", None) ); assert_eq!( @@ -1831,7 +1838,7 @@ mod tests { named("-copyts", None) ); assert_eq!( - option("-i in.mp4 -filter:v x out.mp4"), + option("-i in.mp4 -filter:v x out.m4a"), named("-filter:v", None) ); assert_eq!( @@ -1928,6 +1935,27 @@ mod tests { ); } + #[test] + fn mp4_video_options_reach_effective_codec_selection() { + for line in [ + "-i in.mp4 -vf crop=2:2:0:0 out.mp4", + "-i in.mp4 -c copy -crf 23 out.mp4", + "-i in.mp4 -c:a copy -s 32x24 out.mp4", + ] { + assert!(parse(&args(line)).is_ok(), "{line}"); + } + for line in [ + "-i in.mp4 -vf scale=32:24 out.mp4", + "-i in.mp4 -filter:v scale=32:24 out.mp4", + "-i in.mp4 -b:v 100k out.mp4", + ] { + assert!( + matches!(parse(&args(line)), Err(Refusal::NotNative(_))), + "{line}" + ); + } + } + #[test] fn libopus_and_its_bit_rate() { let c = parse(&args( diff --git a/crates/coreav-cli/src/ffmpeg/encode.rs b/crates/coreav-cli/src/ffmpeg/encode.rs index 383c9c0f5..b2e05ff24 100644 --- a/crates/coreav-cli/src/ffmpeg/encode.rs +++ b/crates/coreav-cli/src/ffmpeg/encode.rs @@ -274,7 +274,18 @@ pub fn request( .all(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())) }) .and_then(|(w, h)| transcode::scale(&format!("{w}:{h}")).ok()); - Some(parsed.ok_or_else(|| refused("-s", s, "the size must be WxH in pixels"))?) + let parsed = + parsed.ok_or_else(|| refused("-s", s, "the size must be WxH in pixels"))?; + if command.muxer == super::args::Muxer::Mp4 + && !command + .codecs + .iter() + .any(|(_, codec)| *codec == Codec::Libx264) + { + None + } else { + Some(parsed) + } } (None, None) => None, }; diff --git a/crates/coreav-cli/src/ffmpeg/mod.rs b/crates/coreav-cli/src/ffmpeg/mod.rs index 36341ceef..1e3a74c45 100644 --- a/crates/coreav-cli/src/ffmpeg/mod.rs +++ b/crates/coreav-cli/src/ffmpeg/mod.rs @@ -1054,6 +1054,27 @@ fn transcode_job( "encoding a stream other than video with libx264", )); } + if command.muxer == Muxer::Mp4 + && !command + .codecs + .iter() + .any(|(_, codec)| *codec == Codec::Libx264) + && let Some(size) = command.encoding.size.as_deref() + && !output.iter().any(|(index, stream)| { + index == video + && size.split_once('x').is_some_and(|(width, height)| { + width.parse::().ok() == Some(int(stream, "width")) + && height.parse::().ok() == Some(int(stream, "height")) + }) + }) + { + return Err(Refusal::NotNative(NotNative::option( + "-s", + Some(size), + "'-s' with the default MP4 video encoder requires unchanged source dimensions" + .to_owned(), + ))); + } if command.muxer != Muxer::Mp4 && command.muxer.matroska().is_none() { return Err(Refusal::not_native( "an encoded output other than one MP4 file", diff --git a/crates/coreav-cli/tests/fallback.rs b/crates/coreav-cli/tests/fallback.rs index 9409edd1f..9c6edfe81 100644 --- a/crates/coreav-cli/tests/fallback.rs +++ b/crates/coreav-cli/tests/fallback.rs @@ -1000,7 +1000,7 @@ fn a_relative_coreav_fallback_is_the_file_in_the_working_directory() { let decoy = tmp("relative-decoy"); symlink("/bin/false", decoy.join("ffmpeg")).unwrap(); let out = Command::new(bin()) - .args(["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"]) + .args(["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"]) .current_dir(stand_in()) .env_clear() .env("PATH", &decoy) @@ -1018,7 +1018,7 @@ fn an_empty_path_element_is_the_working_directory_after_coreav() { let links = tmp("empty-path-element"); symlink(bin(), links.join("ffmpeg")).unwrap(); let out = Command::new(bin()) - .args(["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"]) + .args(["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"]) .current_dir(stand_in()) .env_clear() .env("PATH", format!("{}:", links.display())) @@ -1040,7 +1040,7 @@ fn a_program_on_path_this_process_cannot_run_is_skipped() { return; } let r = coreav( - &["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"], + &["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"], &[&bad, stand_in()], &[], ); @@ -1057,7 +1057,7 @@ fn a_program_that_cannot_start_is_logged_as_not_run() { std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap(); let log = dir.join("fallback.log"); let r = coreav( - &["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"], + &["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"], &[], &[ ("COREAV_FALLBACK", dir.to_str().unwrap()), @@ -1083,13 +1083,13 @@ fn without_a_program_the_command_is_refused_and_says_why() { let links = tmp("missing"); symlink(bin(), links.join("ffmpeg")).unwrap(); let r = coreav( - &["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"], + &["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"], &[&links], &[], ); assert_eq!(r.code, 4); assert!(r.stdout.is_empty()); - assert!(r.stderr.contains("'-vf' is not supported"), "{}", r.stderr); + assert!(r.stderr.contains("'-af' is not supported"), "{}", r.stderr); assert!( r.stderr.contains("no other ffmpeg on PATH to run instead"), "{}", @@ -1213,7 +1213,7 @@ fn the_guard_is_set_by_any_value() { let dir = stand_in().to_str().unwrap(); for value in ["", "0", "1"] { let r = coreav( - &["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"], + &["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"], &[], &[("COREAV_FALLBACK", dir), ("COREAV_FALLBACK_ACTIVE", value)], ); @@ -1233,7 +1233,7 @@ fn a_fallback_that_leads_back_to_coreav_is_stopped_by_the_guard() { symlink(bin(), links.join("ffmpeg")).unwrap(); let log = links.join("fallback.log"); let r = coreav( - &["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"], + &["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"], &[], &[ ("COREAV_FALLBACK", links.to_str().unwrap()), @@ -1389,7 +1389,7 @@ fn the_log_gets_one_redacted_line_per_command_that_is_not_native() { fn a_log_that_cannot_be_written_costs_one_line_and_never_stops_the_command() { let dir = stand_in().to_str().unwrap(); let r = coreav( - &["ffmpeg", "-i", "in.mp4", "-vf", "x", "out.mp4"], + &["ffmpeg", "-i", "in.mp4", "-af", "x", "out.mp4"], &[], &[("COREAV_FALLBACK", dir), ("COREAV_FALLBACK_LOG", dir)], ); diff --git a/crates/coreav-cli/tests/fallback_windows.rs b/crates/coreav-cli/tests/fallback_windows.rs index c44de7092..b7b16c60b 100644 --- a/crates/coreav-cli/tests/fallback_windows.rs +++ b/crates/coreav-cli/tests/fallback_windows.rs @@ -37,7 +37,7 @@ fn stand_in() -> &'static Path { fn args(tool: &str) -> &'static [&'static str] { match tool { - "ffmpeg" => &["-i", "in.mp4", "-vf", "x", "out.mp4"], + "ffmpeg" => &["-i", "in.mp4", "-af", "x", "out.mp4"], _ => &["-show_data", "in.mp4"], } } diff --git a/crates/coreav-cli/tests/ffmpeg.rs b/crates/coreav-cli/tests/ffmpeg.rs index 3ad41d3e7..d389da464 100644 --- a/crates/coreav-cli/tests/ffmpeg.rs +++ b/crates/coreav-cli/tests/ffmpeg.rs @@ -929,10 +929,7 @@ fn commands_outside_the_vocabulary_are_refused_by_name() { let i = input.as_str(); for (args, named) in [ (vec!["-i", i, "-c:v", "libx265", o], "libx265"), - ( - vec!["-i", i, "-vf", "scale=320:-2", "-c:a", "copy", o], - "-vf", - ), + (vec!["-i", i, "-af", "anull", "-c:a", "copy", o], "-af"), (vec!["-f", "mpegts", "-i", i, "-c", "copy", o], "mpegts"), (vec!["-i", i, "-c", "copy", "-f", "hls", o], "hls"), ( diff --git a/crates/coreav-cli/tests/transcode.rs b/crates/coreav-cli/tests/transcode.rs index 39844636a..2f49d6244 100644 --- a/crates/coreav-cli/tests/transcode.rs +++ b/crates/coreav-cli/tests/transcode.rs @@ -1432,6 +1432,220 @@ fn hostile_encoders_end_in_an_error_and_leave_nothing() { nothing_left(&out_dir); } +#[test] +#[cfg(feature = "native-h264")] +fn ffmpeg_default_video_encoder_applies_options() { + let (ffmpeg, ffprobe) = ffmpeg().unwrap(); + let Some(x264) = x264() else { + assert!(!required(), "no pinned x264 (set COREAV_X264)"); + eprintln!("skipping: no pinned x264 (set COREAV_X264)"); + return; + }; + let dir = dir("ffmpeg-default-video-options"); + let successful = |command: &mut Command| { + let result = bounded::output(command, None, std::time::Duration::from_secs(30)).unwrap(); + assert!(result.status.success(), "{result:?}"); + result.stdout + }; + let tools = Tools { + ffmpeg, + ffprobe, + x264, + fixtures: dir.clone(), + }; + let source = dir.join("source.mp4"); + let generated = Command::new(&tools.ffmpeg) + .args([ + "-nostdin", + "-v", + "error", + "-f", + "lavfi", + "-i", + "testsrc2=size=64x48:rate=10:duration=0.8", + "-an", + "-c:v", + "libx264", + "-threads:v", + "1", + "-bf", + "0", + ]) + .arg(&source) + .output() + .unwrap(); + assert!( + generated.status.success(), + "{}", + String::from_utf8_lossy(&generated.stderr) + ); + for (case, options, width, height) in [ + ("size", vec!["-s", "64x48"], 64, 48), + ( + "faststart", + vec!["-movflags", "faststart", "-pix_fmt", "yuv420p"], + 64, + 48, + ), + ("crop", vec!["-vf", "crop=32:24:0:0"], 32, 24), + ("crop-offset", vec!["-filter:v", "crop=32:24:16:8"], 32, 24), + ("quality", vec!["-crf", "19", "-preset", "fast"], 64, 48), + ("passthrough", vec!["-fps_mode", "passthrough"], 64, 48), + ] { + let implicit = dir.join(format!("{case}-implicit.mp4")); + let explicit = dir.join(format!("{case}-explicit.mp4")); + for (path, codec) in [(&implicit, false), (&explicit, true)] { + let mut command = Command::new(bin()); + command.arg("ffmpeg").arg("-i").arg(&source); + if codec { + command.args(["-c:v", "libx264"]); + } + let result = command + .args(if codec && case == "size" { + &[] + } else { + options.as_slice() + }) + .args(["-threads", "1"]) + .arg(path) + .env("COREAV_X264", &tools.x264) + .env("COREAV_FFMPEG", NO_FALLBACK) + .env("COREAV_FALLBACK", NO_FALLBACK) + .env("COREAV_STRICT_SAFE", "0") + .output() + .unwrap(); + assert!( + result.status.success(), + "{case}, explicit={codec}: {}", + String::from_utf8_lossy(&result.stderr) + ); + } + assert_eq!( + std::fs::read(&implicit).unwrap(), + std::fs::read(&explicit).unwrap(), + "{case}" + ); + let probe = probe_json(&tools, &["-show_streams", "-count_frames"], &implicit); + assert_eq!(probe["streams"][0]["codec_name"], "h264"); + assert_eq!(probe["streams"][0]["width"], width); + assert_eq!(probe["streams"][0]["height"], height); + assert_eq!(probe["streams"][0]["nb_read_frames"], "8"); + decodes_cleanly(&tools, &implicit); + let reference = dir.join(format!("{case}-reference.mp4")); + successful( + Command::new(&tools.ffmpeg) + .args(["-nostdin", "-v", "error", "-i"]) + .arg(&source) + .args(&options) + .args(["-threads", "1"]) + .arg(&reference), + ); + let decoded = |path: &Path| { + successful( + Command::new(&tools.ffmpeg) + .args(["-v", "error", "-i"]) + .arg(path) + .args(["-map", "0:v:0", "-f", "framemd5", "-"]), + ) + }; + assert_eq!(decoded(&implicit), decoded(&reference), "{case}"); + let fields = [ + "-count_frames", + "-show_entries", + "stream=codec_name,width,height,sample_aspect_ratio,nb_read_frames", + ]; + assert_eq!( + probe_json(&tools, &fields, &implicit), + probe_json(&tools, &fields, &reference), + "{case}" + ); + } + for options in [ + vec!["-vf", "scale=32:24"], + vec!["-filter:v", "scale=32:24"], + vec!["-s", "32x24"], + vec!["-b:v", "100k"], + vec![ + "-vf", + "scale=32:24", + "-movflags", + "faststart", + "-pix_fmt", + "yuv420p", + ], + vec!["-c", "copy", "-vf", "scale=32:24"], + vec!["-c:v", "copy", "-crf", "19"], + vec!["-c:v", "libx264", "-c", "copy", "-s", "32x24"], + vec!["-c:v:1", "libx264", "-c:v:0", "copy", "-preset", "fast"], + vec!["-vf", "fps=5"], + vec!["-vf", "hflip"], + vec!["-pix_fmt", "yuv444p"], + ] { + let output = dir.join("refused.mp4"); + std::fs::write(&output, b"existing output").unwrap(); + let result = Command::new(bin()) + .arg("ffmpeg") + .arg("-y") + .arg("-i") + .arg(&source) + .args(&options) + .arg(&output) + .env("COREAV_X264", &tools.x264) + .env("COREAV_FFMPEG", NO_FALLBACK) + .env("COREAV_FALLBACK", NO_FALLBACK) + .env("COREAV_STRICT_SAFE", "0") + .output() + .unwrap(); + assert_eq!( + result.status.code(), + Some(4), + "{options:?}: {}", + String::from_utf8_lossy(&result.stderr) + ); + assert_eq!(std::fs::read(&output).unwrap(), b"existing output"); + } + for options in [ + vec!["-vf", "scale=32:24"], + vec!["-filter:v", "scale=32:24"], + vec!["-s", "32x24"], + vec!["-b:v", "100k"], + ] { + let expected = dir.join("fallback-reference.mp4"); + successful( + Command::new(&tools.ffmpeg) + .args(["-nostdin", "-v", "error", "-y", "-i"]) + .arg(&source) + .args(&options) + .args(["-threads", "1"]) + .arg(&expected), + ); + let output = dir.join("fallback.mp4"); + let log = dir.join("fallback.log"); + std::fs::write(&output, b"existing output").unwrap(); + let result = Command::new(bin()) + .args(["ffmpeg", "-y", "-i"]) + .arg(&source) + .args(&options) + .args(["-threads", "1"]) + .arg(&output) + .env("COREAV_X264", &tools.x264) + .env("COREAV_FFMPEG", NO_FALLBACK) + .env("COREAV_FALLBACK", &tools.ffmpeg) + .env("COREAV_FALLBACK_LOG", &log) + .env("COREAV_STRICT_SAFE", "0") + .output() + .unwrap(); + assert!(result.status.success(), "{options:?}: {result:?}"); + assert!(!std::fs::read(&log).unwrap().is_empty()); + assert_eq!( + std::fs::read(output).unwrap(), + std::fs::read(expected).unwrap(), + "{options:?}" + ); + } + nothing_left(&dir); +} + /// `coreav ffmpeg` with `-c:v libx264` (`docs/design/transcode.md`, /// "Through `coreav ffmpeg`"): the video's encode with copied audio, or /// default AAC when this build decodes the source; unsupported commands diff --git a/docs/agents/ffmpeg-cli.md b/docs/agents/ffmpeg-cli.md index 9e8c4d1f6..fa2ca2b7d 100644 --- a/docs/agents/ffmpeg-cli.md +++ b/docs/agents/ffmpeg-cli.md @@ -4,6 +4,8 @@ CLI and ffprobe behavior, the FFmpeg pin, and fallback names. | Rule | Enforced by | |---|---| +| Newly admitted default-video options match the pinned oracle's decoded frames and aspect, not merely the existing explicit encoder path | The MP4 parser keeps implicit scale and bitrate on fallback; `transcode_job` admits implicit output size only when source dimensions match. `ffmpeg_default_video_encoder_applies_options` compares complete frame hashes and geometry/aspect with the oracle and checks unchanged destinations plus byte-exact fallback for excluded paths. Runtime source dimensions and external output cannot be established by argument types | +| Generated default-video option coverage treats x264 as optional only outside required verification, without requiring unrelated fixtures | `ffmpeg_default_video_encoder_applies_options` checks the shared `required()` policy when x264 is absent. Its explicit macOS CI step sets `COREAV_REQUIRE_FFMPEG=1` and requires the exact test-success witness. Filesystem availability is a runtime fact, so a type cannot decide whether the selected executable exists | | Shell oracle consumers retain the complete pair and reject qualification or response failures before creating output, including under macOS's Bash 3.2 | The shared owner's two-line CLI and plain assignment own qualification; guarded array expansions and read loops avoid newer Bash builtins and nounset behavior. Shell regressions exercise malformed responses, retained paths, explicit overrides and audio arguments without `mapfile`; they default to `/bin/bash` and accept `COREAV_TEST_BASH` for legacy-interpreter checks. A path type cannot propagate shell exit status or establish an external interpreter's behavior | | Invocation evidence is exact-site, source-bound and dependency-bound, including import-candidate absence and same-file fold severity | The classifier's single binding owner normalizes checkout roots and binds reviewed inward links to their literal text and tracked regular targets. Its schema validator and freshness controls reject changed targets, retargeting, outward or indirect links, stale files, import shadows, missing relay dependencies, duplicate or dead entries and weaker fold origins. Observer-copy status prevents a development source from inheriting the candidate's registered-source exclusion. The closure, source-span and proposal regressions exercise these external-file facts; a schema type alone cannot establish current checkout bytes | | Invocation discovery normalizes its repository root and never opens missing index entries or nonregular gitlinks as source files | `inventory` owns root resolution and regular-file selection; the classifier retains missing and nonregular entries as unresolved closure rows. Symlink-root, deleted-file and gitlink regressions cover the public inventory boundary. A path type cannot establish the current filesystem kind | diff --git a/docs/design/transcode.md b/docs/design/transcode.md index 957fef914..463f23ed4 100644 --- a/docs/design/transcode.md +++ b/docs/design/transcode.md @@ -1138,8 +1138,18 @@ Explicit codec options still win in their original order. For example, selected stream. Defaults do not add an encoder, widen audio-source support or enable audio-only encoding. An M4A output with selected video is still refused, not silently reduced to audio. Segment output still -requires explicit codecs. Encoder options other than the ignored `-q:v` -still require their explicit encoder name. See [FFmpeg's stream handling documentation](https://ffmpeg.org/ffmpeg.html#Stream-handling). +requires explicit codecs. MP4 crop, CRF, preset, pixel-format and passthrough +options also apply to default libx264 selection. Output `-s` is admitted +only when it repeats the selected source dimensions and is then a no-op, +preserving the source aspect ratio. Implicit `-vf scale`, +`-filter:v scale`, resizing `-s` and `-b:v` still refuse and reach fallback: +the existing scaler and bitrate encoder path do not produce frame-identical +output to the pinned FFmpeg. Explicit libx264 commands keep their previous +behavior; scaling and bitrate parity are not extended here. +An ignored `-q:v` needs no explicit encoder name when the effective video +encoder is libx264. +Explicit copy selection still refuses encoder options, +and audio-only encoding still refuses video encoder options. See [FFmpeg's stream handling documentation](https://ffmpeg.org/ffmpeg.html#Stream-handling). An Opus source can use default AAC encoding. Its current verification uses the separately registered [composed Opus-to-AAC check](composed-opus-aac-v2.md): diff --git a/test-support/oracle-dependent.json b/test-support/oracle-dependent.json index 8271bb990..370ffcd35 100644 --- a/test-support/oracle-dependent.json +++ b/test-support/oracle-dependent.json @@ -184,6 +184,7 @@ "ffmpeg_audio_only_refuses_unpreserved_source_properties", "ffmpeg_avi_mp3_keeps_the_previous_complete_file_bytes", "ffmpeg_default_codecs_match_the_oracle", + "ffmpeg_default_video_encoder_applies_options", "ffmpeg_libopus_lines_encode_the_audio", "ffmpeg_libx264_commands_run_the_pipeline", "ffmpeg_libx264_crop_matches_the_oracle",