Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
34 changes: 31 additions & 3 deletions crates/coreav-cli/src/ffmpeg/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,13 @@ pub fn parse(args: &[String]) -> Result<Command, Refusal> {
}
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
{
Expand Down Expand Up @@ -1791,15 +1798,15 @@ 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!(
option("-i in.mp4 -c:v libx265 out.mp4"),
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!(
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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(
Expand Down
13 changes: 12 additions & 1 deletion crates/coreav-cli/src/ffmpeg/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
capy-ai[bot] marked this conversation as resolved.
Resolved
{
None
} else {
Some(parsed)
}
}
(None, None) => None,
};
Expand Down
21 changes: 21 additions & 0 deletions crates/coreav-cli/src/ffmpeg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i64>().ok() == Some(int(stream, "width"))
&& height.parse::<i64>().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",
Expand Down
18 changes: 9 additions & 9 deletions crates/coreav-cli/tests/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()))
Expand All @@ -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()],
&[],
);
Expand All @@ -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()),
Expand All @@ -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"),
"{}",
Expand Down Expand Up @@ -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)],
);
Expand All @@ -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()),
Expand Down Expand Up @@ -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)],
);
Expand Down
2 changes: 1 addition & 1 deletion crates/coreav-cli/tests/fallback_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/coreav-cli/tests/ffmpeg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
(
Expand Down
Loading
Loading