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
//! The fetchers for required dependencies.

use std::fmt;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::PathBuf;

use derive_more::Constructor;
use serde::Deserialize;
use sha2::{Digest, Sha256};

use crate::client::deps::ffmpeg::BuildFetcher;
use crate::client::deps::ytdlp::YoutubeFetcher;
use crate::download::Fetcher;
use crate::error::Result;
use crate::utils::fs;
use crate::{ternary, utils};

pub mod ffmpeg;
pub mod github;
pub mod ytdlp;

/// Installs required libraries.
///
/// # Examples
///
/// ```rust,no_run
/// # use yt_dlp::client::deps::LibraryInstaller;
/// # use std::path::PathBuf;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let destination = PathBuf::from("libs");
/// let installer = LibraryInstaller::new(destination);
///
/// let youtube = installer.install_youtube(None).await.unwrap();
/// let ffmpeg = installer.install_ffmpeg(None).await.unwrap();
/// # Ok(())
/// # }
/// ```
#[derive(Constructor, Clone, Debug)]
pub struct LibraryInstaller {
    /// The destination directory for the libraries.
    pub destination: PathBuf,
}

impl fmt::Display for LibraryInstaller {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "LibraryInstaller(destination={})", self.destination.display())
    }
}

/// The installed libraries.
///
/// # Examples
///
/// ```rust,no_run
/// # use yt_dlp::client::deps::Libraries;
/// # use std::path::PathBuf;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let destination = PathBuf::from("libs");
///
/// let youtube = destination.join("yt-dlp");
/// let ffmpeg = destination.join("ffmpeg");
///
/// let libraries = Libraries::new(youtube, ffmpeg);
/// # Ok(())
/// # }
/// ```
#[derive(Constructor, Clone, Debug)]
pub struct Libraries {
    /// The path to the installed yt-dlp binary.
    pub youtube: PathBuf,
    /// The path to the installed ffmpeg binary.
    pub ffmpeg: PathBuf,
}

impl fmt::Display for Libraries {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Libraries(youtube={}, ffmpeg={})",
            self.youtube.display(),
            self.ffmpeg.display()
        )
    }
}

impl LibraryInstaller {
    /// Install yt-dlp from the main repository.
    ///
    /// # Arguments
    ///
    /// * `custom_name` - Optional custom name for the executable.
    pub async fn install_youtube(&self, custom_name: Option<String>) -> Result<PathBuf> {
        self.install_youtube_from_repo("yt-dlp", "yt-dlp", None, custom_name)
            .await
    }

    /// Install yt-dlp from a custom repository, assuming releases assets are named correctly.
    ///
    /// # Arguments
    ///
    /// * `owner` - The owner of the repository.
    /// * `repo` - The name of the repository.
    /// * `auth_token` - Optional GitHub token to avoid rate limits.
    /// * `custom_name` - Optional custom name for the executable.
    pub async fn install_youtube_from_repo(
        &self,
        owner: impl Into<String>,
        repo: impl Into<String>,
        auth_token: Option<String>,
        custom_name: Option<String>,
    ) -> Result<PathBuf> {
        let owner: String = owner.into();
        let repo: String = repo.into();

        tracing::debug!(
            owner = %owner,
            repo = %repo,
            custom_name = ?custom_name,
            destination = ?self.destination,
            "📦 Installing yt-dlp from repository"
        );

        fs::create_dir(self.destination.clone()).await?;

        let fetcher = YoutubeFetcher::new(owner, repo);

        let name = custom_name.unwrap_or(String::from("yt-dlp"));
        let path = self.destination.join(utils::find_executable(&name));

        let release = fetcher.fetch_release(auth_token).await?;
        release.download(path.clone()).await?;
        fs::set_executable(path.clone()).await?;

        Ok(path)
    }

