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
//! Builder pattern for Downloader struct.
//!
//! This module provides a fluent API for constructing Downloader instances with various configurations.

use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

#[cfg(cache)]
use crate::cache::{CacheConfig, CacheLayer};
use crate::client::proxy::ProxyConfig;
use crate::client::{Downloader, Libraries};
use crate::download::config::speed_profile::SpeedProfile;
use crate::download::manager::{DownloadManager, ManagerConfig};
use crate::error::Result;
use crate::extractor::ExtractorConfig;
#[cfg(cache)]
use crate::utils::fs;

/// Builder for creating Downloader instances with a fluent API.
///
/// # Examples
///
/// ```rust,no_run
/// # use yt_dlp::DownloaderBuilder;
/// # use yt_dlp::client::deps::Libraries;
/// # use std::path::PathBuf;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let libraries = Libraries::new(PathBuf::from("libs/yt-dlp"), PathBuf::from("libs/ffmpeg"));
///
/// let downloader = DownloaderBuilder::new(libraries, PathBuf::from("output"))
///     .with_args(vec!["--no-playlist".to_string()])
///     .with_timeout(std::time::Duration::from_secs(120))
///     .build()
///     .await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct DownloaderBuilder {
    libraries: Libraries,
    output_dir: PathBuf,
    args: Vec<String>,
    user_agent: Option<String>,
    timeout: Duration,
    proxy: Option<ProxyConfig>,
    cookies: Option<PathBuf>,
    cookies_from_browser: Option<String>,
    use_netrc: bool,
    #[cfg(cache)]
    cache_config: Option<CacheConfig>,
    download_manager_config: Option<ManagerConfig>,
}

impl DownloaderBuilder {
    /// Create a new builder with required parameters.
    ///
    /// # Arguments
    ///
    /// * `libraries` - The required libraries (yt-dlp and ffmpeg paths)
    /// * `output_dir` - The directory where videos will be downloaded
    pub fn new(libraries: Libraries, output_dir: impl Into<PathBuf>) -> Self {
        let output_dir = output_dir.into();

        tracing::debug!(
            output_dir = ?output_dir,
            timeout = ?crate::client::DEFAULT_TIMEOUT,
            "🔧 Creating new DownloaderBuilder"
        );

        Self {
            libraries,
            output_dir,
            args: Vec::new(),
            user_agent: None,
            timeout: crate::client::DEFAULT_TIMEOUT,
            proxy: None,
            cookies: None,
            cookies_from_browser: None,
            use_netrc: false,
            #[cfg(cache)]
            cache_config: None,
            download_manager_config: None,
        }
    }

    /// Set custom arguments to pass to yt-dlp.
    ///
    /// # Arguments
    ///
    /// * `args` - The arguments to pass to yt-dlp
    pub fn with_args(mut self, args: Vec<String>) -> Self {
        tracing::debug!(
            args = ?args,
            arg_count = args.len(),
            "🔧 Setting custom yt-dlp arguments"
        );

        self.args = args;
        self
    }

    /// Add a single argument to pass to yt-dlp.
    ///
    /// # Arguments
    ///
    /// * `arg` - The argument to add
    pub fn add_arg(mut self, arg: impl Into<String>) -> Self {
        self.args.push(arg.into());
        self
    }

    /// Set the timeout for command execution.
    ///
    /// # Arguments
    ///
    /// * `timeout` - The timeout duration
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        tracing::debug!(
            timeout = ?timeout,
            "🔧 Setting command execution timeout"
        );

