1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! TranscodeProfile — shareable encoding configuration with JSON import/export.
//!
//! A `TranscodeProfile` encapsulates all the settings needed to describe a
//! complete transcode operation (codecs, bitrates, quality, filters) and can
//! be serialised to / deserialised from JSON for sharing between operators,
//! pre-sets libraries, and tooling integration.
use serde::{Deserialize, Serialize};
use crate::{LoudnessStandard, MultiPassMode, QualityMode, Result, TranscodeError};
// ─── Profile components ───────────────────────────────────────────────────────
/// Video encoding parameters within a `TranscodeProfile`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VideoProfileParams {
/// Codec name (e.g. `"h264"`, `"av1"`, `"vp9"`).
pub codec: String,
/// Target bitrate in bits per second. `None` means CRF/quality-based.
pub bitrate_bps: Option<u64>,
/// Constant Rate Factor (0–63 for AV1, 0–51 for H.264).
pub crf: Option<u8>,
/// Encoder speed preset (e.g. `"slow"`, `"medium"`, `"fast"`).
pub preset: Option<String>,
/// Codec profile (e.g. `"high"`, `"main"`, `"baseline"`).
pub profile: Option<String>,
/// Output width in pixels.
pub width: Option<u32>,
/// Output height in pixels.
pub height: Option<u32>,
/// Output frame rate as `(numerator, denominator)`.
pub frame_rate: Option<(u32, u32)>,
/// Number of encoding threads (0 = auto).
pub threads: u32,
/// Quality mode used when CRF and bitrate are both absent.
pub quality_mode: Option<QualityMode>,
/// Enable row-based multi-threading (AV1 / VP9).
pub row_mt: bool,
/// Number of tile columns (AV1 tile-based parallel encoding, log2).
pub tile_columns: Option<u8>,
/// Number of tile rows (AV1 tile-based parallel encoding, log2).
pub tile_rows: Option<u8>,
}
impl Default for VideoProfileParams {
fn default() -> Self {
Self {
codec: "h264".into(),
bitrate_bps: None,
crf: Some(23),
preset: Some("medium".into()),
profile: Some("high".into()),
width: None,
height: None,
frame_rate: None,
threads: 0,
quality_mode: None,
row_mt: true,
tile_columns: None,
tile_rows: None,
}
}
}
/// Audio encoding parameters within a `TranscodeProfile`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AudioProfileParams {
/// Codec name (e.g. `"aac"`, `"opus"`, `"flac"`).
pub codec: String,
/// Target bitrate in bits per second.
pub bitrate_bps: u64,
/// Output sample rate in Hz.
pub sample_rate: u32,
/// Number of output channels.
pub channels: u8,
/// Whether to apply integrated-loudness normalisation.
pub normalize: bool,
/// Loudness target standard.
pub loudness_standard: Option<LoudnessStandard>,
/// Target loudness in LUFS (overrides `loudness_standard` when set).
pub target_lufs: Option<f64>,
}
impl Default for AudioProfileParams {
fn default() -> Self {
Self {
codec: "aac".into(),
bitrate_bps: 192_000,
sample_rate: 48_000,
channels: 2,
normalize: false,
loudness_standard: None,
target_lufs: None,
}
}
}
/// Container / muxer settings within a `TranscodeProfile`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ContainerParams {
/// Container format (e.g. `"mp4"`, `"mkv"`, `"webm"`).
pub format: String,
/// Whether to fast-start MP4 for web delivery (moov atom at front).
pub mp4_fast_start: bool,
/// Whether to preserve all metadata from the source.
pub preserve_metadata: bool,
}
impl Default for ContainerParams {
fn default() -> Self {
Self {
format: "mkv".into(),
mp4_fast_start: false,
preserve_metadata: true,
}
}
}
// ─── TranscodeProfile ─────────────────────────────────────────────────────────
/// A complete, shareable encoding configuration.
///
/// # Example (round-trip)
///
/// ```
/// use oximedia_transcode::transcode_profile::TranscodeProfile;
///
/// let profile = TranscodeProfile::youtube_1080p();
/// let json = profile.to_json().expect("serialise");
/// let loaded = TranscodeProfile::from_json(&json).expect("deserialise");
/// assert_eq!(loaded.name, profile.name);
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TranscodeProfile {
/// Human-readable name for this profile (e.g. `"YouTube 1080p"`).
pub name: String,
/// Optional description explaining intended use.
pub description: Option<String>,
/// Profile schema version (for future migrations).
pub version: u32,
/// Video encoding parameters.
pub video: VideoProfileParams,
/// Audio encoding parameters.
pub audio: AudioProfileParams,
/// Container / muxer parameters.
pub container: ContainerParams,
/// Multi-pass encoding mode.
pub multi_pass: MultiPassMode,
/// Arbitrary key/value tags for tooling metadata.
pub tags: Vec<(String, String)>,
}
impl TranscodeProfile {
/// Creates a blank profile with default parameters.
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
description: None,
version: 1,
video: VideoProfileParams::default(),
audio: AudioProfileParams::default(),
container: ContainerParams::default(),
multi_pass: MultiPassMode::SinglePass,
tags: Vec::new(),
}
}
/// Sets the description.
#[must_use]
pub fn description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
/// Sets the video parameters.
#[must_use]
pub fn video(mut self, params: VideoProfileParams) -> Self {
self.video = params;
self
}
/// Sets the audio parameters.
#[must_use]
pub fn audio(mut self, params: AudioProfileParams) -> Self {
self.audio = params;
self
}
/// Sets the container parameters.
#[must_use]
pub fn container(mut self, params: ContainerParams) -> Self {
self.container = params;
self
}
/// Sets the multi-pass mode.
#[must_use]
pub fn multi_pass(mut self, mode: MultiPassMode) -> Self {
self.multi_pass = mode;
self
}
/// Adds a tag to the profile.
#[must_use]
pub fn tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.tags.push((key.into(), value.into()));
self
}
// ── Built-in presets ──────────────────────────────────────────────────────
/// YouTube 1080p upload profile (H.264 + AAC).
#[must_use]
pub fn youtube_1080p() -> Self {
Self::new("YouTube 1080p")
.description("H.264 High 1080p for YouTube upload")
.video(VideoProfileParams {
codec: "h264".into(),
crf: Some(18),
preset: Some("slow".into()),
profile: Some("high".into()),
width: Some(1920),
height: Some(1080),
frame_rate: Some((30, 1)),
..VideoProfileParams::default()
})
.audio(AudioProfileParams {
codec: "aac".into(),
bitrate_bps: 192_000,
sample_rate: 48_000,
channels: 2,
normalize: true,
loudness_standard: Some(LoudnessStandard::EbuR128),
..AudioProfileParams::default()
})
.container(ContainerParams {
format: "mp4".into(),
mp4_fast_start: true,
preserve_metadata: true,
})
}
/// Podcast / audio-only Opus profile (EBU R128 normalised).
#[must_use]
pub fn podcast_opus() -> Self {
Self::new("Podcast Opus")
.description("Opus mono/stereo for podcast distribution (EBU R128 −23 LUFS)")
.video(VideoProfileParams {
codec: "none".into(),
..VideoProfileParams::default()
})
.audio(AudioProfileParams {
codec: "opus".into(),
bitrate_bps: 96_000,
sample_rate: 48_000,
channels: 2,
normalize: true,
loudness_standard: Some(LoudnessStandard::EbuR128),
..AudioProfileParams::default()
})
.container(ContainerParams {
format: "ogg".into(),
mp4_fast_start: false,
preserve_metadata: true,
})
}
/// AV1 1080p archive profile (CRF 28, tile-based parallel).
#[must_use]
pub fn av1_1080p_archive() -> Self {
Self::new("AV1 1080p Archive")
.description("High-efficiency AV1 1080p for long-term archival")
.video(VideoProfileParams {
codec: "av1".into(),
crf: Some(28),
preset: Some("5".into()),
width: Some(1920),
height: Some(1080),
row_mt: true,
tile_columns: Some(2),
tile_rows: Some(1),
..VideoProfileParams::default()
})
.audio(AudioProfileParams {
codec: "opus".into(),
bitrate_bps: 192_000,
..AudioProfileParams::default()
})
.container(ContainerParams {
format: "mkv".into(),
..ContainerParams::default()
})
.multi_pass(MultiPassMode::TwoPass)
}
// ── JSON serialisation ────────────────────────────────────────────────────
/// Serialises the profile to a JSON string.
///
/// # Errors
///
/// Returns an error if serialisation fails (should not happen for valid profiles).
pub fn to_json(&self) -> Result<String> {
serde_json::to_string_pretty(self)
.map_err(|e| TranscodeError::CodecError(format!("Profile serialisation failed: {e}")))
}
/// Serialises the profile to a compact (non-pretty) JSON string.
///
/// # Errors
///
/// Returns an error if serialisation fails.
pub fn to_json_compact(&self) -> Result<String> {
serde_json::to_string(self)
.map_err(|e| TranscodeError::CodecError(format!("Profile serialisation failed: {e}")))
}
/// Deserialises a profile from a JSON string.
///
/// # Errors
///
/// Returns an error if the JSON is malformed or the schema does not match.
pub fn from_json(json: &str) -> Result<Self> {
serde_json::from_str(json).map_err(|e| {
TranscodeError::InvalidInput(format!("Profile deserialisation failed: {e}"))
})
}
/// Saves the profile to a file in JSON format.
///
/// # Errors
///
/// Returns an error if the file cannot be written.
pub fn save_to_file(&self, path: &std::path::Path) -> Result<()> {
let json = self.to_json()?;
std::fs::write(path, json.as_bytes()).map_err(|e| {
TranscodeError::IoError(format!("Cannot write profile to '{}': {e}", path.display()))
})
}
/// Loads a profile from a JSON file.
///
/// # Errors
///
/// Returns an error if the file cannot be read or parsed.
pub fn load_from_file(path: &std::path::Path) -> Result<Self> {
let data = std::fs::read_to_string(path).map_err(|e| {
TranscodeError::IoError(format!(
"Cannot read profile from '{}': {e}",
path.display()
))
})?;
Self::from_json(&data)
}
}
// ─── Tests ────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
use std::env::temp_dir;
#[test]
fn test_profile_new() {
let p = TranscodeProfile::new("Test");
assert_eq!(p.name, "Test");
assert_eq!(p.version, 1);
assert!(p.description.is_none());
assert!(p.tags.is_empty());
}
#[test]
fn test_json_round_trip() {
let original = TranscodeProfile::youtube_1080p();
let json = original.to_json().expect("serialise");
let loaded = TranscodeProfile::from_json(&json).expect("deserialise");
assert_eq!(loaded.name, original.name);
assert_eq!(loaded.video.codec, original.video.codec);
assert_eq!(loaded.video.width, Some(1920));
assert_eq!(loaded.audio.codec, "aac");
assert_eq!(loaded.container.format, "mp4");
}
#[test]
fn test_json_compact() {
let p = TranscodeProfile::podcast_opus();
let json = p.to_json_compact().expect("compact json");
assert!(
!json.contains('\n'),
"compact json should not contain newlines"
);
}
#[test]
fn test_invalid_json_returns_error() {
let result = TranscodeProfile::from_json("not valid json {{{");
assert!(result.is_err());
}
#[test]
fn test_podcast_profile() {
let p = TranscodeProfile::podcast_opus();
assert_eq!(p.audio.codec, "opus");
assert_eq!(p.audio.sample_rate, 48_000);
assert!(p.audio.normalize);
assert_eq!(p.container.format, "ogg");
}
#[test]
fn test_av1_archive_profile() {
let p = TranscodeProfile::av1_1080p_archive();
assert_eq!(p.video.codec, "av1");
assert_eq!(p.video.tile_columns, Some(2));
assert_eq!(p.video.tile_rows, Some(1));
assert!(p.video.row_mt);
assert_eq!(p.multi_pass, MultiPassMode::TwoPass);
}
#[test]
fn test_save_and_load_file() {
let path = temp_dir().join("oximedia_test_profile.json");
let original = TranscodeProfile::youtube_1080p().tag("env", "ci");
original.save_to_file(&path).expect("save ok");
let loaded = TranscodeProfile::load_from_file(&path).expect("load ok");
assert_eq!(loaded.name, original.name);
assert_eq!(loaded.tags, vec![("env".to_string(), "ci".to_string())]);
std::fs::remove_file(&path).ok();
}
#[test]
fn test_tag_builder() {
let p = TranscodeProfile::new("Tagged")
.tag("author", "ci")
.tag("project", "oximedia");
assert_eq!(p.tags.len(), 2);
assert_eq!(p.tags[0], ("author".into(), "ci".into()));
}
#[test]
fn test_profile_description() {
let p = TranscodeProfile::new("Desc test").description("A helpful description");
assert_eq!(p.description.as_deref(), Some("A helpful description"));
}
#[test]
fn test_video_profile_default_codec() {
let vp = VideoProfileParams::default();
assert_eq!(vp.codec, "h264");
assert!(vp.row_mt);
}
#[test]
fn test_audio_profile_default_codec() {
let ap = AudioProfileParams::default();
assert_eq!(ap.codec, "aac");
assert_eq!(ap.channels, 2);
}
}