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
//! Tools for working with the file system.

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

use tar::Archive;
use tokio::fs::{File, OpenOptions};
use uuid::Uuid;
use xz2::read::XzDecoder;
use zip::ZipArchive;

use crate::error::{Error, Result};

/// Converts a path to a UTF-8 string reference.
///
/// # Arguments
///
/// * `path` - The path to convert
///
/// # Returns
///
/// The path as a UTF-8 string slice
///
/// # Errors
///
/// Returns `Error::PathValidation` if the path contains invalid UTF-8
pub fn try_path_str(path: &Path) -> Result<&str> {
    path.to_str()
        .ok_or_else(|| Error::path_validation(path, "Path contains invalid UTF-8"))
}

/// Gets the file extension from a path, lowercased.
///
/// # Arguments
///
/// * `path` - The path to extract the extension from
///
/// # Returns
///
/// Lowercase file extension string
///
/// # Errors
///
/// Returns `Error::PathValidation` if the file has no extension or contains invalid characters
pub fn try_extension(path: &Path) -> Result<String> {
    let ext = path
        .extension()
        .ok_or_else(|| Error::path_validation(path, "File has no extension"))?
        .to_str()
        .ok_or_else(|| Error::path_validation(path, "Invalid characters in file extension"))?
        .to_lowercase();

    Ok(ext)
}

/// Creates a temporary output path for file processing.
///
/// # Arguments
///
/// * `file_path` - Original file path
/// * `file_format` - File extension for the temporary file
///
/// # Returns
///
/// `PathBuf` to a unique temporary file in the same directory
pub fn create_temp_path(file_path: &Path, file_format: &str) -> PathBuf {
    let parent_dir = file_path.parent().unwrap_or_else(|| Path::new(""));
    let uuid = Uuid::new_v4();

    if let Some(file_stem) = file_path.file_stem().and_then(|s| s.to_str()) {
        parent_dir.join(format!("{}_{}_temp.{}", file_stem, uuid, file_format))
    } else {
        parent_dir.join(format!("output_{}_temp.{}", uuid, file_format))
    }
}

/// Determines the MIME type of a file based on its extension.
///
/// # Arguments
///
/// * `path` - Path to the file
///
/// # Returns
///
/// The MIME type as a string
pub fn determine_mime_type(path: &Path) -> String {
    let extension = path.extension().and_then(|ext| ext.to_str()).unwrap_or("");

    match extension.to_lowercase().as_str() {
        "mp4" => "video/mp4",
        "webm" => "video/webm",
        "mp3" => "audio/mpeg",
        "m4a" => "audio/mp4",
        "jpg" | "jpeg" => "image/jpeg",
        "png" => "image/png",
        "vtt" => "text/vtt",
        "srt" => "application/x-subrip",
        "ass" | "ssa" => "text/x-ssa",
        _ => "application/octet-stream",
    }
    .to_string()
}

/// Returns the name of the given path.
///
/// # Arguments
///
/// * `path` - The path to extract the name from
///
/// # Returns
///
/// The file name as a string
///
/// # Errors
///
/// Returns an error if the path has no file name or contains invalid UTF-8
pub fn try_name(path: impl Into<PathBuf>) -> Result<String> {
    let path: PathBuf = path.into();

    let name = path
        .file_name()
        .ok_or_else(|| Error::path_validation(&path, "Path has no file name"))?;
    let name = name
        .to_str()
        .ok_or_else(|| Error::path_validation(&path, "File name contains invalid UTF-8"))?;

    Ok(name.to_string())
}

/// Returns the name of the given path without the extension.
///
/// # Arguments
///
/// * `path` - The path to extract the name from
///
/// # Returns
///
/// The file name without extension
///
/// # Errors
///
/// Returns an error if the path has no file stem or contains invalid UTF-8
pub fn try_without_extension(path: impl Into<PathBuf>) -> Result<String> {
    let path: PathBuf = path.into();

    let name = path
        .file_stem()
        .ok_or_else(|| Error::path_validation(&path, "Path has no file stem"))?;
    let name = name
        .to_str()
        .ok_or_else(|| Error::path_validation(&path, "File stem contains invalid UTF-8"))?;

    Ok(name.to_string())
}

/// Returns the parent directory of the given path.
///
/// # Arguments
///
/// * `path` - The path to extract the parent from
///
/// # Returns
///
/// The parent directory path
///
/// # Errors
///
/// Returns an error if the path has no parent
pub fn try_parent(path: impl Into<PathBuf>) -> Result<PathBuf> {
    let path: PathBuf = path.into();

    let parent = path
        .parent()
        .ok_or_else(|| Error::path_validation(&path, "Path has no parent directory"))?;

    Ok(parent.to_path_buf())
}