    /// Install ffmpeg from static builds.
    ///
    /// # Arguments
    ///
    /// * `custom_name` - Optional custom name for the executable.
    pub async fn install_ffmpeg(&self, custom_name: Option<String>) -> Result<PathBuf> {
        tracing::debug!(
            custom_name = ?custom_name,
            destination = ?self.destination,
            "📦 Installing ffmpeg from static builds"
        );

        fs::create_dir(self.destination.clone()).await?;

        let fetcher = BuildFetcher::new();
        let archive = self.destination.join("ffmpeg-release.zip");

        let release = fetcher.fetch_binary().await?;
        release.download(archive.clone()).await?;
        let path = fetcher.extract_binary(archive).await?;

        if let Some(name) = custom_name {
            let new_path = self.destination.join(utils::find_executable(&name));
            tokio::fs::rename(&path, &new_path).await?;

            return Ok(new_path);
        }

        Ok(path)
    }
}

impl Libraries {
    /// Install the required dependencies.
    pub async fn install_dependencies(&self) -> Result<Self> {
        tracing::info!(
            youtube_path = ?self.youtube,
            ffmpeg_path = ?self.ffmpeg,
            "📦 Installing required dependencies"
        );

        let (youtube, ffmpeg) = tokio::join!(self.install_youtube(), self.install_ffmpeg());

        Ok(Self::new(youtube?, ffmpeg?))
    }

    /// Install the required dependencies with an authentication token.
    ///
    /// # Arguments
    ///
    /// * `auth_token` - The authentication token to use for downloading the dependencies.
    pub async fn install_dependencies_with_token(&self, auth_token: impl Into<String>) -> Result<Self> {
        tracing::info!(
            youtube_path = ?self.youtube,
            ffmpeg_path = ?self.ffmpeg,
            has_token = true,
            "📦 Installing required dependencies with authentication token"
        );

        let token = auth_token.into();
        let youtube = self.install_youtube_with_token(token.clone()).await?;
        let ffmpeg = self.install_ffmpeg_with_token(token).await?;

        Ok(Self::new(youtube, ffmpeg))
    }

    /// Install yt-dlp.
    pub async fn install_youtube(&self) -> Result<PathBuf> {
        self.install_youtube_internal(None).await
    }

    /// Install yt-dlp with an authentication token.
    pub async fn install_youtube_with_token(&self, auth_token: impl Into<String>) -> Result<PathBuf> {
        self.install_youtube_internal(Some(auth_token.into())).await
    }

    async fn install_youtube_internal(&self, auth_token: Option<String>) -> Result<PathBuf> {
        tracing::debug!(
            youtube_path = ?self.youtube,
            has_token = auth_token.is_some(),
            "📦 Installing yt-dlp binary"
        );

        let parent = fs::try_parent(self.youtube.clone())?;
        let installer = LibraryInstaller::new(parent);

        if self.youtube.exists() {
            return Ok(self.youtube.clone());
        }

        let name = utils::find_executable("yt-dlp");
        let file_name = fs::try_name(self.youtube.clone())?;

        let custom_name = ternary!(file_name == name, None, Some(file_name));
        installer
            .install_youtube_from_repo("yt-dlp", "yt-dlp", auth_token, custom_name)
            .await
    }

    /// Install ffmpeg.
    pub async fn install_ffmpeg(&self) -> Result<PathBuf> {
        self.install_ffmpeg_internal(None).await
    }

    /// Install ffmpeg with an authentication token.
    pub async fn install_ffmpeg_with_token(&self, auth_token: impl Into<String>) -> Result<PathBuf> {
        self.install_ffmpeg_internal(Some(auth_token.into())).await
    }

    async fn install_ffmpeg_internal(&self, _auth_token: Option<String>) -> Result<PathBuf> {
        tracing::debug!(
            ffmpeg_path = ?self.ffmpeg,
            "📦 Installing ffmpeg binary"
        );

        let parent = fs::try_parent(self.ffmpeg.clone())?;
        let installer = LibraryInstaller::new(parent);

        if self.ffmpeg.exists() {
            return Ok(self.ffmpeg.clone());
        }

        let name = utils::find_executable("ffmpeg");
        let file_name = fs::try_name(self.ffmpeg.clone())?;

        let custom_name = ternary!(file_name == name, None, Some(file_name));
        installer.install_ffmpeg(custom_name).await
    }
}

