The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.
Rust ffmpegfree-video-editormodel-context-protocolnon-linear-editoroffline-firstopen-source-video-editorself-hostedvideo-editingvideo-editing-automationvideo-editing-softwarevideo-editing-toolsvideo-editorvoice-cloningyoutube-automationyoutube-shorts
fix(export): a phone exports with its own hardware encoders, and the sheet offers only what the device can encode
Jareer8d3bf19834c1
diff --git a/src/crates/concat-media/src/encode.rs b/src/crates/concat-media/src/encode.rsindex 2c01bc2f..5ee72c7b 100644--- a/src/crates/concat-media/src/encode.rs+++ b/src/crates/concat-media/src/encode.rs@@ -4,6 +4,7 @@//! Writing RGBA frames back out to a file.use std::path::{Path, PathBuf};+use std::sync::OnceLock;use concat_core::frame::{Depth, Frame, Signal};use concat_core::time::FrameRate;@@ -80,30 +81,96 @@ impl VideoCodec {}}- /// The FFmpeg encoders that make this codec, best first. With- /// `hardware`, the platform's own encoder leads where there is one- /// worth leading with: VideoToolbox for HEVC on macOS, which is many- /// times faster than x265 and, at these rates, as good to look at.- /// H.264 stays with x264 everywhere: the hardware H.264 encoders- /// spend noticeably more bits for the same picture, and H.264 is the- /// choice made for compatibility, not speed.+ /// The FFmpeg encoders that make this codec, best first; an export+ /// takes the first that is linked and opens.+ ///+ /// A phone has its platform's hardware and nothing else: its FFmpeg is+ /// built LGPL with MediaCodec or VideoToolbox and none of the GPL+ /// encoders (scripts/ffmpeg-mobile.sh), and its cores are no match for+ /// its chip anyway.+ ///+ /// On a desktop the software encoders lead, and the platform's own+ /// comes after them for an FFmpeg built without them. With `hardware`,+ /// VideoToolbox leads for HEVC on macOS, which is many times faster+ /// than x265 and, at these rates, as good to look at. H.264 stays with+ /// x264 wherever it is linked: the hardware H.264 encoders spend+ /// noticeably more bits for the same picture, and H.264 is the choice+ /// made for compatibility, not speed.pub fn encoders(self, hardware: bool) -> &'static [&'static str] {+ let macos = cfg!(target_os = "macos");+ let windows = cfg!(target_os = "windows");+ if cfg!(target_os = "android") {+ return match self {+ VideoCodec::H264 => &["h264_mediacodec"],+ VideoCodec::Hevc => &["hevc_mediacodec"],+ VideoCodec::Av1 => &["av1_mediacodec"],+ };+ }+ if cfg!(target_os = "ios") {+ return match self {+ VideoCodec::H264 => &["h264_videotoolbox"],+ VideoCodec::Hevc => &["hevc_videotoolbox"],+ VideoCodec::Av1 => &[],+ };+ }match self {+ VideoCodec::H264 if macos => &["libx264", "h264_videotoolbox"],+ VideoCodec::H264 if windows => &["libx264", "h264_mf"],VideoCodec::H264 => &["libx264"],- VideoCodec::Hevc if hardware && cfg!(target_os = "macos") => {- &["hevc_videotoolbox", "libx265"]- }+ VideoCodec::Hevc if hardware && macos => &["hevc_videotoolbox", "libx265"],+ VideoCodec::Hevc if macos => &["libx265", "hevc_videotoolbox"],+ VideoCodec::Hevc if windows => &["libx265", "hevc_mf"],VideoCodec::Hevc => &["libx265"],VideoCodec::Av1 => &["libsvtav1", "libaom-av1"],}}+ /// The encoders for it the linked FFmpeg carries, best first.+ fn linked(self, hardware: bool, ten_bit: bool) -> impl Iterator<Item = &'static str> {+ ffi::init();+ self.encoders(hardware)+ .iter()+ .copied()+ .filter(move |name| !ten_bit || takes_ten_bits(name))+ .filter(|name| encoder::find_by_name(name).is_some())+ }+/// Whether the linked FFmpeg carries an encoder for it.pub fn available(self) -> bool {- ffi::init();- self.encoders(true)+ self.linked(true, false).next().is_some()+ }++ /// Whether an export in it would open here: an encoder for it is+ /// linked and, where that is the platform's hardware, the chip takes+ /// it. Linked is not enough on a phone: its FFmpeg carries MediaCodec's+ /// encoder for every codec, and few phones have an AV1 one. A hardware+ /// encoder is opened once, at 720p, the first time it is asked about,+ /// and the answer kept for the process.+ pub fn encodable(self) -> bool {+ static KNOWN: [OnceLock<bool>; 3] = [const { OnceLock::new() }; 3];+ let slot = VideoCodec::ALL.iter()- .any(|name| encoder::find_by_name(name).is_some())+ .position(|codec| *codec == self)+ .expect("every codec is in ALL");+ *KNOWN[slot].get_or_init(|| {+ self.linked(true, false)+ .any(|name| !Family::of(name).hardware() || opens(self, name))+ })+ }++ /// Whether it can be written at ten bits here: some linked encoder for+ /// it takes ten-bit pictures. Not on a phone, whose encoders FFmpeg+ /// feeds eight bits only.+ pub fn ten_bit_available(self) -> bool {+ self.linked(true, true).next().is_some()+ }++ /// Whether the encoder an export would pick first is the platform's+ /// hardware rather than the CPU.+ pub fn hardware_encoded(self, hardware: bool, ten_bit: bool) -> bool {+ self.linked(hardware, ten_bit)+ .next()+ .is_some_and(|name| Family::of(name).hardware())}/// Bytes per second relative to H.264 at the same quality, for a size@@ -232,6 +299,314 @@ fn videotoolbox_quality(crf: u8) -> u8 {(100.0 - f32::from(crf) * 2.2).round().clamp(1.0, 100.0) as u8}+/// Which kind of encoder an FFmpeg encoder is: what it takes its pictures+/// in, and what it is steered by.+#[derive(Clone, Copy, PartialEq, Eq, Debug)]+enum Family {+ /// x264, x265, SVT-AV1, libaom: planar 4:2:0, steered by a CRF.+ Software,+ /// Apple's: semi-planar 4:2:0, steered by a quality.+ VideoToolbox,+ /// Android's: semi-planar 4:2:0 at eight bits, steered by a bitrate.+ MediaCodec,+ /// Windows': semi-planar 4:2:0 at eight bits, steered by a bitrate.+ MediaFoundation,+}++impl Family {+ fn of(encoder_name: &str) -> Self {+ if encoder_name.ends_with("_videotoolbox") {+ Family::VideoToolbox+ } else if encoder_name.ends_with("_mediacodec") {+ Family::MediaCodec+ } else if encoder_name.ends_with("_mf") {+ Family::MediaFoundation+ } else {+ Family::Software+ }+ }++ /// The pixel format the family's encoders take. Semi-planar for the+ /// hardware, which is the layout every one of them reads - MediaCodec+ /// also names planar, and a good share of phones refuse it.+ fn pixel_format(self, ten_bit: bool) -> Pixel {+ match (self, ten_bit) {+ (Family::Software, false) => Pixel::YUV420P,+ (Family::Software, true) => Pixel::YUV420P10LE,+ (_, false) => Pixel::NV12,+ (_, true) => Pixel::P010LE,+ }+ }++ /// Whether the family's encoders are told a bitrate rather than a+ /// quality, in VBR as in CBR.+ fn steered_by_bitrate(self) -> bool {+ matches!(self, Family::MediaCodec | Family::MediaFoundation)+ }++ /// Whether this is the platform's hardware rather than the CPU.+ fn hardware(self) -> bool {+ self != Family::Software+ }+}++/// Whether `encoder_name` opens for an eight-bit 720p30 file of `codec`:+/// the question a phone's chip answers only by being asked.+fn opens(codec: VideoCodec, encoder_name: &'static str) -> bool {+ let Some(found) = encoder::find_by_name(encoder_name) else {+ return false;+ };+ let options = EncodeOptions {+ codec,+ ..EncodeOptions::default()+ };+ let tags = (+ ffmpeg::color::Primaries::BT709,+ ffmpeg::color::TransferCharacteristic::BT709,+ ffmpeg::color::Space::BT709,+ );+ // Nothing is written: the path only names the file in an error.+ let result = open_video(+ encoder_name,+ found,+ Path::new("probe.mp4"),+ (1280, 720),+ ffmpeg::Rational::new(30, 1),+ &options,+ tags,+ None,+ true,+ );+ if let Err(error) = &result {+ log::info!("{encoder_name} does not open here: {error}");+ }+ result.is_ok()+}++/// Whether the encoder takes ten-bit pictures. FFmpeg's MediaCodec and+/// Media Foundation encoders take eight-bit input only, and VideoToolbox+/// makes ten-bit HEVC but not ten-bit H.264.+fn takes_ten_bits(encoder_name: &str) -> bool {+ match Family::of(encoder_name) {+ Family::Software => true,+ Family::VideoToolbox => encoder_name.starts_with("hevc_"),+ Family::MediaCodec | Family::MediaFoundation => false,+ }+}++/// The bitrate in kbps a CRF comes to, for an encoder that is steered by a+/// bitrate alone: the export sheet's rule for its size estimate - 16 Mb/s+/// for H.264 at 1080p30 and CRF 16, halving about every five steps -+/// scaled by the pixels, the rate and the codec's size factor.+fn implied_kbps(codec: VideoCodec, crf: u8, width: u32, height: u32, rate: ffmpeg::Rational) -> u32 {+ let fps = f64::from(rate.numerator()) / f64::from(rate.denominator().max(1));+ let pixels = f64::from(width) * f64::from(height) / (1920.0 * 1080.0);+ let mbps = 16.0+ * 2f64.powf((16.0 - f64::from(crf)) / 5.0)+ * pixels+ * (fps / 30.0)+ * f64::from(codec.size_factor());+ (mbps * 1000.0).round().clamp(500.0, 200_000.0) as u32+}++/// One encoder's context, set up and opened for the file: the frame's+/// size, rate and tags, and the settings `encoder_name` is steered by.+/// Returns the encoder and the pixel format it takes its pictures in.+fn open_video(+ encoder_name: &'static str,+ codec: ffmpeg::Codec,+ path: &Path,+ (width, height): (u32, u32),+ rate: ffmpeg::Rational,+ options: &EncodeOptions,+ tags: (+ ffmpeg::color::Primaries,+ ffmpeg::color::TransferCharacteristic,+ ffmpeg::color::Space,+ ),+ hdr: Option<Signal>,+ global_header: bool,+) -> Result<(encoder::video::Encoder, Pixel)> {+ let pq = hdr == Some(Signal::Pq);+ let family = Family::of(encoder_name);+ let pixel_format = family.pixel_format(options.ten_bit);+ let cbr = options.rate_mode == RateMode::Cbr && options.bitrate_kbps > 0;+ let mut video = ffmpeg::codec::Context::new_with_codec(codec)+ .encoder()+ .video()+ .map_err(|error| ffi::fail("video encoder", path, error))?;+ video.set_width(width);+ video.set_height(height);+ video.set_format(pixel_format);+ video.set_time_base(rate.invert());+ video.set_frame_rate(Some(rate));+ let (primaries, transfer, matrix) = tags;+ video.set_colorspace(matrix);+ // The range the frames will be converted to below, so the tag is+ // true; VideoToolbox reads it to pick its full- or video-range+ // pixel format, the software encoders write it into the stream.+ video.set_color_range(options.color_range.as_ffmpeg());+ // SAFETY: `video` owns a live AVCodecContext; primaries and+ // transfer have no setter in the bindings, and all three are plain+ // fields the encoder reads at open.+ unsafe {+ let context = video.as_mut_ptr();+ (*context).color_primaries = primaries.into();+ (*context).color_trc = transfer.into();+ if options.threads > 0 {+ (*context).thread_count = i32::from(options.threads);+ }+ }+ if global_header {+ video.set_flags(ffmpeg::codec::Flags::GLOBAL_HEADER);+ }+ if pq {+ // The display it was mastered on, where an encoder that writes+ // it into the stream reads it.+ // SAFETY: `video` owns a live AVCodecContext; the side data is+ // allocated by FFmpeg at the struct's size and filled here.+ unsafe {+ let context = video.as_mut_ptr();+ let entry = ffmpeg::sys::av_frame_side_data_new(+ &mut (*context).decoded_side_data,+ &mut (*context).nb_decoded_side_data,+ ffmpeg::sys::AVFrameSideDataType::MASTERING_DISPLAY_METADATA,+ std::mem::size_of::<MasteringDisplay>(),+ 0,+ );+ if !entry.is_null() {+ std::ptr::write(+ (*entry).data.cast::<MasteringDisplay>(),+ MasteringDisplay::p3_1000(),+ );+ }+ }+ }++ // The encoders steered by a bitrate alone take one whatever the mode:+ // the target in CBR, and in VBR the rate the CRF would come to.+ if family.steered_by_bitrate() {+ let kbps = if cbr {+ options.bitrate_kbps+ } else {+ implied_kbps(options.codec, options.crf, width, height, rate)+ };+ video.set_bit_rate(kbps as usize * 1000);+ // Two seconds between keyframes, where MediaCodec's own default+ // is one per second rounded from a GOP of twelve frames.+ let fps = (f64::from(rate.numerator()) / f64::from(rate.denominator().max(1))).round();+ video.set_gop((fps.max(1.0) * 2.0) as u32);+ }+ let crf = options.crf.to_string();+ let bitrate = format!("{}k", options.bitrate_kbps);+ // One second of VBV, not two. With a looser buffer x264 can stay+ // well under b:v on calm content and never emit the padding that+ // makes a CBR a CBR; `bufsize == bitrate` is where it starts+ // holding the target.+ let bufsize = format!("{}k", options.bitrate_kbps);+ let settings = match encoder_name {+ // libx264 / libx265 both take a bitrate, -minrate and -maxrate+ // for CBR; VBR is the CRF that already shipped. The bitrate is+ // `b`, the codec option's own name: `b:v` is the command line's+ // spelling with a stream specifier, which the library does not+ // know, so it was dropped - x264 then ran at its CRF under the+ // cap, and x265 refused strict-cbr without a bitrate.+ // Real CBR needs `nal-hrd=cbr` on x264, and `strict-cbr=1` on+ // libx265. Without it x264 caps at maxrate but does not pad the+ // output to b:v on content that does not need the bitrate, so an+ // "8000k CBR" export of a calm clip comes out at whatever the+ // content costs, not 8000k.+ "libx264" if cbr => ffmpeg::dict! {+ "preset" => options.preset.as_str(),+ "b" => bitrate.as_str(),+ "minrate" => bitrate.as_str(),+ "maxrate" => bitrate.as_str(),+ "bufsize" => bufsize.as_str(),+ "x264-params" => "nal-hrd=cbr:force-cfr=1",+ },+ "libx265" if cbr => ffmpeg::dict! {+ "preset" => options.preset.as_str(),+ "b" => bitrate.as_str(),+ "minrate" => bitrate.as_str(),+ "maxrate" => bitrate.as_str(),+ "bufsize" => bufsize.as_str(),+ "x265-params" => "strict-cbr=1",+ },+ "libx264" | "libx265" => ffmpeg::dict! {+ "preset" => options.preset.as_str(),+ "crf" => crf.as_str(),+ },+ "libsvtav1" => ffmpeg::dict! {+ "preset" => &svt_preset(&options.preset).to_string(),+ // AV1's CRF runs to 63 and reads a few steps coarser than+ // x264's; eight on is where the pictures match.+ "crf" => &options.crf.saturating_add(8).min(63).to_string(),+ },+ "libaom-av1" => ffmpeg::dict! {+ "crf" => &options.crf.saturating_add(8).min(63).to_string(),+ "cpu-used" => "6",+ },+ "hevc_videotoolbox" | "h264_videotoolbox" => ffmpeg::dict! {+ "q:v" => &videotoolbox_quality(options.crf).to_string(),+ "profile" => match (encoder_name, options.ten_bit) {+ ("h264_videotoolbox", _) => "high",+ (_, true) => "main10",+ (_, false) => "main",+ },+ // A machine without the hardware still gets a file.+ "allow_sw" => "1",+ },+ // A phone's chip. The NDK's MediaCodec, not the Java one: no+ // JavaVM is handed to FFmpeg, and the NDK needs none.+ _ if family == Family::MediaCodec => ffmpeg::dict! {+ "bitrate_mode" => if cbr { "cbr" } else { "vbr" },+ },+ // Windows' Media Foundation, whatever the machine has behind it.+ _ if family == Family::MediaFoundation => ffmpeg::dict! {+ "rate_control" => if cbr { "cbr" } else { "u_vbr" },+ },+ _ => ffmpeg::dict! {},+ };+ // HDR's own words to the software encoders, besides the tags they+ // read off the context: headers with every keyframe, and a PQ+ // file's mastering display.+ let mut settings = settings;+ if hdr.is_some() {+ match encoder_name {+ "libx265" => {+ let mut params = settings+ .get("x265-params")+ .map(str::to_owned)+ .unwrap_or_default();+ for param in [+ Some("repeat-headers=1".to_owned()),+ pq.then(|| format!("hdr10=1:master-display={X265_MASTER_DISPLAY}")),+ ]+ .into_iter()+ .flatten()+ {+ if !params.is_empty() {+ params.push(':');+ }+ params.push_str(¶m);+ }+ settings.set("x265-params", ¶ms);+ }+ "libsvtav1" if pq => {+ settings.set(+ "svtav1-params",+ format!("mastering-display={SVT_MASTER_DISPLAY}"),+ );+ }+ _ => {}+ }+ }+ let encoder = video+ .open_with(settings)+ .map_err(|error| ffi::fail("open encoder", path, error))?;+ Ok((encoder, pixel_format))+}+/// A four-character code as the muxer stores it.const fn fourcc(tag: [u8; 4]) -> u32 {u32::from_le_bytes(tag)@@ -522,26 +897,6 @@ impl Encoder {let rate = ffmpeg::Rational::new(fps.numerator() as i32, fps.denominator() as i32);let time_base = rate.invert();- // The first encoder the linked FFmpeg has for the codec.- let (encoder_name, codec) = options- .codec- .encoders(options.hardware)- .iter()- .find_map(|name| encoder::find_by_name(name).map(|codec| (*name, codec)))- .ok_or_else(|| Error::Missing {- what: "encoder",- name: options.codec.label().to_owned(),- })?;- let videotoolbox = encoder_name.ends_with("_videotoolbox");- // VideoToolbox takes its pictures planar-chroma; the software- // encoders take the planar 4:2:0 they all read.- let pixel_format = match (videotoolbox, options.ten_bit) {- (true, true) => Pixel::P010LE,- (true, false) => Pixel::NV12,- (false, true) => Pixel::YUV420P10LE,- (false, false) => Pixel::YUV420P,- };-let mut output =ffmpeg::format::output(path).map_err(|error| ffi::fail("create", path, error))?;let global_header = output@@ -549,153 +904,52 @@ impl Encoder {.flags().contains(format::Flags::GLOBAL_HEADER);- let mut video = ffmpeg::codec::Context::new_with_codec(codec)- .encoder()- .video()- .map_err(|error| ffi::fail("video encoder", path, error))?;- video.set_width(width);- video.set_height(height);- video.set_format(pixel_format);- video.set_time_base(time_base);- video.set_frame_rate(Some(rate));- let (primaries, transfer, matrix) = tags;- video.set_colorspace(matrix);- // The range the frames will be converted to below, so the tag is- // true; VideoToolbox reads it to pick its full- or video-range- // pixel format, the software encoders write it into the stream.- video.set_color_range(options.color_range.as_ffmpeg());- // SAFETY: `video` owns a live AVCodecContext; primaries and- // transfer have no setter in the bindings, and all three are plain- // fields the encoder reads at open.- unsafe {- let context = video.as_mut_ptr();- (*context).color_primaries = primaries.into();- (*context).color_trc = transfer.into();- if options.threads > 0 {- (*context).thread_count = i32::from(options.threads);- }- }- if global_header {- video.set_flags(ffmpeg::codec::Flags::GLOBAL_HEADER);- }- if pq {- // The display it was mastered on, where an encoder that writes- // it into the stream reads it.- // SAFETY: `video` owns a live AVCodecContext; the side data is- // allocated by FFmpeg at the struct's size and filled here.- unsafe {- let context = video.as_mut_ptr();- let entry = ffmpeg::sys::av_frame_side_data_new(- &mut (*context).decoded_side_data,- &mut (*context).nb_decoded_side_data,- ffmpeg::sys::AVFrameSideDataType::MASTERING_DISPLAY_METADATA,- std::mem::size_of::<MasteringDisplay>(),- 0,- );- if !entry.is_null() {- std::ptr::write(- (*entry).data.cast::<MasteringDisplay>(),- MasteringDisplay::p3_1000(),- );- }- }+ // Every encoder the linked FFmpeg has for the codec, best first,+ // and among them the first that opens. Being linked is not being+ // able: a phone's FFmpeg carries MediaCodec's encoders for every+ // codec, and the chip it runs on has a few of them.+ let candidates: Vec<(&'static str, ffmpeg::Codec)> = options+ .codec+ .linked(options.hardware, options.ten_bit)+ .filter_map(|name| encoder::find_by_name(name).map(|codec| (name, codec)))+ .collect();+ if candidates.is_empty() {+ return Err(Error::Missing {+ what: "encoder",+ name: if options.ten_bit {+ format!("{} at ten bits", options.codec.label())+ } else {+ options.codec.label().to_owned()+ },+ });}-- let crf = options.crf.to_string();- let cbr = options.rate_mode == RateMode::Cbr && options.bitrate_kbps > 0;- let bitrate = format!("{}k", options.bitrate_kbps);- // One second of VBV, not two. With a looser buffer x264 can stay- // well under b:v on calm content and never emit the padding that- // makes a CBR a CBR; `bufsize == bitrate` is where it starts- // holding the target.- let bufsize = format!("{}k", options.bitrate_kbps);- let settings = match encoder_name {- // libx264 / libx265 both take a bitrate, -minrate and -maxrate- // for CBR; VBR is the CRF that already shipped. The bitrate is- // `b`, the codec option's own name: `b:v` is the command line's- // spelling with a stream specifier, which the library does not- // know, so it was dropped - x264 then ran at its CRF under the- // cap, and x265 refused strict-cbr without a bitrate.- // Real CBR needs `nal-hrd=cbr` on x264, and `strict-cbr=1` on- // libx265. Without it x264 caps at maxrate but does not pad the- // output to b:v on content that does not need the bitrate, so an- // "8000k CBR" export of a calm clip comes out at whatever the- // content costs, not 8000k.- "libx264" if cbr => ffmpeg::dict! {- "preset" => options.preset.as_str(),- "b" => bitrate.as_str(),- "minrate" => bitrate.as_str(),- "maxrate" => bitrate.as_str(),- "bufsize" => bufsize.as_str(),- "x264-params" => "nal-hrd=cbr:force-cfr=1",- },- "libx265" if cbr => ffmpeg::dict! {- "preset" => options.preset.as_str(),- "b" => bitrate.as_str(),- "minrate" => bitrate.as_str(),- "maxrate" => bitrate.as_str(),- "bufsize" => bufsize.as_str(),- "x265-params" => "strict-cbr=1",- },- "libx264" | "libx265" => ffmpeg::dict! {- "preset" => options.preset.as_str(),- "crf" => crf.as_str(),- },- "libsvtav1" => ffmpeg::dict! {- "preset" => &svt_preset(&options.preset).to_string(),- // AV1's CRF runs to 63 and reads a few steps coarser than- // x264's; eight on is where the pictures match.- "crf" => &options.crf.saturating_add(8).min(63).to_string(),- },- "libaom-av1" => ffmpeg::dict! {- "crf" => &options.crf.saturating_add(8).min(63).to_string(),- "cpu-used" => "6",- },- "hevc_videotoolbox" => ffmpeg::dict! {- "q:v" => &videotoolbox_quality(options.crf).to_string(),- "profile" => if options.ten_bit { "main10" } else { "main" },- // A machine without the hardware still gets a file.- "allow_sw" => "1",- },- _ => ffmpeg::dict! {},- };- // HDR's own words to the software encoders, besides the tags they- // read off the context: headers with every keyframe, and a PQ- // file's mastering display.- let mut settings = settings;- if hdr.is_some() {- match encoder_name {- "libx265" => {- let mut params = settings- .get("x265-params")- .map(str::to_owned)- .unwrap_or_default();- for param in [- Some("repeat-headers=1".to_owned()),- pq.then(|| format!("hdr10=1:master-display={X265_MASTER_DISPLAY}")),- ]- .into_iter()- .flatten()- {- if !params.is_empty() {- params.push(':');- }- params.push_str(¶m);- }- settings.set("x265-params", ¶ms);+ let mut failure = None;+ let mut opened = None;+ for (name, codec) in candidates {+ match open_video(+ name,+ codec,+ path,+ (width, height),+ rate,+ options,+ tags,+ hdr,+ global_header,+ ) {+ Ok((encoder, pixel_format)) => {+ opened = Some((name, codec, encoder, pixel_format));+ break;}- "libsvtav1" if pq => {- settings.set(- "svtav1-params",- format!("mastering-display={SVT_MASTER_DISPLAY}"),- );+ Err(error) => {+ log::warn!("{name} would not open, trying the next encoder: {error}");+ failure = Some(error);}- _ => {}}}- let encoder = video- .open_with(settings)- .map_err(|error| ffi::fail("open encoder", path, error))?;+ let Some((encoder_name, codec, encoder, pixel_format)) = opened else {+ return Err(failure.expect("at least one encoder was tried"));+ };{let mut stream = output@@ -1176,6 +1430,97 @@ mod tests {)}+ /// An encoder steered by a bitrate alone is handed the rate the export+ /// sheet estimates the CRF at: 16 Mb/s for H.264 at 1080p30 and CRF 16,+ /// about a quarter of that at CRF 26, and less again for HEVC.+ #[test]+ fn a_bitrate_steered_encoder_gets_what_the_crf_comes_to() {+ let thirty = ffmpeg::Rational::new(30, 1);+ assert_eq!(implied_kbps(VideoCodec::H264, 16, 1920, 1080, thirty), 16_000);+ assert_eq!(implied_kbps(VideoCodec::H264, 26, 1920, 1080, thirty), 4_000);+ assert_eq!(implied_kbps(VideoCodec::Hevc, 16, 1920, 1080, thirty), 9_600);+ let sixty = ffmpeg::Rational::new(60, 1);+ assert_eq!(implied_kbps(VideoCodec::H264, 16, 3840, 2160, sixty), 128_000);+ // A thumbnail-sized file still gets a rate a chip will take.+ assert_eq!(implied_kbps(VideoCodec::H264, 26, 64, 64, thirty), 500);+ }++ /// Each kind of encoder is handed the pictures it reads, and ten bits+ /// only where it takes them.+ #[test]+ fn each_encoder_family_takes_its_own_pictures() {+ assert_eq!(Family::of("libx264").pixel_format(false), Pixel::YUV420P);+ assert_eq!(Family::of("libx265").pixel_format(true), Pixel::YUV420P10LE);+ assert_eq!(Family::of("hevc_videotoolbox").pixel_format(true), Pixel::P010LE);+ assert_eq!(Family::of("h264_mediacodec").pixel_format(false), Pixel::NV12);+ assert!(Family::of("h264_mediacodec").steered_by_bitrate());+ assert!(!Family::of("libx264").steered_by_bitrate());+ assert!(takes_ten_bits("libx265") && takes_ten_bits("hevc_videotoolbox"));+ for eight in ["h264_videotoolbox", "hevc_mediacodec", "h264_mediacodec", "hevc_mf"] {+ assert!(!takes_ten_bits(eight), "{eight}");+ }+ }++ /// Every codec has an encoder listed for every platform but AV1 on+ /// iOS, and a phone lists its hardware alone, whatever `hardware` says:+ /// its FFmpeg has nothing else.+ #[test]+ fn a_phone_is_asked_for_its_own_hardware() {+ for codec in VideoCodec::ALL {+ for hardware in [false, true] {+ let names = codec.encoders(hardware);+ if cfg!(target_os = "android") {+ assert!(names.iter().all(|name| name.ends_with("_mediacodec")));+ } else if cfg!(target_os = "ios") {+ assert!(names.iter().all(|name| name.ends_with("_videotoolbox")));+ } else {+ assert!(!names.is_empty(), "{codec:?}");+ }+ }+ // A desktop has a software encoder behind any hardware one, so+ // what is linked is what can be encoded.+ if !cfg!(any(target_os = "android", target_os = "ios")) {+ assert_eq!(codec.encodable(), codec.available(), "{codec:?}");+ }+ }+ }++ /// On a Mac, the fallback H.264 encoder for an FFmpeg without x264+ /// opens with the settings it is handed, and the iPhone's H.264 is the+ /// same encoder.+ #[test]+ fn the_hardware_h264_encoder_opens() {+ ffi::init();+ let Some(codec) = encoder::find_by_name("h264_videotoolbox") else {+ eprintln!("h264_videotoolbox not in the linked FFmpeg; skipped");+ return;+ };+ let path = std::env::temp_dir().join("concat-encode-h264-videotoolbox.mp4");+ let tags = (+ ffmpeg::color::Primaries::BT709,+ ffmpeg::color::TransferCharacteristic::BT709,+ ffmpeg::color::Space::BT709,+ );+ let options = EncodeOptions {+ codec: VideoCodec::H264,+ crf: 20,+ ..EncodeOptions::default()+ };+ let (_, format) = open_video(+ "h264_videotoolbox",+ codec,+ &path,+ (1280, 720),+ ffmpeg::Rational::new(30, 1),+ &options,+ tags,+ None,+ true,+ )+ .expect("opens");+ assert_eq!(format, Pixel::NV12);+ }+/// Every codec the linked FFmpeg has, at eight and ten bits, makes a/// file that says what it is: the codec, BT.709 all the way through,/// the bit depth asked for, and `hvc1` on HEVC.diff --git a/src/crates/concat/locales/de.json b/src/crates/concat/locales/de.jsonindex f497982f..d4ed416b 100644--- a/src/crates/concat/locales/de.json+++ b/src/crates/concat/locales/de.json@@ -562,6 +562,7 @@"export.high": "Hoch","export.master": "Master","export.mixingAudio": "Ton wird gemischt",+ "export.notOnThisDevice": "Nicht auf diesem Gerät","export.nothingTimelineExport": "Auf der Zeitleiste gibt es nichts zu exportieren","export.nothingToExport": "Nichts zu exportieren","export.output": "Ausgabe",diff --git a/src/crates/concat/locales/en.json b/src/crates/concat/locales/en.jsonindex 5a6fa9c1..7661c0e9 100644--- a/src/crates/concat/locales/en.json+++ b/src/crates/concat/locales/en.json@@ -562,6 +562,7 @@"export.high": "High","export.master": "Master","export.mixingAudio": "Mixing audio",+ "export.notOnThisDevice": "Not on this device","export.nothingTimelineExport": "There is nothing on the timeline to export","export.nothingToExport": "Nothing to export","export.output": "Output",diff --git a/src/crates/concat/locales/es.json b/src/crates/concat/locales/es.jsonindex db00d975..2f72efef 100644--- a/src/crates/concat/locales/es.json+++ b/src/crates/concat/locales/es.json@@ -562,6 +562,7 @@"export.high": "Alta","export.master": "Máster","export.mixingAudio": "Mezclando el audio",+ "export.notOnThisDevice": "No disponible en este dispositivo","export.nothingTimelineExport": "No hay nada en la línea de tiempo que exportar","export.nothingToExport": "Nada que exportar","export.output": "Salida",diff --git a/src/crates/concat/locales/fa.json b/src/crates/concat/locales/fa.jsonindex 8170812a..b2f2cb3b 100644--- a/src/crates/concat/locales/fa.json+++ b/src/crates/concat/locales/fa.json@@ -562,6 +562,7 @@"export.high": "بالا","export.master": "اصلی (Master)","export.mixingAudio": "در حال میکس کردن صدا",+ "export.notOnThisDevice": "در این دستگاه نیست","export.nothingTimelineExport": "چیزی روی تایملاین برای خروجی گرفتن وجود ندارد","export.nothingToExport": "چیزی برای خروجی گرفتن وجود ندارد","export.output": "خروجی",diff --git a/src/crates/concat/locales/fr.json b/src/crates/concat/locales/fr.jsonindex 924cc477..94ba4aae 100644--- a/src/crates/concat/locales/fr.json+++ b/src/crates/concat/locales/fr.json@@ -562,6 +562,7 @@"export.high": "Haute","export.master": "Master","export.mixingAudio": "Mixage de l'audio",+ "export.notOnThisDevice": "Pas sur cet appareil","export.nothingTimelineExport": "Il n'y a rien à exporter sur la timeline","export.nothingToExport": "Rien à exporter","export.output": "Sortie",diff --git a/src/crates/concat/locales/hr.json b/src/crates/concat/locales/hr.jsonindex 35bee0e6..9474053b 100644--- a/src/crates/concat/locales/hr.json+++ b/src/crates/concat/locales/hr.json@@ -562,6 +562,7 @@"export.high": "Visoko","export.master": "Master","export.mixingAudio": "Miksanje zvuka",+ "export.notOnThisDevice": "Nije na ovom uređaju","export.nothingTimelineExport": "Na vremenskoj liniji nema ničega za izvoz","export.nothingToExport": "Ništa za izvoz","export.output": "Izlaz",diff --git a/src/crates/concat/locales/it.json b/src/crates/concat/locales/it.jsonindex ee4b9c6a..6025139e 100644--- a/src/crates/concat/locales/it.json+++ b/src/crates/concat/locales/it.json@@ -562,6 +562,7 @@"export.high": "Alta","export.master": "Master","export.mixingAudio": "Mixaggio dell'audio",+ "export.notOnThisDevice": "Non su questo dispositivo","export.nothingTimelineExport": "Non c'è nulla sulla timeline da esportare","export.nothingToExport": "Niente da esportare","export.output": "Uscita",diff --git a/src/crates/concat/locales/ja.json b/src/crates/concat/locales/ja.jsonindex a049bf51..f0b7df22 100644--- a/src/crates/concat/locales/ja.json+++ b/src/crates/concat/locales/ja.json@@ -562,6 +562,7 @@"export.high": "高","export.master": "マスター","export.mixingAudio": "オーディオをミックス中",+ "export.notOnThisDevice": "このデバイスでは使用不可","export.nothingTimelineExport": "タイムラインに書き出すものがありません","export.nothingToExport": "書き出すものがありません","export.output": "出力",diff --git a/src/crates/concat/locales/ko.json b/src/crates/concat/locales/ko.jsonindex a8154452..187a0a85 100644--- a/src/crates/concat/locales/ko.json+++ b/src/crates/concat/locales/ko.json@@ -562,6 +562,7 @@"export.high": "높음","export.master": "마스터","export.mixingAudio": "오디오 믹싱 중",+ "export.notOnThisDevice": "이 기기에서 사용 불가","export.nothingTimelineExport": "타임라인에 내보낼 것이 없습니다","export.nothingToExport": "내보낼 것이 없습니다","export.output": "출력",diff --git a/src/crates/concat/locales/pt-BR.json b/src/crates/concat/locales/pt-BR.jsonindex 26ea3004..9cbb93d9 100644--- a/src/crates/concat/locales/pt-BR.json+++ b/src/crates/concat/locales/pt-BR.json@@ -562,6 +562,7 @@"export.high": "Alta","export.master": "Master","export.mixingAudio": "Mixando o áudio",+ "export.notOnThisDevice": "Indisponível neste dispositivo","export.nothingTimelineExport": "Não há nada na linha do tempo para exportar","export.nothingToExport": "Nada para exportar","export.output": "Saída",diff --git a/src/crates/concat/locales/ru.json b/src/crates/concat/locales/ru.jsonindex f4739ca4..c0619251 100644--- a/src/crates/concat/locales/ru.json+++ b/src/crates/concat/locales/ru.json@@ -562,6 +562,7 @@"export.high": "Высокое","export.master": "Мастер","export.mixingAudio": "Сведение звука",+ "export.notOnThisDevice": "Недоступно на этом устройстве","export.nothingTimelineExport": "На таймлайне нечего экспортировать","export.nothingToExport": "Нечего экспортировать","export.output": "Вывод",diff --git a/src/crates/concat/locales/tr.json b/src/crates/concat/locales/tr.jsonindex 2cda9f40..20c2c10c 100644--- a/src/crates/concat/locales/tr.json+++ b/src/crates/concat/locales/tr.json@@ -562,6 +562,7 @@"export.high": "Yüksek","export.master": "Master","export.mixingAudio": "Ses karıştırılıyor",+ "export.notOnThisDevice": "Bu cihazda yok","export.nothingTimelineExport": "Zaman çizelgesinde dışa aktarılacak bir şey yok","export.nothingToExport": "Dışa aktarılacak bir şey yok","export.output": "Çıktı",diff --git a/src/crates/concat/locales/zh-Hans.json b/src/crates/concat/locales/zh-Hans.jsonindex 61df08e0..0406657c 100644--- a/src/crates/concat/locales/zh-Hans.json+++ b/src/crates/concat/locales/zh-Hans.json@@ -562,6 +562,7 @@"export.high": "高","export.master": "母版","export.mixingAudio": "正在混合音频",+ "export.notOnThisDevice": "此设备不支持","export.nothingTimelineExport": "时间线上没有可导出的内容","export.nothingToExport": "没有可导出的内容","export.output": "输出",diff --git a/src/crates/concat/locales/zh-TW.json b/src/crates/concat/locales/zh-TW.jsonindex e4b8707e..039f5220 100644--- a/src/crates/concat/locales/zh-TW.json+++ b/src/crates/concat/locales/zh-TW.json@@ -562,6 +562,7 @@"export.high": "高","export.master": "母帶","export.mixingAudio": "正在混合音訊",+ "export.notOnThisDevice": "此裝置不支援","export.nothingTimelineExport": "時間軸上沒有可匯出的內容","export.nothingToExport": "沒有可匯出的內容","export.output": "輸出",diff --git a/src/crates/concat/src/panes/export.rs b/src/crates/concat/src/panes/export.rsindex 450aca6e..4b93b600 100644--- a/src/crates/concat/src/panes/export.rs+++ b/src/crates/concat/src/panes/export.rs@@ -147,7 +147,14 @@ impl ExportPane {}ExportMsg::RateChanged(index) => self.rate = (index.max(0) as usize).min(2),ExportMsg::QualityChanged(index) => self.quality = (index.max(0) as usize).min(2),- ExportMsg::CodecChanged(index) => self.codec = (index.max(0) as usize).min(2),+ // A codec this device cannot encode is not taken: the picker+ // says so under it, and the choice before stands.+ ExportMsg::CodecChanged(index) => {+ let index = (index.max(0) as usize).min(concat_media::VideoCodec::ALL.len() - 1);+ if concat_media::VideoCodec::ALL[index].encodable() {+ self.codec = index;+ }+ }ExportMsg::TenBitChanged(on) => self.ten_bit = on,ExportMsg::HdrChanged(on) => self.sdr = !on,ExportMsg::ColorRangeChanged(index) => {@@ -240,7 +247,7 @@ impl ExportPane {* pixels* (rate / 30.0)* self.codec(studio).size_factor()- * if self.ten_bit || self.hdr(studio) {+ * if self.deep(studio) {1.05} else {1.0@@ -261,10 +268,27 @@ impl ExportPane {}}- /// Whether the file is written HDR: the timeline is, and the sheet was- /// not told to tone-map it to SDR.+ /// Whether the file is written HDR: the timeline is, the sheet was+ /// not told to tone-map it to SDR, and the codec an HDR file would be+ /// written in can be written at ten bits here - on a phone it cannot,+ /// and the file is tone-mapped rather than failing.pub fn hdr(&self, studio: &Studio) -> bool {- studio.timeline().video.color_space.is_hdr() && !self.sdr+ let chosen =+ concat_media::VideoCodec::ALL[self.codec.min(concat_media::VideoCodec::ALL.len() - 1)];+ let deep = match chosen {+ concat_media::VideoCodec::H264 => concat_media::VideoCodec::Hevc,+ codec => codec,+ };+ studio.timeline().video.color_space.is_hdr()+ && !self.sdr+ && deep.ten_bit_available()+ && deep.encodable()+ }++ /// Whether the file is written at ten bits: HDR always, SDR when the+ /// switch asks and the codec can be written that deep here.+ pub fn deep(&self, studio: &Studio) -> bool {+ self.hdr(studio) || (self.ten_bit && self.codec(studio).ten_bit_available())}/// The range the file is written in: the Advanced section's choice@@ -306,7 +330,7 @@ impl ExportPane {crf: EXPORT_CRF[self.quality.min(2)],preset: "veryfast".into(),codec: self.codec(studio),- ten_bit: self.ten_bit || self.hdr(studio),+ ten_bit: self.deep(studio),rate_mode: if self.advanced && self.rate_mode == 1 {concat_media::RateMode::Cbr} else {@@ -409,9 +433,15 @@ impl ExportPane {.iter().position(|codec| *codec == self.codec(studio)).unwrap_or(0) as i32,+ h264_encodable: concat_media::VideoCodec::H264.encodable(),+ hevc_encodable: concat_media::VideoCodec::Hevc.encodable(),+ av1_encodable: concat_media::VideoCodec::Av1.encodable(),ten_bit: self.ten_bit,hdr_timeline: studio.timeline().video.color_space.is_hdr(),hdr: self.hdr(studio),+ deep_available: concat_media::VideoCodec::ALL+ .iter()+ .any(|codec| codec.ten_bit_available()),hdr_label: match studio.timeline().video.color_space {concat_project::model::ColorSpace::Pq => "HDR (PQ)",_ => "HDR (HLG)",@@ -427,7 +457,7 @@ impl ExportPane {// will be doing it.let codec = self.codec(studio);let mut words = vec![codec.label().to_owned()];- if self.ten_bit || self.hdr(studio) {+ if self.deep(studio) {words.push("10-bit".to_owned());}if self.hdr(studio) {@@ -442,11 +472,7 @@ impl ExportPane {if self.color_range() == ColorRange::Full {words.push(t("export.fullRange"));}- if codec- .encoders(true)- .first()- .is_some_and(|name| name.ends_with("_videotoolbox"))- {+ if codec.hardware_encoded(true, self.deep(studio)) {words.push(format!("· {}", t("export.hardware")));}words.join(" ")diff --git a/src/crates/concat/ui/dialogs/export.slint b/src/crates/concat/ui/dialogs/export.slintindex c66fa54b..bb9f2df1 100644--- a/src/crates/concat/ui/dialogs/export.slint+++ b/src/crates/concat/ui/dialogs/export.slint@@ -33,11 +33,19 @@ export struct ExportData {quality: int,/// index into the codec list: H.264, HEVC, AV1codec: int,+ /// Which of them this device can encode. A phone's chip may lack+ /// HEVC or AV1; the picker says so rather than failing on Export.+ h264-encodable: bool,+ hevc-encodable: bool,+ av1-encodable: bool,ten-bit: bool,/// The timeline is HDR, so the file can be written HDR or SDR.hdr-timeline: bool,/// It is being written HDR: ten bits, HEVC or AV1.hdr: bool,+ /// This platform can write ten bits at all. A phone's hardware+ /// encoders take eight, so neither the switch nor HDR is offered there.+ deep-available: bool,/// "HDR (HLG)" or "HDR (PQ)": the timeline's own.hdr-label: string,/// 0 = limited (video) range, 1 = full. Shown under Advanced.@@ -268,7 +276,7 @@ export component ExportDialog inherits Modal {// An HDR timeline is written as it is by default, or rolled off// to SDR for wherever HDR does not play.- if root.data.hdr-timeline: DialogRow {+ if root.data.hdr-timeline && root.data.deep-available: DialogRow {label: I18n.t("common.color");Select {current: root.data.hdr ? 0 : 1;@@ -287,7 +295,11 @@ export component ExportDialog inherits Modal {Select {current: root.data.codec;options: ["H.264", "HEVC", "AV1"];- details: [I18n.t("export.playsEverywhere"), I18n.t("export.halfTheSize"), I18n.t("export.smallest")];+ details: [+ root.data.h264-encodable ? I18n.t("export.playsEverywhere") : I18n.t("export.notOnThisDevice"),+ root.data.hevc-encodable ? I18n.t("export.halfTheSize") : I18n.t("export.notOnThisDevice"),+ root.data.av1-encodable ? I18n.t("export.smallest") : I18n.t("export.notOnThisDevice"),+ ];changed(index) => { root.codec-changed(index); }}}@@ -297,13 +309,16 @@ export component ExportDialog inherits Modal {Select {current: root.data.codec - 1;options: ["HEVC", "AV1"];- details: [I18n.t("export.halfTheSize"), I18n.t("export.smallest")];+ details: [+ root.data.hevc-encodable ? I18n.t("export.halfTheSize") : I18n.t("export.notOnThisDevice"),+ root.data.av1-encodable ? I18n.t("export.smallest") : I18n.t("export.notOnThisDevice"),+ ];changed(index) => { root.codec-changed(index + 1); }}}// HDR is ten bits always, so the switch is for SDR alone.- if !root.data.hdr: DialogRow {+ if !root.data.hdr && root.data.deep-available: DialogRow {label: I18n.t("export.tenBitColour");HorizontalLayout {alignment: end;