forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

export_presets.rs254 lines, 10.2 KiBHistoryRawDownload
1//! The export preset table, one list for the MCP `export_start { preset }` and the GUI's preset
2//! picker: [`presets`], [`by_id`], [`apply`]. Each [`Preset`] has a stable id, a label, a
3//! one-line description and a [`Target`] (category, video or audio-only, size) to group by. A preset names a [`Format`], a bitrate, a sound rate, a canvas size and (for sound
4//! alone) a loudness target; [`apply`] lays those over a caller's [`ExportSettings`] and leaves
5//! everything else (chapters, subtitles, seat, range, burn, bit depth unless the preset names one)
6//! as the caller had it.
7//!
8//! The video figures are YouTube's recommended SDR upload bitrates. The engine clamps an explicit
9//! bitrate to 50 Mb/s, so the 4K60 row sits at that ceiling rather than YouTube's 53-68.
10//! There is no standalone Opus file in this engine, so the second podcast row is Ogg Vorbis.
11
12use crate::export::{ExportSettings, Format, HdrOutput, HdrTransfer, LoudnessTarget, ProResProfile};
13
14/// Where a preset is meant to go: the group a picker lists it under.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub enum Category {
17 /// Long-form web video (YouTube).
18 Web,
19 /// Short vertical video (Shorts, Reels, TikTok).
20 Social,
21 /// Sound-only delivery (podcasts).
22 Audio,
23 /// Masters kept for later: high quality, large files.
24 Archive,
25}
26
27/// Whether the preset writes a picture or only sound.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29pub enum Output {
30 Video,
31 AudioOnly,
32}
33
34/// What a preset aims at: for a UI to group and badge presets by.
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub struct Target {
37 pub category: Category,
38 pub output: Output,
39 /// The canvas the file is drawn at, `(width, height)`; `None` keeps the project's (and is
40 /// always `None` for [`Output::AudioOnly`]).
41 pub size: Option<(u32, u32)>,
42}
43
44/// One row of the table. The fields are the export settings the preset sets; [`apply`] lays them
45/// over a caller's [`ExportSettings`].
46#[derive(Debug, Clone, Copy, PartialEq)]
47pub struct Preset {
48 /// Stable snake_case name: what `export_start { preset }` takes and a saved choice stores.
49 pub id: &'static str,
50 /// Short name for a list row.
51 pub label: &'static str,
52 /// One line saying what the file is and what it is for.
53 pub description: &'static str,
54 /// Group, output kind and target size.
55 pub target: Target,
56 pub format: Format,
57 /// Bits per second of the picture; `None` is the automatic figure.
58 pub bitrate: Option<u64>,
59 /// The sound's rate in kbps.
60 pub audio_kbps: u32,
61 /// `Some` sets the AV1 sample depth; `None` leaves the caller's.
62 pub video_bit_depth: Option<u8>,
63 /// Integrated loudness and true-peak ceiling the sound is levelled to, if any.
64 pub loudness: Option<LoudnessTarget>,
65 /// `Some` sets the ProRes profile of a ProRes file; `None` leaves the caller's.
66 pub prores_profile: Option<ProResProfile>,
67 /// The HDR transfer of an HEVC file; [`HdrTransfer::Sdr`] is none, and [`apply`] clears any
68 /// the caller had, since an HDR tag on another preset's file would be refused.
69 pub hdr: HdrTransfer,
70 /// `Some` sets a constant quantiser for the picture; `None` leaves the caller's.
71 pub qp: Option<u32>,
72 /// `Some` sets the seconds between key frames; `None` leaves the caller's.
73 pub keyframe_s: Option<f64>,
74 /// Whether the mp4 is laid out for streaming ([`ExportSettings::faststart`]).
75 pub faststart: bool,
76}
77
78const PODCAST: Option<LoudnessTarget> = Some(LoudnessTarget { lufs: -16.0, true_peak_dbtp: -1.0 });
79
80#[allow(clippy::too_many_arguments)]
81const fn video(
82 id: &'static str,
83 label: &'static str,
84 description: &'static str,
85 category: Category,
86 format: Format,
87 mbps: u64,
88 size: (u32, u32),
89 audio_kbps: u32,
90) -> Preset {
91 Preset {
92 id,
93 label,
94 description,
95 target: Target { category, output: Output::Video, size: Some(size) },
96 format,
97 bitrate: Some(mbps * 1_000_000),
98 audio_kbps,
99 video_bit_depth: None,
100 loudness: None,
101 prores_profile: None,
102 hdr: HdrTransfer::Sdr,
103 qp: None,
104 keyframe_s: None,
105 faststart: false,
106 }
107}
108
109const fn sound(
110 id: &'static str,
111 label: &'static str,
112 description: &'static str,
113 category: Category,
114 format: Format,
115 audio_kbps: u32,
116 loudness: Option<LoudnessTarget>,
117) -> Preset {
118 Preset {
119 id,
120 label,
121 description,
122 target: Target { category, output: Output::AudioOnly, size: None },
123 format,
124 bitrate: None,
125 audio_kbps,
126 video_bit_depth: None,
127 loudness,
128 prores_profile: None,
129 hdr: HdrTransfer::Sdr,
130 qp: None,
131 keyframe_s: None,
132 faststart: false,
133 }
134}
135
136const WEB: Category = Category::Web;
137const HD: (u32, u32) = (1920, 1080);
138const UHD: (u32, u32) = (3840, 2160);
139
140static TABLE: [Preset; 16] = [
141 video("youtube_1080p", "YouTube 1920×1080", "H.264 1080p30 at 8 Mb/s, AAC 384k: YouTube's recommended SDR upload.", WEB, Format::Mp4, 8, HD, 384),
142 video("youtube_1080p60", "YouTube 1920×1080 60fps", "H.264 1080p60 at 12 Mb/s, AAC 384k.", WEB, Format::Mp4, 12, HD, 384),
143 video("youtube_1080p_hevc", "YouTube 1920×1080 HEVC", "HEVC 1080p at 6 Mb/s in an mp4, AAC 384k: a smaller file at the same look.", WEB, Format::HevcMp4, 6, HD, 384),
144 video("youtube_4k", "YouTube 3840×2160", "H.264 2160p30 at 40 Mb/s, AAC 384k.", WEB, Format::Mp4, 40, UHD, 384),
145 video("youtube_4k60", "YouTube 3840×2160 60fps", "H.264 2160p60 at 50 Mb/s (the engine's ceiling; YouTube suggests 53-68), AAC 384k.", WEB, Format::Mp4, 50, UHD, 384),
146 video("youtube_4k_hevc", "YouTube 3840×2160 HEVC", "HEVC 2160p at 28 Mb/s in an mp4, AAC 384k.", WEB, Format::HevcMp4, 28, UHD, 384),
147 Preset {
148 id: "youtube_4k_master",
149 label: "YouTube 3840×2160 master",
150 description: "HEVC 2160p, constant QP 18 on the GPU, 0.5 s keyframes, AAC 384k, faststart: an upload YouTube re-encodes from.",
151 target: Target { category: WEB, output: Output::Video, size: Some(UHD) },
152 format: Format::HevcMp4,
153 bitrate: None,
154 audio_kbps: 384,
155 video_bit_depth: None,
156 loudness: None,
157 prores_profile: None,
158 hdr: HdrTransfer::Sdr,
159 qp: Some(18),
160 keyframe_s: Some(0.5),
161 faststart: true,
162 },
163 video("shorts_reels", "Shorts / Reels", "Vertical 9:16 1080x1920, H.264 at 8 Mb/s, AAC 192k.", Category::Social, Format::Mp4, 8, (1080, 1920), 192),
164 sound("podcast", "Podcast m4a", "AAC 128k in an m4a, levelled to -16 LUFS with a -1 dBTP ceiling.", Category::Audio, Format::M4a, 128, PODCAST),
165 sound("podcast_ogg", "Podcast ogg", "Vorbis in an ogg, -16 LUFS, -1 dBTP.", Category::Audio, Format::Ogg, 128, PODCAST),
166 sound("podcast_opus", "Podcast opus", "Opus 96k stereo in an Ogg file, levelled to -16 LUFS with a -1 dBTP ceiling.", Category::Audio, Format::Opus, 96, PODCAST),
167 Preset {
168 id: "archive",
169 label: "Archive 10-bit AV1",
170 description: "10-bit AV1 in an mkv at 30 Mb/s, the project's size, audio at the top AAC rate.",
171 target: Target { category: Category::Archive, output: Output::Video, size: None },
172 format: Format::Av1,
173 bitrate: Some(30_000_000),
174 audio_kbps: 320,
175 video_bit_depth: Some(10),
176 loudness: None,
177 prores_profile: None,
178 hdr: HdrTransfer::Sdr,
179 qp: None,
180 keyframe_s: None,
181 faststart: false,
182 },
183 Preset {
184 id: "archive_prores",
185 label: "ProRes 422 HQ master",
186 description: "ProRes 422 HQ in a QuickTime .mov at the project's size, 24-bit PCM sound: an editing master.",
187 target: Target { category: Category::Archive, output: Output::Video, size: None },
188 format: Format::ProRes,
189 bitrate: None,
190 audio_kbps: 320,
191 video_bit_depth: None,
192 loudness: None,
193 prores_profile: Some(ProResProfile::Hq),
194 hdr: HdrTransfer::Sdr,
195 qp: None,
196 keyframe_s: None,
197 faststart: false,
198 },
199 Preset {
200 id: "hdr10_hevc",
201 label: "HDR10 (HEVC 10-bit)",
202 description: "10-bit HEVC in an mp4 tagged HDR10 (PQ, BT.2020) at 40 Mb/s, the project's size, AAC 384k.",
203 target: Target { category: Category::Archive, output: Output::Video, size: None },
204 format: Format::HevcMp4,
205 bitrate: Some(40_000_000),
206 audio_kbps: 384,
207 video_bit_depth: Some(10),
208 loudness: None,
209 prores_profile: None,
210 hdr: HdrTransfer::Pq,
211 qp: None,
212 keyframe_s: None,
213 faststart: false,
214 },
215 sound("archive_flac", "Archive FLAC", "Lossless FLAC of the mix.", Category::Archive, Format::Flac, 320, None),
216 sound("archive_wav", "Archive WAV", "16-bit PCM WAV of the mix.", Category::Archive, Format::Wav, 320, None),
217];
218
219/// Every preset, in the order a picker lists them (grouped by [`Target::category`]).
220pub fn presets() -> &'static [Preset] {
221 &TABLE
222}
223
224/// The preset whose [`Preset::id`] is `id`.
225pub fn by_id(id: &str) -> Option<&'static Preset> {
226 TABLE.iter().find(|p| p.id == id)
227}
228
229impl Preset {
230 /// The settings this preset alone describes, over [`ExportSettings::default`].
231 pub fn settings(&self) -> ExportSettings {
232 apply(self, ExportSettings::default())
233 }
234}
235
236/// `base` with the preset's format, bitrate, sound rate, size, loudness and HDR transfer laid over it.
237/// Everything else stays as `base` had it -- chapters, subtitles, seat, range -- and the bit depth
238/// changes only for a preset that names one.
239pub fn apply(preset: &Preset, base: ExportSettings) -> ExportSettings {
240 ExportSettings {
241 format: preset.format,
242 bitrate: preset.bitrate,
243 audio_kbps: Some(preset.audio_kbps),
244 size: preset.target.size,
245 video_bit_depth: preset.video_bit_depth.or(base.video_bit_depth),
246 loudness: preset.loudness,
247 prores_profile: preset.prores_profile.unwrap_or(base.prores_profile),
248 hdr: HdrOutput { transfer: preset.hdr, ..HdrOutput::default() },
249 qp: preset.qp.or(base.qp),
250 keyframe_s: preset.keyframe_s.or(base.keyframe_s),
251 faststart: preset.faststart || base.faststart,
252 ..base
253 }
254}