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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use std::sync::Arc;
use std::time::{Duration, Instant};

use tokio::sync::RwLock;
use tokio::sync::broadcast::error::RecvError;
use tokio::task::JoinHandle;

use super::config::TrackerConfig;
use super::inner::{CompletedDownload, DownloadOutcome, InProgressDownload, StatsInner};
use super::snapshot::{
    ActiveDownloadSnapshot, DownloadOutcomeSnapshot, DownloadSnapshot, DownloadStats, FetchStats, GlobalSnapshot,
    PlaylistStats, PostProcessStats,
};
use crate::download::DownloadPriority;
use crate::events::{DownloadEvent, EventBus};

/// Subscribes to the event bus and maintains running statistics about all download
/// and metadata fetch operations.
///
/// The tracker runs a background task that processes events from the [`EventBus`] and
/// updates its internal counters. Use [`snapshot`](StatisticsTracker::snapshot) to
/// retrieve a point-in-time view of the collected data.
///
/// # Examples
///
/// ```rust,no_run
/// # use yt_dlp::Downloader;
/// # use yt_dlp::client::deps::Libraries;
/// # use std::path::PathBuf;
/// # #[tokio::main]
/// # async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
/// let libraries = Libraries::new(PathBuf::from("libs/yt-dlp"), PathBuf::from("libs/ffmpeg"));
/// let downloader = Downloader::builder(libraries, "output").build().await?;
///
/// // ... perform downloads ...
///
/// let snapshot = downloader.statistics().snapshot().await;
/// println!("Completed: {}", snapshot.downloads.completed);
/// println!("Total bytes: {}", snapshot.downloads.total_bytes);
/// # Ok(())
/// # }
/// ```
pub struct StatisticsTracker {
    inner: Arc<RwLock<StatsInner>>,
    // Kept alive to ensure the background task runs for as long as the tracker lives.
    _task: JoinHandle<()>,
}

impl StatisticsTracker {
    /// Creates a tracker with default configuration and subscribes to `bus`.
    ///
    /// # Arguments
    ///
    /// * `bus` - The event bus to subscribe to.
    ///
    /// # Returns
    ///
    /// A new [`StatisticsTracker`] that is already running its background event loop.
    pub fn new(bus: &EventBus) -> Self {
        Self::with_config(bus, TrackerConfig::default())
    }

    /// Creates a tracker with custom configuration and subscribes to `bus`.
    ///
    /// # Arguments
    ///
    /// * `bus` - The event bus to subscribe to.
    /// * `config` - History-size bounds and other settings.
    ///
    /// # Returns
    ///
    /// A new [`StatisticsTracker`] that is already running its background event loop.
    pub fn with_config(bus: &EventBus, config: TrackerConfig) -> Self {
        tracing::debug!(
            max_history = config.max_download_history,
            "📊 Creating statistics tracker"
        );

        let inner = Arc::new(RwLock::new(StatsInner::new(config)));
        let rx = bus.subscribe();
        let inner_clone = inner.clone();

        let task = tokio::spawn(run_event_loop(inner_clone, rx));

        Self { inner, _task: task }
    }

    /// Returns a point-in-time snapshot of all collected statistics.
    ///
    /// Acquires a read lock on the internal state, computes derived metrics (averages,
    /// rates), and returns a fully-owned [`GlobalSnapshot`]. Subsequent mutations to the
    /// tracker are not reflected in an already-obtained snapshot.
    ///
    /// # Returns
    ///
    /// A [`GlobalSnapshot`] containing aggregate statistics for downloads, fetches,
    /// post-processing, and playlists at the time of the call.
    pub async fn snapshot(&self) -> GlobalSnapshot {
        let inner = self.inner.read().await;
        build_snapshot(&inner)
    }

