YoutubeExplode

repository·prime·Indexed 25 days ago

https://github.com/tyrrrz/youtubeexplode

A .NET library for querying YouTube metadata and downloading content, including videos, playlists, channels, and closed captions. It features the YoutubeExplode.Converter extension, which uses FFmpeg to fetch and mux individual audio and video streams into single files.

Tokens
3.4K
Snippets
12
Records
15
Agent score
37%

What's inside YoutubeExplode

  1. Overview of YoutubeExplode capabilities

    prime

    YoutubeExplode is a library for querying YouTube metadata and downloading content. It provides interfaces to:

    • Query metadata for videos, playlists, and channels.
    • Resolve video streams.
    • Download video streams.
    • Download closed caption tracks.

    Note: The library works by scraping raw page data and using reverse-engineered internal endpoints.

  2. Initialize YoutubeClient

    prime

    The YoutubeClient class is the single entry point for all operations in YoutubeExplode. You can use it for unauthenticated requests or provide a list of cookies to access private videos and playlists.

    using YoutubeExplode;
    
    // Unauthenticated
    using var youtube = new YoutubeClient();
    
    // Authenticated with cookies
    // Cookie collection must be of type IReadOnlyList<System.Net.Cookie>
    using var youtube = new YoutubeClient(cookies);
  3. Download videos using DownloadAsync

    prime

    You can download a YouTube video directly to a file by calling the DownloadAsync extension method on VideoClient. The method automatically resolves the best media streams (based on format, bitrate, framerate, and quality) and muxes them into a single file using FFmpeg.

    Note: If the output format is an audio-only container (like mp3 or ogg), only the audio stream will be downloaded.

    Tip: To avoid resource-intensive transcoding, use mp4 or webm as the output format, as these match YouTube's native stream containers.

    using YoutubeExplode;
    using YoutubeExplode.Converter;
    
    using var youtube = new YoutubeClient();
    
    var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
    await youtube.Videos.DownloadAsync(videoUrl, "video.mp4");
  4. Manually select streams for muxing

    prime

    For precise control, you can manually select specific audio and video streams from the StreamManifest and pass them to DownloadAsync using a ConversionRequestBuilder. This allows you to bypass the automatic high-quality selection logic.

    using YoutubeExplode;
    using YoutubeExplode.Videos.Streams;
    using YoutubeExplode.Converter;
    
    using var youtube = new YoutubeClient();
    
    // Get stream manifest
    var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
    var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl);
    
    // Select best audio stream (highest bitrate)
    var audioStreamInfo = streamManifest
        .GetAudioStreams()
        .Where(s => s.Container == Container.Mp4)
        .GetWithHighestBitrate();
    
    // Select best video stream (1080p60 in this example)
    var videoStreamInfo = streamManifest
        .GetVideoStreams()
        .Where(s => s.Container == Container.Mp4)
        .First(s => s.VideoQuality.Label == "1080p60");
    
    // Download and mux streams into a single file
    await youtube.Videos.DownloadAsync(
        [audioStreamInfo, videoStreamInfo],
        new ConversionRequestBuilder("video.mp4").Build()
    );
  5. Retrieve video metadata with Videos.GetAsync

    prime

    Use Videos.GetAsync(...) to retrieve metadata for a specific video. You can pass either the video URL or the video ID.

    using YoutubeExplode;
    
    using var youtube = new YoutubeClient();
    var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
    var video = await youtube.Videos.GetAsync(videoUrl);
    
    var title = video.Title;
    var author = video.Author.ChannelTitle;
    var duration = video.Duration;
  6. Retrieve channel metadata and uploads

    prime
    Retrieve channel information using Channels.GetAsync(...). You can also look up channels by username (GetByUserAsync), slug/custom URL (GetBySlugAsync), or handle (GetByHandleAsync). To get all videos uploaded by a channel, use Channels.GetUploadsAsync(...).
  7. Download video streams

    prime

    Once you have identified a StreamInfo object, you can either resolve the actual stream using Videos.Streams.GetAsync(...) or download it directly to a file using Videos.Streams.DownloadAsync(...).

    // Get the actual stream
    var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
    
    // Download the stream to a file
    await youtube.Videos.Streams.DownloadAsync(streamInfo, $"video.{streamInfo.Container}");
  8. Download closed captions

    prime

    Closed captions can be retrieved by getting a manifest of available tracks via Videos.ClosedCaptions.GetManifestAsync(...), selecting a track (e.g., by language), and then either getting the content via GetAsync(...) or downloading it as an .srt file via DownloadAsync(...).

    using YoutubeExplode;
    
    using var youtube = new YoutubeClient();
    var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
    
    // Get manifest
    var trackManifest = await youtube.Videos.ClosedCaptions.GetManifestAsync(videoUrl);
    
    // Find track in English
    var trackInfo = trackManifest.GetByLanguage("en");
    
    // Get content at a specific time
    var track = await youtube.Videos.ClosedCaptions.GetAsync(trackInfo);
    var caption = track.GetByTime(TimeSpan.FromSeconds(35));
    var text = caption.Text;
    
    // Or download as SRT
    await youtube.Videos.ClosedCaptions.DownloadAsync(trackInfo, "cc_track.srt");
  9. Retrieve playlist metadata and videos

    prime

    Use Playlists.GetAsync(...) for metadata and Playlists.GetVideosAsync(...) to retrieve the videos within a playlist. You can use .CollectAsync(n) to limit the number of videos retrieved or iterate using await foreach for efficient enumeration.

    using YoutubeExplode;
    using YoutubeExplode.Common;
    
    using var youtube = new YoutubeClient();
    var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6";
    
    // Metadata
    var playlist = await youtube.Playlists.GetAsync(playlistUrl);
    
    // Get all videos
    var videos = await youtube.Playlists.GetVideosAsync(playlistUrl);
    
    // Get first 20 videos
    var videosSubset = await youtube.Playlists.GetVideosAsync(playlistUrl).CollectAsync(20);
    
    // Iterate through videos
    await foreach (var video in youtube.Playlists.GetVideosAsync(playlistUrl))
    {
        var title = video.Title;
    }