yt-dlp 2.7.2

🎬️ A Rust library (with auto dependencies installation) for Youtube downloading
Documentation
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Post-processing configuration for video and audio processing.
//!
//! This module provides comprehensive post-processing options using FFmpeg,
//! including codec conversion, bitrate adjustment, video filters, and more.

use std::fmt;

/// Video codec options for encoding
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub enum VideoCodec {
    /// H.264/AVC codec (libx264)
    H264,
    /// H.265/HEVC codec (libx265)
    H265,
    /// VP9 codec (libvpx-vp9)
    VP9,
    /// AV1 codec (libaom-av1)
    AV1,
    /// Copy video stream without re-encoding
    #[default]
    Copy,
}

impl VideoCodec {
    /// Converts to FFmpeg codec name
    ///
    /// # Returns
    ///
    /// The FFmpeg codec name string
    pub fn to_ffmpeg_name(&self) -> &str {
        match self {
            Self::H264 => "libx264",
            Self::H265 => "libx265",
            Self::VP9 => "libvpx-vp9",
            Self::AV1 => "libaom-av1",
            Self::Copy => "copy",
        }
    }
}

impl fmt::Display for VideoCodec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::H264 => f.write_str("H264"),
            Self::H265 => f.write_str("H265"),
            Self::VP9 => f.write_str("VP9"),
            Self::AV1 => f.write_str("AV1"),
            Self::Copy => f.write_str("Copy"),
        }
    }
}

/// Audio codec options for encoding
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub enum AudioCodec {
    /// AAC codec    
    AAC,
    /// MP3 codec (libmp3lame)
    MP3,
    /// Opus codec
    Opus,
    /// Vorbis codec
    Vorbis,
    /// Copy audio stream without re-encoding
    #[default]
    Copy,
}

impl AudioCodec {
    /// Converts to FFmpeg codec name
    ///
    /// # Returns
    ///
    /// The FFmpeg codec name string
    pub fn to_ffmpeg_name(&self) -> &str {
        match self {
            Self::AAC => "aac",
            Self::MP3 => "libmp3lame",
            Self::Opus => "libopus",
            Self::Vorbis => "libvorbis",
            Self::Copy => "copy",
        }
    }
}

impl fmt::Display for AudioCodec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::AAC => f.write_str("AAC"),
            Self::MP3 => f.write_str("MP3"),
            Self::Opus => f.write_str("Opus"),
            Self::Vorbis => f.write_str("Vorbis"),
            Self::Copy => f.write_str("Copy"),
        }
    }
}

/// Video resolution preset
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum Resolution {
    /// 7680x4320 (8K)
    UHD8K,
    /// 3840x2160 (4K)
    UHD4K,
    /// 2560x1440 (2K/QHD)
    QHD,
    /// 1920x1080 (Full HD)
    FullHD,
    /// 1280x720 (HD)
    HD,
    /// 854x480 (SD)
    SD,
    /// 640x360
    Low,
    /// Custom resolution
    Custom { width: u32, height: u32 },
}

impl Resolution {
    /// Returns the width and height for this resolution
    ///
    /// # Returns
    ///
    /// A tuple (width, height) in pixels
    pub fn dimensions(&self) -> (u32, u32) {
        match self {
            Self::UHD8K => (7680, 4320),
            Self::UHD4K => (3840, 2160),
            Self::QHD => (2560, 1440),
            Self::FullHD => (1920, 1080),
            Self::HD => (1280, 720),
            Self::SD => (854, 480),
            Self::Low => (640, 360),
            Self::Custom { width, height } => (*width, *height),
        }
    }

    /// Converts to FFmpeg scale filter format
    ///
    /// # Returns
    ///
    /// FFmpeg scale filter string (e.g., "1920:1080")
    pub fn to_ffmpeg_scale(&self) -> String {
        let (width, height) = self.dimensions();
        format!("{}:{}", width, height)
    }
}

impl fmt::Display for Resolution {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UHD8K => f.write_str("UHD8K"),
            Self::UHD4K => f.write_str("UHD4K"),
            Self::QHD => f.write_str("QHD"),
            Self::FullHD => f.write_str("FullHD"),
            Self::HD => f.write_str("HD"),
            Self::SD => f.write_str("SD"),
            Self::Low => f.write_str("Low"),
            Self::Custom { width, height } => {
                write!(f, "Custom(width={}, height={})", width, height)
            }
        }
    }
}

/// Encoding preset for quality/speed trade-off
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
pub enum EncodingPreset {
    /// Ultra fast encoding (lowest quality)
    UltraFast,
    /// Super fast encoding
    SuperFast,
    /// Very fast encoding
    VeryFast,
    /// Fast encoding
    Fast,
    /// Medium encoding (balanced)
    #[default]
    Medium,
    /// Slow encoding (better quality)
    Slow,
    /// Slower encoding
    Slower,
    /// Very slow encoding (best quality)
    VerySlow,
}

