YoutubeDLSharp

repository·master·Indexed 42 days ago

https://github.com/bluegrams/youtubedlsharp

A .NET wrapper for youtube-dl and yt-dlp command-line tools. It allows C# developers to integrate video downloading, audio extraction, and metadata retrieval into applications, featuring high-level operations via the YoutubeDL class and low-level control through YoutubeDLProcess.

Tokens
2.2K
Snippets
10
Records
10
Agent score
4%

What's inside YoutubeDLSharp

  1. Download yt-dlp, FFmpeg, and Deno binaries

    master

    YoutubeDLSharp requires the yt-dlp and FFmpeg binaries to function. You can either download them manually or use the built-in utility methods provided by YoutubeDLSharp.Utils to download them automatically to your environment.

    Note: DownloadDeno() is optional and used specifically for certain YouTube downloads.

    await YoutubeDLSharp.Utils.DownloadYtDlp();
    await YoutubeDLSharp.Utils.DownloadFFmpeg();
    
    await YoutubeDLSharp.Utils.DownloadDeno(); // optional, for YouTube downloads
  2. Download audio and playlist content with YoutubeDL

    master

    You can use YoutubeDL to perform specialized downloads:

    • Audio Extraction: Use RunAudioDownload and specify the AudioConversionFormat (e.g., AudioConversionFormat.Mp3).
    • Playlist Downloads: Use RunVideoPlaylistDownload and provide start and end indices to select a range of videos from a playlist.
    // Extract audio as MP3
    var res = await ytdl.RunAudioDownload(
        "https://www.youtube.com/watch?v=QUQsqBqxoR4",
        AudioConversionFormat.Mp3
    );
    
    // Download specific range from a playlist
    var res = await ytdl.RunVideoPlaylistDownload(
        "https://www.youtube.com/playlist?list=PLPfak9ofGSn9sWgKrHrXrxQXXxwhCblaT",
        start: 52, end: 76
    );
  3. Load and save OptionSet configuration files

    master

    You can persist OptionSet configurations to a file and reload them later. The resulting file format is compatible with the configuration format used by yt-dlp and youtube-dl itself.

    // Save to file
    var saveOptions = new OptionSet();
    saveOptions.WriteConfigFile("path\\to\\file");
    
    // Reload configuration
    OptionSet loadOptions = OptionSet.LoadConfigFile("path\\to\\file");
  4. Track download progress and cancel downloads

    master

    All YoutubeDL download methods support progress tracking via IProgress<DownloadProgress> and cancellation via a CancellationToken.

    // a progress handler with a callback that updates a progress bar
    var progress = new Progress<DownloadProgress>(p => progressBar.Value = p.Progress);
    // a cancellation token source used for cancelling the download
    var cts = new CancellationTokenSource();
    
    await ytdl.RunVideoDownload("https://www.youtube.com/watch?v=_QdPW8JrYzQ",
                                progress: progress, ct: cts.Token);
  5. Use YoutubeDLProcess for low-level control

    master

    For direct and flexible access to the underlying process, use the YoutubeDLProcess class. This allows you to capture standard output and error streams directly.

    var ytdlProc = new YoutubeDLProcess();
    // capture the standard output and error output
    ytdlProc.OutputReceived += (o, e) => Console.WriteLine(e.Data);
    ytdlProc.ErrorReceived += (o, e) => Console.WriteLine("ERROR: " + e.Data);
    
    // start running
    string[] urls = new[] { "https://github.com/ytdl-org/youtube-dl#options" };
    await ytdlProc.RunAsync(urls, options);
  6. Configure downloads using OptionSet

    master

    The OptionSet class models the command-line options used by yt-dlp. You can pass an OptionSet to YoutubeDL methods to override default behavior or use RunWithOptions() to run a command directly with specific options.

    Properties in OptionSet correspond to yt-dlp flags. For options that can be specified multiple times, pass an array of strings.

    var options = new OptionSet()
    {
        NoContinue = true,
        RestrictFilenames = true,
        Format = "best",
        RecodeVideo = VideoRecodeFormat.Mp4,
        Exec = "echo {}"
    };
    
    // Handling multiple values for an option
    var optionsWithArgs = new OptionSet()
    {
        PostprocessorArgs = new[]
        {
            "ffmpeg:-vcodec h264_nvenc",
            "ffmpeg_i1:-hwaccel cuda -hwaccel_output_format cuda"
        }
    };
    
    // Applying options to a download
    var ytdl = new YoutubeDL();
    var res = await ytdl.RunVideoDownload(
        "https://www.youtube.com/watch?v=bq9ghmgqoyc",
        overrideOptions: options
    );
    
    // Or using WithOptions directly
    var res2 = await ytdl.WithOptions("<YOUR_URL>", options);
  7. Fetch video metadata without downloading

    master

    Use RunVideoDataFetch to retrieve extensive metadata about a video (like title, uploader, view count, and available formats) without actually downloading the media file. This returns a VideoData object.

    var res = await ytdl.RunVideoDataFetch("https://www.youtube.com/watch?v=_QdPW8JrYzQ");
    // get some video information
    VideoData video = res.Data;
    string title = video.Title;
    string uploader = video.Uploader;
    long? views = video.ViewCount;
    // all available download formats
    FormatData[] formats = video.Formats;
  8. Use the YoutubeDL class for high-level video operations

    master

    The YoutubeDL class provides high-level methods for common tasks like downloading videos, extracting audio, and fetching metadata.

    Before running downloads, you must configure the paths to your yt-dlp and FFmpeg binaries if they are not in your system's PATH or the current directory. You can also specify an OutputFolder.

    var ytdl = new YoutubeDL();
    // set the path of yt-dlp and FFmpeg if they're not in PATH or current directory
    ytdl.YoutubeDLPath = "path\\to\\yt-dlp.exe";
    ytdl.FFmpegPath = "path\\to\\ffmpeg.exe";
    // optional: set a different download folder
    ytdl.OutputFolder = "some\\directory\\for\\video\\downloads";
    
    // download a video
    var res = await ytdl.RunVideoDownload("https://www.youtube.com/watch?v=bq9ghmgqoyc");
    // the path of the downloaded file
    string path = res.Data;
  9. Add custom options to OptionSet

    master

    If you are using a modified version of yt-dlp or need flags not explicitly mapped in the OptionSet class, you can add custom options using AddCustomOption or SetCustomOption.

    // add
    options.AddCustomOption<string>("--my-custom-option", "value");
    // set
    options.SetCustomOption<string>("--my-custom-option", "new value");