    /// Returns the number of downloads currently in progress.
    ///
    /// # Returns
    ///
    /// The count of downloads that have been started but have not yet reached a terminal
    /// state (completed, failed, or canceled).
    pub async fn active_count(&self) -> usize {
        self.inner.read().await.in_progress.len()
    }

    /// Returns the total number of successfully completed downloads.
    ///
    /// # Returns
    ///
    /// Cumulative count of downloads that ended with a [`DownloadCompleted`](crate::events::DownloadEvent::DownloadCompleted) event.
    pub async fn completed_count(&self) -> u64 {
        self.inner.read().await.completed
    }

    /// Returns the total number of bytes transferred across all completed downloads.
    ///
    /// # Returns
    ///
    /// Sum of `total_bytes` from every [`DownloadCompleted`](crate::events::DownloadEvent::DownloadCompleted) event received.
    pub async fn total_bytes(&self) -> u64 {
        self.inner.read().await.total_bytes
    }

    /// Resets all counters and history to their initial state, preserving the configuration.
    ///
    /// This is useful for implementing rolling windows or clearing statistics between
    /// logical phases of an application. The tracker continues running and will start
    /// collecting fresh data immediately after the reset.
    pub async fn reset(&self) {
        tracing::debug!("📊 Resetting statistics tracker");

        let mut inner = self.inner.write().await;
        let config = inner.config;
        *inner = StatsInner::new(config);
    }
}

impl std::fmt::Debug for StatisticsTracker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StatisticsTracker").finish_non_exhaustive()
    }
}

impl std::fmt::Display for StatisticsTracker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("StatisticsTracker")
    }
}

/// Background loop: receives events from the broadcast channel and updates `inner`.
async fn run_event_loop(inner: Arc<RwLock<StatsInner>>, mut rx: tokio::sync::broadcast::Receiver<Arc<DownloadEvent>>) {
    loop {
        match rx.recv().await {
            Ok(event) => {
                let mut state = inner.write().await;
                handle_event(&mut state, &event);
            }
            Err(RecvError::Lagged(missed)) => {
                tracing::warn!(missed = missed, "Statistics tracker lagged, some events were missed");
            }
            Err(RecvError::Closed) => break,
        }
    }

    tracing::debug!("📊 Statistics tracker event loop terminated");
}

/// Resolved fields extracted from an in-progress download record.
///
/// Produced by [`resolve_in_progress_record`] to avoid repeating the same
/// `match record { Some(r) => ..., None => ... }` pattern in every terminal arm.
struct ResolvedRecord {
    url: String,
    priority: DownloadPriority,
    queue_wait: Option<Duration>,
    peak_speed: f64,
    elapsed: Option<Duration>,
}

/// Extracts timing and identity fields from an optional in-progress record.
///
/// Returns a fully-populated [`ResolvedRecord`] with zero-valued defaults when
/// `record` is `None` (i.e. the download was not tracked — e.g. it was queued
/// before the tracker started).
///
/// # Arguments
///
/// * `record` - The removed in-progress entry, or `None` if not found.
///
/// # Returns
///
/// A [`ResolvedRecord`] with `url`, `priority`, `queue_wait`, `peak_speed`,
/// and `elapsed` populated from the record, or defaulted to empty/`None`/`0.0`.
fn resolve_in_progress_record(record: Option<InProgressDownload>) -> ResolvedRecord {
    match record {
        Some(r) => ResolvedRecord {
            queue_wait: r.started_at.map(|s| s.duration_since(r.queued_at)),
            elapsed: r.started_at.map(|s| s.elapsed()),
            peak_speed: r.peak_speed,
            url: r.url,
            priority: r.priority,
        },
        None => ResolvedRecord {
            url: String::new(),
            priority: DownloadPriority::Normal,
            queue_wait: None,
            peak_speed: 0.0,
            elapsed: None,
        },
    }
}