impl EncodingPreset {
    /// Converts to FFmpeg preset name
    ///
    /// # Returns
    ///
    /// The FFmpeg preset name string
    pub fn to_ffmpeg_name(&self) -> &str {
        match self {
            Self::UltraFast => "ultrafast",
            Self::SuperFast => "superfast",
            Self::VeryFast => "veryfast",
            Self::Fast => "fast",
            Self::Medium => "medium",
            Self::Slow => "slow",
            Self::Slower => "slower",
            Self::VerySlow => "veryslow",
        }
    }
}

impl fmt::Display for EncodingPreset {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UltraFast => f.write_str("UltraFast"),
            Self::SuperFast => f.write_str("SuperFast"),
            Self::VeryFast => f.write_str("VeryFast"),
            Self::Fast => f.write_str("Fast"),
            Self::Medium => f.write_str("Medium"),
            Self::Slow => f.write_str("Slow"),
            Self::Slower => f.write_str("Slower"),
            Self::VerySlow => f.write_str("VerySlow"),
        }
    }
}

/// Watermark position on the video
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum WatermarkPosition {
    /// Top left corner
    TopLeft,
    /// Top right corner
    TopRight,
    /// Bottom left corner
    BottomLeft,
    /// Bottom right corner
    BottomRight,
    /// Center
    Center,
    /// Custom position (x, y coordinates)
    Custom { x: u32, y: u32 },
}

impl WatermarkPosition {
    /// Converts to FFmpeg overlay position
    pub fn to_ffmpeg_position(&self) -> String {
        match self {
            Self::TopLeft => "x=10:y=10".to_string(),
            Self::TopRight => "x=W-w-10:y=10".to_string(),
            Self::BottomLeft => "x=10:y=H-h-10".to_string(),
            Self::BottomRight => "x=W-w-10:y=H-h-10".to_string(),
            Self::Center => "x=(W-w)/2:y=(H-h)/2".to_string(),
            Self::Custom { x, y } => format!("x={}:y={}", x, y),
        }
    }
}

impl fmt::Display for WatermarkPosition {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TopLeft => f.write_str("TopLeft"),
            Self::TopRight => f.write_str("TopRight"),
            Self::BottomLeft => f.write_str("BottomLeft"),
            Self::BottomRight => f.write_str("BottomRight"),
            Self::Center => f.write_str("Center"),
            Self::Custom { x, y } => write!(f, "Custom(x={}, y={})", x, y),
        }
    }
}

/// Video filter options
#[derive(Clone, Debug, PartialEq)]
pub enum FfmpegFilter {
    /// Crop video to specific dimensions
    Crop { width: u32, height: u32, x: u32, y: u32 },
    /// Rotate video by degrees
    Rotate { angle: i32 },
    /// Add watermark image
    Watermark { path: String, position: WatermarkPosition },
    /// Adjust brightness (-1.0 to 1.0)
    Brightness { value: f32 },
    /// Adjust contrast (0.0 to 4.0)
    Contrast { value: f32 },
    /// Adjust saturation (0.0 to 3.0)
    Saturation { value: f32 },
    /// Apply blur effect
    Blur { radius: u32 },
    /// Flip horizontally
    FlipHorizontal,
    /// Flip vertically
    FlipVertical,
    /// Denoise video
    Denoise,
    /// Sharpen video
    Sharpen,
    /// Custom FFmpeg filter string
    Custom { filter: String },
}

impl FfmpegFilter {
    /// Converts filter to FFmpeg filter string
    ///
    /// # Returns
    ///
    /// The FFmpeg filter string
    pub fn to_ffmpeg_string(&self) -> String {
        match self {
            Self::Crop { width, height, x, y } => format!("crop={}:{}:{}:{}", width, height, x, y),
            Self::Rotate { angle } => {
                let radians = (*angle as f64) * std::f64::consts::PI / 180.0;
                format!("rotate={}:ow=rotw({}):oh=roth({})", radians, radians, radians)
            }
            Self::Watermark { path, position } => {
                format!("movie={}[wm];[in][wm]overlay={}", path, position.to_ffmpeg_position())
            }
            Self::Brightness { value } => format!("eq=brightness={}", value),
            Self::Contrast { value } => format!("eq=contrast={}", value),
            Self::Saturation { value } => format!("eq=saturation={}", value),
            Self::Blur { radius } => format!("boxblur={}:{}", radius, radius),
            Self::FlipHorizontal => "hflip".to_string(),
            Self::FlipVertical => "vflip".to_string(),
            Self::Denoise => "hqdn3d".to_string(),
            Self::Sharpen => "unsharp=5:5:1.0:5:5:0.0".to_string(),
            Self::Custom { filter } => filter.clone(),
        }
    }
}