/// A GitHub release.
#[derive(Debug, Deserialize)]
pub struct Release {
    /// The tag name of the release.
    pub tag_name: String,
    /// The assets of the release.
    pub assets: Vec<Asset>,
}

impl fmt::Display for Release {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Release(tag={}, assets={})", self.tag_name, self.assets.len())
    }
}

/// A release asset.
#[derive(Debug, Deserialize)]
pub struct Asset {
    /// The name of the asset.
    pub name: String,
    /// The download URL of the asset.
    #[serde(rename = "browser_download_url")]
    pub download_url: String,
    /// The digest of the asset (if available via API, e.g. sha256:...).
    pub digest: Option<String>,
}

impl fmt::Display for Asset {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Asset(name={}, url={})", self.name, self.download_url)
    }
}

/// A release that has been selected for the current platform.
#[derive(Debug)]
pub struct WantedRelease {
    /// The URL of the release asset.
    pub url: String,
    /// The name of the release asset.
    pub name: String,
    /// The expected SHA256 checksum of the asset.
    pub checksum: Option<String>,
}

impl fmt::Display for WantedRelease {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "WantedRelease(asset={}, url={}, checksum={})",
            self.name,
            self.url,
            self.checksum.as_deref().unwrap_or("none")
        )
    }
}

impl WantedRelease {
    /// Download the release asset to the given destination.
    ///
    /// # Arguments
    ///
    /// * `destination` - The path to write the asset to.
    ///
    /// # Errors
    ///
    /// This function will return an error if the asset could not be downloaded, written to the destination,
    /// or if the checksum verification fails.
    pub async fn download(&self, destination: impl Into<PathBuf>) -> Result<()> {
        let destination: PathBuf = destination.into();
        tracing::debug!(
            url = %self.url,
            destination = ?destination,
            asset_name = %self.name,
            has_checksum = self.checksum.is_some(),
            "📦 Downloading release asset"
        );

        let fetcher = Fetcher::new(&self.url, None, None)?;
        fetcher.fetch_asset(destination.clone()).await?;

        if let Some(expected_checksum) = &self.checksum {
            tracing::debug!(
                destination = ?destination,
                expected_checksum = %expected_checksum,
                "⚙️ Verifying asset checksum"
            );

            let dest_path = destination.clone();
            let actual_checksum = tokio::task::spawn_blocking(move || {
                let file = File::open(&dest_path)
                    .map_err(|e| crate::error::Error::io_with_path("open file for checksum", dest_path.clone(), e))?;
                let mut reader = BufReader::new(file);
                let mut hasher = Sha256::new();
                let mut buffer = [0; 8192];

                loop {
                    let count = reader.read(&mut buffer).map_err(|e| {
                        crate::error::Error::io_with_path("read file for checksum", dest_path.clone(), e)
                    })?;
                    if count == 0 {
                        break;
                    }
                    hasher.update(&buffer[..count]);
                }

                let result = hasher.finalize();
                Ok::<_, crate::error::Error>(result.iter().fold(String::new(), |mut acc, b| {
                    use std::fmt::Write;
                    let _ = write!(acc, "{:02x}", b);
                    acc
                }))
            })
            .await
            .map_err(|e| crate::error::Error::runtime("checksum computation", e))??;

            if actual_checksum != *expected_checksum {
                // Delete the invalid file
                let _ = tokio::fs::remove_file(&destination).await;
                return Err(crate::error::Error::ChecksumMismatch {
                    path: destination.clone(),
                    expected: expected_checksum.to_string(),
                    actual: actual_checksum.clone(),
                });
            }

            tracing::debug!(
                expected = %expected_checksum,
                actual = %actual_checksum,
                "✅ Checksum verification passed"
            );
        }

        Ok(())
    }
}