/// Applies a single event to the mutable state. No I/O or `.await` inside.
fn handle_event(state: &mut StatsInner, event: &DownloadEvent) {
    match event {
        DownloadEvent::DownloadQueued {
            download_id,
            url,
            priority,
            ..
        } => {
            state.attempted += 1;
            state.queued += 1;
            state.in_progress.insert(
                *download_id,
                InProgressDownload {
                    url: url.clone(),
                    priority: *priority,
                    queued_at: Instant::now(),
                    started_at: None,
                    peak_speed: 0.0,
                    downloaded_bytes: 0,
                    total_bytes: 0,
                },
            );

            tracing::debug!(download_id = download_id, url = url, "📊 Download queued");
        }

        DownloadEvent::DownloadStarted {
            download_id,
            total_bytes,
            ..
        } => {
            state.queued = state.queued.saturating_sub(1);

            if let Some(entry) = state.in_progress.get_mut(download_id) {
                entry.started_at = Some(Instant::now());
                entry.total_bytes = *total_bytes;
            }

            tracing::debug!(
                download_id = download_id,
                total_bytes = total_bytes,
                "📊 Download started"
            );
        }

        DownloadEvent::DownloadProgress {
            download_id,
            downloaded_bytes,
            total_bytes,
            speed_bytes_per_sec,
            ..
        } => {
            if let Some(entry) = state.in_progress.get_mut(download_id) {
                entry.downloaded_bytes = *downloaded_bytes;
                entry.total_bytes = *total_bytes;
                if *speed_bytes_per_sec > entry.peak_speed {
                    entry.peak_speed = *speed_bytes_per_sec;
                }
            }
        }

        DownloadEvent::DownloadCompleted {
            download_id,
            duration,
            total_bytes,
            ..
        } => {
            state.completed += 1;
            state.total_bytes += total_bytes;
            state.total_download_duration += *duration;

            let rec = resolve_in_progress_record(state.in_progress.remove(download_id));

            state.push_history(CompletedDownload {
                download_id: *download_id,
                url: rec.url,
                priority: rec.priority,
                outcome: DownloadOutcome::Completed,
                bytes: *total_bytes,
                duration: Some(*duration),
                queue_wait: rec.queue_wait,
                peak_speed: rec.peak_speed,
                retry_count: 0,
            });

            tracing::debug!(
                download_id = download_id,
                total_bytes = total_bytes,
                duration = ?duration,
                "📊 Download completed"
            );
        }

        DownloadEvent::DownloadFailed {
            download_id,
            retry_count,
            ..
        } => {
            state.failed += 1;
            state.total_retries += *retry_count as u64;

            let rec = resolve_in_progress_record(state.in_progress.remove(download_id));

            state.push_history(CompletedDownload {
                download_id: *download_id,
                url: rec.url,
                priority: rec.priority,
                outcome: DownloadOutcome::Failed,
                bytes: 0,
                duration: rec.elapsed,
                queue_wait: rec.queue_wait,
                peak_speed: rec.peak_speed,
                retry_count: *retry_count,
            });

            tracing::debug!(
                download_id = download_id,
                retry_count = retry_count,
                "📊 Download failed"
            );
        }

        DownloadEvent::DownloadCanceled { download_id, .. } => {
            state.canceled += 1;
            if state.queued > 0 {
                state.queued -= 1;
            }

            let rec = resolve_in_progress_record(state.in_progress.remove(download_id));

            state.push_history(CompletedDownload {
                download_id: *download_id,
                url: rec.url,
                priority: rec.priority,
                outcome: DownloadOutcome::Canceled,
                bytes: 0,
                duration: None,
                queue_wait: rec.queue_wait,
                peak_speed: 0.0,
                retry_count: 0,
            });

            tracing::debug!(download_id = download_id, "📊 Download canceled");
        }

        DownloadEvent::VideoFetched { url, duration, .. } => {
            state.fetch_attempted += 1;
            state.fetch_succeeded += 1;
            state.total_fetch_duration += *duration;

            tracing::debug!(
                url = url,
                duration = ?duration,
                "📊 Video fetched"
            );
        }

        DownloadEvent::VideoFetchFailed { url, duration, .. } => {
            state.fetch_attempted += 1;
            state.fetch_failed += 1;

            tracing::debug!(
                url = url,
                duration = ?duration,
                "📊 Video fetch failed"
            );
        }

        DownloadEvent::PlaylistFetched {
            url,
            duration,
            playlist,
        } => {
            state.fetch_attempted += 1;
            state.fetch_succeeded += 1;
            state.total_fetch_duration += *duration;
            state.playlists_fetched += 1;

            tracing::debug!(
                url = url,
                duration = ?duration,
                playlist_id = %playlist.id,
                "📊 Playlist fetched"
            );
        }

        DownloadEvent::PlaylistFetchFailed { url, duration, .. } => {
            state.fetch_attempted += 1;
            state.fetch_failed += 1;
            state.playlist_fetch_failed += 1;

            tracing::debug!(
                url = url,
                duration = ?duration,
                "📊 Playlist fetch failed"
            );
        }

        DownloadEvent::PlaylistCompleted { successful, failed, .. } => {
            state.playlist_items_successful += *successful as u64;
            state.playlist_items_failed += *failed as u64;

            tracing::debug!(successful = successful, failed = failed, "📊 Playlist completed");
        }

        DownloadEvent::PostProcessStarted { operation, .. } => {
            state.postprocess_attempted += 1;

            tracing::debug!(
                operation = ?operation,
                "📊 Post-process started"
            );
        }

        DownloadEvent::PostProcessCompleted {
            operation, duration, ..
        } => {
            state.postprocess_succeeded += 1;
            state.total_postprocess_duration += *duration;

            tracing::debug!(
                operation = ?operation,
                duration = ?duration,
                "📊 Post-process completed"
            );
        }

        DownloadEvent::PostProcessFailed { operation, error, .. } => {
            state.postprocess_failed += 1;

            tracing::debug!(
                operation = ?operation,
                error = error,
                "📊 Post-process failed"
            );
        }

        DownloadEvent::SegmentStarted { .. }
        | DownloadEvent::SegmentCompleted { .. }
        | DownloadEvent::FormatSelected { .. }
        | DownloadEvent::MetadataApplied { .. }
        | DownloadEvent::ChaptersEmbedded { .. }
        | DownloadEvent::DownloadPaused { .. }
        | DownloadEvent::DownloadResumed { .. }
        | DownloadEvent::PlaylistItemStarted { .. }
        | DownloadEvent::PlaylistItemCompleted { .. }
        | DownloadEvent::PlaylistItemFailed { .. } => {
            tracing::debug!(event = ?event, "📊 Untracked event, ignoring");
        }

        #[cfg(feature = "live-recording")]
        DownloadEvent::LiveRecordingStarted { .. }
        | DownloadEvent::LiveRecordingProgress { .. }
        | DownloadEvent::LiveRecordingStopped { .. }
        | DownloadEvent::LiveRecordingFailed { .. } => {
            tracing::debug!(event = ?event, "📊 Live recording event, ignoring in stats");
        }
        #[cfg(feature = "live-streaming")]
        DownloadEvent::LiveStreamStarted { .. }
        | DownloadEvent::LiveStreamProgress { .. }
        | DownloadEvent::LiveStreamStopped { .. }
        | DownloadEvent::LiveStreamFailed { .. } => {
            tracing::debug!(event = ?event, "📊 Live stream event, ignoring in stats");
        }
    }
}