/// Creates a new file at the given destination.
///
/// # Arguments
///
/// * `destination` - The path to create the file at
///
/// # Returns
///
/// An opened file handle
///
/// # Errors
///
/// Returns an error if the file cannot be created
pub async fn create_file(destination: impl Into<PathBuf>) -> Result<File> {
    let destination: PathBuf = destination.into();

    tracing::debug!(
        destination = ?destination,
        "⚙️ Creating new file"
    );

    let mut open_options = OpenOptions::new();
    open_options.read(true);
    open_options.write(true);
    open_options.create(true);
    open_options.truncate(true);

    #[cfg(unix)]
    {
        open_options.mode(0o644);
    }

    let file = open_options.open(&destination).await?;

    tracing::debug!(
        destination = ?destination,
        "✅ File created successfully"
    );

    Ok(file)
}

/// Creates a new directory at the given destination.
/// If the directory already exists, nothing is done.
///
/// # Arguments
///
/// * `destination` - The path to create the directory at
///
/// # Returns
///
/// Ok(()) if the directory was created or already exists
///
/// # Errors
///
/// Returns an error if the directory cannot be created
pub async fn create_dir(destination: impl Into<PathBuf>) -> Result<()> {
    let destination: PathBuf = destination.into();

    tracing::debug!(
        destination = ?destination,
        "⚙️ Creating directory"
    );

    tokio::fs::create_dir_all(&destination).await?;

    tracing::debug!(
        destination = ?destination,
        "✅ Directory created successfully"
    );

    Ok(())
}

/// Creates the parent directory of the given destination.
/// If the parent directory already exists, nothing is done.
///
/// # Arguments
///
/// * `destination` - The path to create the parent directory for
///
/// # Returns
///
/// Ok(()) if the parent directory was created or already exists
///
/// # Errors
///
/// Returns an error if the parent directory cannot be created
pub async fn create_parent_dir(destination: impl Into<PathBuf>) -> Result<()> {
    let destination: PathBuf = destination.into();

    tracing::debug!(
        destination = ?destination,
        "⚙️ Creating parent directory"
    );

    if let Some(parent) = destination.parent() {
        tokio::fs::create_dir_all(parent).await?;
    } else {
        tokio::fs::create_dir_all(&destination).await?;
    }

    tracing::debug!(
        destination = ?destination,
        "✅ Parent directory created successfully"
    );

    Ok(())
}

/// Extracts a zip file to the given destination.
///
/// # Arguments
///
/// * `zip_path` - The path to the zip file.
/// * `destination` - The path to extract the zip file to.
pub async fn extract_zip(zip_path: impl Into<PathBuf>, destination: impl Into<PathBuf>) -> Result<()> {
    let zip_path: PathBuf = zip_path.into();
    let destination: PathBuf = destination.into();

    tracing::debug!(
        zip_path = ?zip_path,
        destination = ?destination,
        "⚙️ Extracting zip file"
    );

    let zip_path_for_tracing = zip_path.clone();
    let destination_for_tracing = destination.clone();

    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::open(&zip_path).map_err(|e| Error::io_with_path("open zip file", &zip_path, e))?;

        let mut archive = ZipArchive::new(file)?;

        for i in 0..archive.len() {
            let mut file = archive.by_index(i)?;

            let file_name = file
                .enclosed_name()
                .ok_or_else(|| {
                    Error::path_validation(
                        PathBuf::from(format!("zip entry {}", i)),
                        "Zip entry has no valid file name",
                    )
                })?
                .to_path_buf();

            let dest_path = destination.join(file_name);

            if file.is_dir() {
                std::fs::create_dir_all(&dest_path)
                    .map_err(|e| Error::io_with_path("create directory from zip", &dest_path, e))?;
            } else {
                if let Some(parent) = dest_path.parent() {
                    std::fs::create_dir_all(parent)
                        .map_err(|e| Error::io_with_path("create parent directory from zip", parent, e))?;
                }

                let mut outfile = std::fs::File::create(&dest_path)
                    .map_err(|e| Error::io_with_path("create file from zip", &dest_path, e))?;

                std::io::copy(&mut file, &mut outfile)
                    .map_err(|e| Error::io_with_path("copy file content from zip", &dest_path, e))?;
            }

            // Get and set permissions on Unix
            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                if let Some(mode) = file.unix_mode() {
                    std::fs::set_permissions(&dest_path, std::fs::Permissions::from_mode(mode))
                        .map_err(|e| Error::io_with_path("set permissions from zip", &dest_path, e))?;
                }
            }
        }

        Ok::<_, Error>(())
    })
    .await
    .map_err(|e| Error::runtime("extract zip archive", e))??;

    tracing::debug!(
        zip_path = ?zip_path_for_tracing,
        destination = ?destination_for_tracing,
        "✅ Zip file extracted successfully"
    );

    Ok(())
}