        self.timeout = timeout;
        self
    }

    /// Set proxy configuration for HTTP requests and yt-dlp.
    ///
    /// # Arguments
    ///
    /// * `proxy` - The proxy configuration
    pub fn with_proxy(mut self, proxy: ProxyConfig) -> Self {
        tracing::debug!(
            proxy_type = ?proxy.proxy_type(),
            proxy_url = proxy.url(),
            has_auth = proxy.username().is_some(),
            "🔧 Setting proxy configuration"
        );

        self.proxy = Some(proxy);
        self
    }

    /// Use a Netscape cookie file for authentication.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the Netscape cookie file
    pub fn with_cookies(mut self, path: impl Into<PathBuf>) -> Self {
        let path = path.into();
        tracing::debug!(cookies_path = ?path, "🔧 Setting cookie file");

        self.cookies = Some(path);
        self
    }

    /// Extract cookies from a browser for authentication.
    ///
    /// # Arguments
    ///
    /// * `browser` - Browser name (e.g. `"chrome"`, `"firefox"`)
    pub fn with_cookies_from_browser(mut self, browser: impl Into<String>) -> Self {
        let browser = browser.into();
        tracing::debug!(browser = %browser, "🔧 Setting cookies from browser");

        self.cookies_from_browser = Some(browser);
        self
    }

    /// Use .netrc for authentication.
    pub fn with_netrc(mut self) -> Self {
        tracing::debug!("🔧 Enabling .netrc authentication");

        self.use_netrc = true;
        self
    }

    /// Enable caching with the specified cache directory.
    ///
    /// # Arguments
    ///
    /// * `cache_dir` - The directory to store cache files
    #[cfg(cache)]
    pub fn with_cache(mut self, cache_dir: impl Into<PathBuf>) -> Self {
        let cache_dir = cache_dir.into();

        tracing::debug!(
            cache_dir = ?cache_dir,
            "🔧 Enabling cache with directory"
        );

        self.cache_config = Some(CacheConfig::builder().cache_dir(cache_dir).build());
        self
    }

    /// Enable caching with a full configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The cache configuration (directory, TTLs, optional Redis URL)
    #[cfg(cache)]
    pub fn with_cache_config(mut self, config: CacheConfig) -> Self {
        tracing::debug!(
            config = %config,
            "🔧 Enabling cache with config"
        );

        self.cache_config = Some(config);
        self
    }

    /// Set the download manager configuration.
    ///
    /// # Arguments
    ///
    /// * `config` - The download manager configuration
    pub fn with_download_manager_config(mut self, config: ManagerConfig) -> Self {
        self.download_manager_config = Some(config);
        self
    }

    /// Set the maximum number of concurrent downloads.
    ///
    /// # Arguments
    ///
    /// * `max_concurrent` - Maximum number of concurrent downloads
    pub fn with_max_concurrent_downloads(mut self, max_concurrent: usize) -> Self {
        let mut config = self.download_manager_config.take().unwrap_or_default();
        config.max_concurrent_downloads = max_concurrent;
        self.download_manager_config = Some(config);
        self
    }

    /// Set the speed profile for download optimization.
    ///
    /// This automatically configures all download parameters (concurrent downloads,
    /// parallel segments, segment size, buffer size) based on the selected profile.
    ///
    /// # Arguments
    ///
    /// * `profile` - The speed profile to use (Conservative, Balanced, or Aggressive)
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use yt_dlp::DownloaderBuilder;
    /// # use yt_dlp::client::deps::Libraries;
    /// # use yt_dlp::download::SpeedProfile;
    /// # use std::path::PathBuf;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let libraries = Libraries::new(PathBuf::from("libs/yt-dlp"), PathBuf::from("libs/ffmpeg"));
    ///
    /// // Use aggressive profile for high-speed connections
    /// let downloader = DownloaderBuilder::new(libraries, PathBuf::from("output"))
    ///     .with_speed_profile(SpeedProfile::Aggressive)
    ///     .build()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_speed_profile(mut self, profile: SpeedProfile) -> Self {
        tracing::debug!(
            profile = ?profile,
            max_concurrent = profile.max_concurrent_downloads(),
            segment_size = profile.segment_size(),
            parallel_segments = profile.parallel_segments(),
            max_buffer_size = profile.max_buffer_size(),
            "🔧 Setting speed profile"
        );

        if let Some(config) = &mut self.download_manager_config {
            config.max_concurrent_downloads = profile.max_concurrent_downloads();
            config.segment_size = profile.segment_size();
            config.parallel_segments = profile.parallel_segments();
            config.max_buffer_size = profile.max_buffer_size();
            config.speed_profile = profile;
        } else {
            self.download_manager_config = Some(ManagerConfig::from_speed_profile(profile));
        }
        self
    }

    /// Set a custom User-Agent header for HTTP requests.
    ///
    /// # Arguments
    ///
    /// * `user_agent` - The User-Agent string to use
    ///
    /// # Returns
    ///
    /// The builder with the user agent configured.
    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
        let ua = user_agent.into();
        tracing::debug!(user_agent = ua, "🔧 Setting user agent");
        self.user_agent = Some(ua);
        self
    }

    /// Build the Downloader instance.
    ///
    /// This method is async because it may need to create cache directories
    /// and initialize the download manager.
    ///
    /// # Returns
    ///
    /// A configured Downloader instance ready to use.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The output directory cannot be created
    /// - The cache directories cannot be created (if caching is enabled)
    /// - The download manager cannot be initialized
    pub async fn build(self) -> Result<Downloader> {
        {
            #[cfg(cache)]
            tracing::debug!(
                output_dir = ?self.output_dir,
                args_count = self.args.len(),
                timeout = ?self.timeout,
                has_proxy = self.proxy.is_some(),
                has_cache = self.cache_config.is_some(),
                "🔧 Building Downloader instance"
            );

            #[cfg(not(cache))]
            tracing::debug!(
                output_dir = ?self.output_dir,
                args_count = self.args.len(),
                timeout = ?self.timeout,
                has_proxy = self.proxy.is_some(),
                "🔧 Building Downloader instance"
            );
        }

        // Create output directory if it doesn't exist
        if !self.output_dir.exists() {
            tokio::fs::create_dir_all(&self.output_dir).await?;
        }

        // Create event bus first
        let event_bus = crate::events::EventBus::with_default_capacity();

        // Create download manager with proxy configuration and event bus
        let download_manager = if let Some(mut config) = self.download_manager_config {
            config.proxy = self.proxy.clone();
            Arc::new(DownloadManager::with_config_and_event_bus(
                config,
                Some(event_bus.clone()),
            ))
        } else {
            let config = ManagerConfig {
                proxy: self.proxy.clone(),
                ..Default::default()
            };
            Arc::new(DownloadManager::with_config_and_event_bus(
                config,
                Some(event_bus.clone()),
            ))
        };

        // Add proxy argument to yt-dlp args if configured
        let mut args = self.args;
        if let Some(ref proxy) = self.proxy {
            args.push("--proxy".to_string());
            args.push(proxy.to_ytdlp_arg());
        }

        // Create extractors (must be mut so cookie args can be pushed)
        let mut youtube_extractor = crate::extractor::Youtube::new(self.libraries.youtube.clone());
        let mut generic_extractor = crate::extractor::Generic::new(self.libraries.youtube.clone());

        // Propagate cookie configuration to both extractors and raw args
        if let Some(ref path) = self.cookies {
            youtube_extractor.with_cookies(path);
            generic_extractor.with_cookies(path);
            args.push(format!("--cookies={}", path.display()));
        }
        if let Some(ref browser) = self.cookies_from_browser {
            youtube_extractor.with_cookies_from_browser(browser);
            generic_extractor.with_cookies_from_browser(browser);
            args.push(format!("--cookies-from-browser={}", browser));
        }
        if self.use_netrc {
            youtube_extractor.with_netrc();
            generic_extractor.with_netrc();
            args.push("--netrc".to_string());
        }

        // Create cache layer if configured
        #[cfg(cache)]
        let cache = if let Some(config) = self.cache_config {
            // Ensure cache directory exists
            if !config.cache_dir.exists() {
                fs::create_dir(&config.cache_dir).await?;
            }
            Some(Arc::new(CacheLayer::from_config(&config).await?))
        } else {
            None
        };

        #[cfg(feature = "statistics")]
        let statistics = Arc::new(crate::stats::StatisticsTracker::new(&event_bus));

        Ok(Downloader {
            youtube_extractor,
            generic_extractor,
            libraries: self.libraries,
            output_dir: self.output_dir,
            args,
            user_agent: self.user_agent,
            timeout: self.timeout,
            proxy: self.proxy,
            #[cfg(cache)]
            cache,
            download_manager,
            cancellation_token: tokio_util::sync::CancellationToken::new(),
            event_bus,
            #[cfg(feature = "hooks")]
            hook_registry: Some(crate::events::HookRegistry::new()),
            #[cfg(feature = "webhooks")]
            webhook_delivery: Some(crate::events::WebhookDelivery::new()),
            #[cfg(feature = "statistics")]
            statistics,
        })
    }
}

impl std::fmt::Display for DownloaderBuilder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "DownloaderBuilder(output_dir={}, timeout={}s, proxy={})",
            self.output_dir.display(),
            self.timeout.as_secs(),
            self.proxy.is_some()
        )
    }
}