/// Constructs a [`GlobalSnapshot`] from the current state. Called under a read lock.
fn build_snapshot(state: &StatsInner) -> GlobalSnapshot {
    let now = Instant::now();
    let terminal = state.completed + state.failed + state.canceled;
    let download_success_rate = if terminal > 0 {
        Some(state.completed as f64 / terminal as f64)
    } else {
        None
    };

    let fetch_success_rate = if state.fetch_attempted > 0 {
        Some(state.fetch_succeeded as f64 / state.fetch_attempted as f64)
    } else {
        None
    };

    let postprocess_success_rate = if state.postprocess_attempted > 0 {
        Some(state.postprocess_succeeded as f64 / state.postprocess_attempted as f64)
    } else {
        None
    };

    let postprocess_avg_duration = if state.postprocess_succeeded > 0 {
        Some(Duration::from_secs_f64(
            state.total_postprocess_duration.as_secs_f64() / state.postprocess_succeeded as f64,
        ))
    } else {
        None
    };

    let playlist_items_total = state.playlist_items_successful + state.playlist_items_failed;
    let item_success_rate = if playlist_items_total > 0 {
        Some(state.playlist_items_successful as f64 / playlist_items_total as f64)
    } else {
        None
    };

    let mut active_downloads: Vec<ActiveDownloadSnapshot> = state
        .in_progress
        .iter()
        .map(|(id, r)| {
            let progress = if r.total_bytes > 0 {
                Some(r.downloaded_bytes as f64 / r.total_bytes as f64)
            } else {
                None
            };
            ActiveDownloadSnapshot {
                download_id: *id,
                url: r.url.clone(),
                priority: r.priority,
                downloaded_bytes: r.downloaded_bytes,
                total_bytes: r.total_bytes,
                progress,
                peak_speed_bytes_per_sec: r.peak_speed,
                elapsed: r.started_at.map(|s| now.duration_since(s)),
                time_since_queued: now.duration_since(r.queued_at),
            }
        })
        .collect();
    active_downloads.sort_by_key(|e| e.download_id);

    let recent_downloads: Vec<DownloadSnapshot> = state
        .history
        .iter()
        .map(|r| DownloadSnapshot {
            download_id: r.download_id,
            url: r.url.clone(),
            priority: r.priority,
            outcome: match r.outcome {
                DownloadOutcome::Completed => DownloadOutcomeSnapshot::Completed,
                DownloadOutcome::Failed => DownloadOutcomeSnapshot::Failed,
                DownloadOutcome::Canceled => DownloadOutcomeSnapshot::Canceled,
            },
            bytes: r.bytes,
            duration: r.duration,
            queue_wait: r.queue_wait,
            peak_speed_bytes_per_sec: r.peak_speed,
            retry_count: r.retry_count,
        })
        .collect();

    GlobalSnapshot {
        downloads: DownloadStats {
            attempted: state.attempted,
            completed: state.completed,
            failed: state.failed,
            canceled: state.canceled,
            queued: state.queued,
            total_bytes: state.total_bytes,
            total_retries: state.total_retries,
            total_duration: state.total_download_duration,
            avg_duration: state.avg_download_duration(),
            avg_speed_bytes_per_sec: state.avg_speed_bytes_per_sec(),
            peak_speed_bytes_per_sec: state.peak_speed_bytes_per_sec(),
            success_rate: download_success_rate,
        },
        fetches: FetchStats {
            attempted: state.fetch_attempted,
            succeeded: state.fetch_succeeded,
            failed: state.fetch_failed,
            avg_duration: state.avg_fetch_duration(),
            success_rate: fetch_success_rate,
        },
        post_processing: PostProcessStats {
            attempted: state.postprocess_attempted,
            succeeded: state.postprocess_succeeded,
            failed: state.postprocess_failed,
            avg_duration: postprocess_avg_duration,
            success_rate: postprocess_success_rate,
        },
        playlists: PlaylistStats {
            playlists_fetched: state.playlists_fetched,
            playlists_fetch_failed: state.playlist_fetch_failed,
            items_successful: state.playlist_items_successful,
            items_failed: state.playlist_items_failed,
            item_success_rate,
        },
        active_count: active_downloads.len(),
        active_downloads,
        recent_downloads,
    }
}