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
//! Error types for encoding operations.
use std::path::PathBuf;
use thiserror::Error;
/// Encoding error type.
#[derive(Error, Debug)]
pub enum EncodeError {
/// Cannot create output file
#[error("Cannot create output file: {path}")]
CannotCreateFile {
/// File path that failed
path: PathBuf,
},
/// Unsupported codec
#[error("Unsupported codec: {codec}")]
UnsupportedCodec {
/// Codec name
codec: String,
},
/// No suitable encoder found
#[error("No suitable encoder found for {codec} (tried: {tried:?})")]
NoSuitableEncoder {
/// Requested codec
codec: String,
/// Attempted encoders
tried: Vec<String>,
},
/// Encoding failed at specific frame
#[error("Encoding failed at frame {frame}: {reason}")]
EncodingFailed {
/// Frame number where encoding failed
frame: u64,
/// Failure reason
reason: String,
},
/// Invalid configuration
#[error("Invalid configuration: {reason}")]
InvalidConfig {
/// Configuration issue description
reason: String,
},
/// Hardware encoder unavailable
#[error("Hardware encoder unavailable: {encoder}")]
HwEncoderUnavailable {
/// Hardware encoder name
encoder: String,
},
/// Specific encoder is unavailable — the hint explains what is needed.
#[error("encoder unavailable: codec={codec} hint={hint}")]
EncoderUnavailable {
/// Requested codec name (e.g. `"h265/hevc"`).
codec: String,
/// Human-readable guidance (e.g. how to build FFmpeg with this encoder).
hint: String,
},
/// Muxing failed
#[error("Muxing failed: {reason}")]
MuxingFailed {
/// Failure reason
reason: String,
},
/// `FFmpeg` error
#[error("ffmpeg error: {message} (code={code})")]
Ffmpeg {
/// Raw `FFmpeg` error code (negative integer). `0` when no numeric code is available.
code: i32,
/// Human-readable error message from `av_strerror` or an internal description.
message: String,
},
/// IO error
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
/// Invalid option value
#[error("Invalid option: {name} — {reason}")]
InvalidOption {
/// Option name
name: String,
/// Description of the constraint that was violated
reason: String,
},
/// Codec is incompatible with the target container format
#[error("codec {codec} is not supported by container {container} — {hint}")]
UnsupportedContainerCodecCombination {
/// Container format name (e.g. `"webm"`)
container: String,
/// Codec name that was rejected (e.g. `"h264"`)
codec: String,
/// Human-readable guidance on compatible codecs
hint: String,
},
/// Video dimensions are outside the supported range [2, 32768].
#[error("dimensions {width}x{height} out of range [2, 32768]")]
InvalidDimensions {
/// Requested frame width.
width: u32,
/// Requested frame height.
height: u32,
},
/// Target bitrate exceeds the 800 Mbps maximum.
#[error("bitrate {bitrate} bps exceeds maximum 800 Mbps (800,000,000 bps)")]
InvalidBitrate {
/// Requested bitrate in bits per second.
bitrate: u64,
},
/// Audio channel count exceeds the supported maximum of 8.
#[error("channel count {count} exceeds maximum 8")]
InvalidChannelCount {
/// Requested channel count.
count: u32,
},
/// Audio sample rate is outside the supported range [8000, 384000] Hz.
#[error("sample rate {rate} Hz outside supported range [8000, 384000]")]
InvalidSampleRate {
/// Requested sample rate in Hz.
rate: u32,
},
/// Encoding cancelled by user
#[error("Encoding cancelled by user")]
Cancelled,
/// Async encoder worker thread panicked or disconnected unexpectedly
#[error("Async encoder worker panicked or disconnected")]
WorkerPanicked,
/// A media operation (trim, extract, replace, …) failed.
///
/// Returned by [`StreamCopyTrim`](crate::StreamCopyTrim) and other
/// `media_ops` types when a structural precondition is violated or an
/// FFmpeg mux/remux call fails.
#[error("media operation failed: {reason}")]
MediaOperationFailed {
/// Human-readable description of the failure.
reason: String,
},
/// An export preset violated a platform-specific constraint.
///
/// Returned by [`ExportPreset::validate()`](crate::ExportPreset::validate)
/// when the preset's configuration conflicts with a platform rule (e.g.
/// fps > 60 for a YouTube preset).
#[error("preset constraint violated: preset={preset} reason={reason}")]
PresetConstraintViolation {
/// Name of the preset that failed validation.
preset: String,
/// Human-readable description of the violated constraint.
reason: String,
},
}
impl EncodeError {
/// Create an error from an FFmpeg error code.
///
/// This is more type-safe than implementing `From<i32>` globally,
/// as it makes the conversion explicit and prevents accidental
/// conversion of arbitrary i32 values.
pub(crate) fn from_ffmpeg_error(errnum: i32) -> Self {
EncodeError::Ffmpeg {
code: errnum,
message: ff_sys::av_error_string(errnum),
}
}
}
#[cfg(test)]
mod tests {
use super::EncodeError;
#[test]
fn from_ffmpeg_error_should_return_ffmpeg_variant() {
let err = EncodeError::from_ffmpeg_error(ff_sys::error_codes::EINVAL);
assert!(matches!(err, EncodeError::Ffmpeg { .. }));
}
#[test]
fn from_ffmpeg_error_should_carry_numeric_code() {
let err = EncodeError::from_ffmpeg_error(ff_sys::error_codes::EINVAL);
match err {
EncodeError::Ffmpeg { code, .. } => assert_eq!(code, ff_sys::error_codes::EINVAL),
_ => panic!("expected Ffmpeg variant"),
}
}
#[test]
fn from_ffmpeg_error_should_format_with_code_in_display() {
let err = EncodeError::from_ffmpeg_error(ff_sys::error_codes::EINVAL);
let msg = err.to_string();
assert!(msg.contains("code=-22"), "expected 'code=-22' in '{msg}'");
}
#[test]
fn from_ffmpeg_error_message_should_be_nonempty() {
let err = EncodeError::from_ffmpeg_error(ff_sys::error_codes::ENOMEM);
assert!(!err.to_string().is_empty());
}
#[test]
fn from_ffmpeg_error_eof_should_be_constructible() {
let err = EncodeError::from_ffmpeg_error(ff_sys::error_codes::EOF);
assert!(matches!(err, EncodeError::Ffmpeg { .. }));
assert!(!err.to_string().is_empty());
}
#[test]
fn invalid_dimensions_display_should_contain_dimension_string() {
let err = EncodeError::InvalidDimensions {
width: 0,
height: 720,
};
let msg = err.to_string();
assert!(msg.contains("0x720"), "expected '0x720' in '{msg}'");
}
#[test]
fn invalid_dimensions_display_should_contain_range_hint() {
let err = EncodeError::InvalidDimensions {
width: 99999,
height: 99999,
};
let msg = err.to_string();
assert!(
msg.contains("[2, 32768]"),
"expected '[2, 32768]' in '{msg}'"
);
}
#[test]
fn invalid_bitrate_display_should_contain_bitrate_value() {
let err = EncodeError::InvalidBitrate {
bitrate: 900_000_000,
};
let msg = err.to_string();
assert!(msg.contains("900000000"), "expected '900000000' in '{msg}'");
}
#[test]
fn invalid_bitrate_display_should_contain_maximum_hint() {
let err = EncodeError::InvalidBitrate {
bitrate: 900_000_000,
};
let msg = err.to_string();
assert!(
msg.contains("800,000,000"),
"expected '800,000,000' in '{msg}'"
);
}
#[test]
fn invalid_channel_count_display_should_contain_count() {
let err = EncodeError::InvalidChannelCount { count: 9 };
let msg = err.to_string();
assert!(msg.contains('9'), "expected '9' in '{msg}'");
}
#[test]
fn invalid_channel_count_display_should_contain_maximum_hint() {
let err = EncodeError::InvalidChannelCount { count: 9 };
let msg = err.to_string();
assert!(msg.contains('8'), "expected '8' in '{msg}'");
}
#[test]
fn invalid_sample_rate_display_should_contain_rate() {
let err = EncodeError::InvalidSampleRate { rate: 7999 };
let msg = err.to_string();
assert!(msg.contains("7999"), "expected '7999' in '{msg}'");
}
#[test]
fn invalid_sample_rate_display_should_contain_range_hint() {
let err = EncodeError::InvalidSampleRate { rate: 7999 };
let msg = err.to_string();
assert!(
msg.contains("[8000, 384000]"),
"expected '[8000, 384000]' in '{msg}'"
);
}
#[test]
fn encode_error_media_operation_failed_should_display_correctly() {
let err = EncodeError::MediaOperationFailed {
reason: "input file has no audio stream".to_string(),
};
let msg = err.to_string();
assert!(
msg.contains("media operation failed"),
"expected 'media operation failed' in '{msg}'"
);
assert!(
msg.contains("input file has no audio stream"),
"expected reason in '{msg}'"
);
assert!(
matches!(err, EncodeError::MediaOperationFailed { .. }),
"pattern match with struct syntax must work"
);
}
}