/// Extracts a tar.xz file to the given destination.
///
/// # Arguments
///
/// * `tar_path` - The path to the tar.xz file.
/// * `destination` - The path to extract the tar.xz file to.
pub async fn extract_tar_xz(tar_path: impl Into<PathBuf>, destination: impl Into<PathBuf>) -> Result<()> {
    let tar_path: PathBuf = tar_path.into();
    let destination: PathBuf = destination.into();

    tracing::debug!(
        tar_path = ?tar_path,
        destination = ?destination,
        "⚙️ Extracting tar.xz file"
    );

    let tar_path_for_tracing = tar_path.clone();
    let destination_for_tracing = destination.clone();

    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::open(&tar_path).map_err(|e| Error::io_with_path("open tar.xz file", &tar_path, e))?;

        let decompressor = XzDecoder::new(file);
        let mut archive = Archive::new(decompressor);

        archive
            .unpack(&destination)
            .map_err(|e| Error::io_with_path("unpack tar.xz archive", &destination, e))?;

        Ok::<_, Error>(())
    })
    .await
    .map_err(|e| Error::runtime("extract tar.xz archive", e))??;

    tracing::debug!(
        tar_path = ?tar_path_for_tracing,
        destination = ?destination_for_tracing,
        "✅ Tar.xz file extracted successfully"
    );

    Ok(())
}

/// Sets the executable bit on the given file.
///
/// # Arguments
///
/// * `executable` - The path to the executable file.
#[cfg(unix)]
pub async fn set_executable(executable: impl Into<PathBuf>) -> Result<()> {
    let executable: PathBuf = executable.into();

    tracing::debug!(path = ?executable, "⚙️ Setting executable permissions");

    let mut perms = tokio::fs::metadata(&executable).await?.permissions();

    perms.set_mode(0o755);
    tokio::fs::set_permissions(executable, perms).await?;

    Ok(())
}

/// No-op implementation for Windows, as Windows doesn't use executable bits.
///
/// # Arguments
///
/// * `executable` - The path to the executable file.
#[cfg(not(unix))]
pub async fn set_executable(_executable: impl Into<PathBuf>) -> Result<()> {
    // Windows doesn't use executable bits, so this is a no-op
    Ok(())
}

/// Generates a random filename with the specified length.
///
/// # Arguments
///
/// * `length` - The length of the random string to generate.
///
/// # Returns
///
/// A random string of the specified length.
pub fn random_filename(length: usize) -> String {
    let uuid = Uuid::new_v4().to_string().replace('-', "");

    uuid.chars().take(length).collect()
}

use std::sync::LazyLock;

use regex::Regex;

static VIDEO_ID_REGEX_1: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?:video|audio)-([a-zA-Z0-9_-]{11})").expect("Invalid regex"));
static VIDEO_ID_REGEX_2: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"([a-zA-Z0-9_-]{11})\.[a-zA-Z0-9]+$").expect("Invalid regex"));
static VIDEO_ID_REGEX_3: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[a-zA-Z0-9_-]{11}").expect("Invalid regex"));

/// Extracts a potential video ID from a filename.
pub fn extract_video_id(filename: &str) -> Option<String> {
    // Pattern 1: filename contains "video-[ID]" or "audio-[ID]"
    if let Some(captures) = VIDEO_ID_REGEX_1.captures(filename)
        && let Some(id) = captures.get(1)
    {
        return Some(id.as_str().to_string());
    }

    // Pattern 2: filename contains "[ID].mp4" or "[ID].mp3", etc.
    if let Some(captures) = VIDEO_ID_REGEX_2.captures(filename)
        && let Some(id) = captures.get(1)
    {
        return Some(id.as_str().to_string());
    }

    // Pattern 3: if the name directly contains a YouTube ID (11 characters)
    if let Some(captures) = VIDEO_ID_REGEX_3.captures(filename)
        && let Some(id) = captures.get(0)
    {
        let id_str = id.as_str();
        if id_str.len() == 11 {
            return Some(id_str.to_string());
        }
    }

    None
}

/// Removes a temporary file and logs any errors.
/// Does not propagate errors to avoid interrupting the execution flow.
///
/// # Arguments
///
/// * `file_path` - The path of the file to delete
///
/// # Returns
///
/// `true` if the file was successfully deleted, `false` otherwise
pub async fn remove_temp_file(file_path: impl Into<PathBuf>) -> bool {
    let file_path: PathBuf = file_path.into();
    let result = tokio::fs::remove_file(&file_path).await;

    if let Err(ref e) = result {
        tracing::warn!(path = ?file_path, error = %e, "Failed to remove temporary file");
    }

    result.is_ok()
}