impl fmt::Display for FfmpegFilter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Crop { width, height, x, y } => {
                write!(f, "Crop(width={}, height={}, x={}, y={})", width, height, x, y)
            }
            Self::Rotate { angle } => write!(f, "Rotate(angle={})", angle),
            Self::Watermark { position, .. } => write!(f, "Watermark(position={})", position),
            Self::Brightness { value } => write!(f, "Brightness(value={})", value),
            Self::Contrast { value } => write!(f, "Contrast(value={})", value),
            Self::Saturation { value } => write!(f, "Saturation(value={})", value),
            Self::Blur { radius } => write!(f, "Blur(radius={})", radius),
            Self::FlipHorizontal => f.write_str("FlipHorizontal"),
            Self::FlipVertical => f.write_str("FlipVertical"),
            Self::Denoise => f.write_str("Denoise"),
            Self::Sharpen => f.write_str("Sharpen"),
            Self::Custom { filter } => write!(f, "Custom(filter={})", filter),
        }
    }
}

/// Comprehensive post-processing configuration
#[derive(Clone, Debug, PartialEq)]
pub struct PostProcessConfig {
    /// Video codec to use for encoding
    pub video_codec: Option<VideoCodec>,
    /// Audio codec to use for encoding
    pub audio_codec: Option<AudioCodec>,
    /// Video bitrate (e.g., "2M", "5M")
    pub video_bitrate: Option<String>,
    /// Audio bitrate (e.g., "128k", "192k", "320k")
    pub audio_bitrate: Option<String>,
    /// Target resolution for scaling
    pub resolution: Option<Resolution>,
    /// Target framerate
    pub framerate: Option<u32>,
    /// Encoding preset (quality/speed trade-off)
    pub preset: Option<EncodingPreset>,
    /// Video filters to apply
    pub filters: Vec<FfmpegFilter>,
}

impl PostProcessConfig {
    /// Creates a new post-processing configuration
    ///
    /// # Returns
    ///
    /// An empty PostProcessConfig with all options set to None
    pub fn new() -> Self {
        tracing::debug!("✂️ Created new post-processing configuration");

        Self {
            video_codec: None,
            audio_codec: None,
            video_bitrate: None,
            audio_bitrate: None,
            resolution: None,
            framerate: None,
            preset: None,
            filters: Vec::new(),
        }
    }

    /// Sets the video codec
    ///
    /// # Arguments
    ///
    /// * `codec` - Video codec to use
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_video_codec(mut self, codec: VideoCodec) -> Self {
        self.video_codec = Some(codec);
        self
    }

    /// Sets the audio codec
    ///
    /// # Arguments
    ///
    /// * `codec` - Audio codec to use
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn with_audio_codec(mut self, codec: AudioCodec) -> Self {
        self.audio_codec = Some(codec);
        self
    }

    /// Sets the video bitrate
    pub fn with_video_bitrate(mut self, bitrate: impl Into<String>) -> Self {
        self.video_bitrate = Some(bitrate.into());
        self
    }

    /// Sets the audio bitrate
    pub fn with_audio_bitrate(mut self, bitrate: impl Into<String>) -> Self {
        self.audio_bitrate = Some(bitrate.into());
        self
    }

    /// Sets the target resolution
    pub fn with_resolution(mut self, resolution: Resolution) -> Self {
        self.resolution = Some(resolution);
        self
    }

    /// Sets the target framerate
    pub fn with_framerate(mut self, fps: u32) -> Self {
        self.framerate = Some(fps);
        self
    }

    /// Sets the encoding preset
    pub fn with_preset(mut self, preset: EncodingPreset) -> Self {
        self.preset = Some(preset);
        self
    }

    /// Adds a filter to the processing pipeline
    ///
    /// # Arguments
    ///
    /// * `filter` - FFmpeg filter to add
    ///
    /// # Returns
    ///
    /// Self for method chaining
    pub fn add_filter(mut self, filter: FfmpegFilter) -> Self {
        tracing::debug!(filter = ?filter, "✂️ Adding FFmpeg filter to post-processing config");

        self.filters.push(filter);
        self
    }

    /// Checks if any post-processing is configured
    ///
    /// # Returns
    ///
    /// true if no post-processing options are set, false otherwise
    pub fn is_empty(&self) -> bool {
        self.video_codec.is_none()
            && self.audio_codec.is_none()
            && self.video_bitrate.is_none()
            && self.audio_bitrate.is_none()
            && self.resolution.is_none()
            && self.framerate.is_none()
            && self.preset.is_none()
            && self.filters.is_empty()
    }
}

impl Default for PostProcessConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for PostProcessConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let video = self.video_codec.as_ref().map_or("None".to_string(), |c| c.to_string());
        let audio = self.audio_codec.as_ref().map_or("None".to_string(), |c| c.to_string());
        write!(
            f,
            "PostProcessConfig(video_codec={}, audio_codec={}, filters={})",
            video,
            audio,
            self.filters.len()
        )
    }
}