csharp-youtube-explode

How to download YouTube videos from C# with YouTubeExplode

  • 2 min

YouTubeExplode is a C# library that provides an interface for querying metadata of YouTube videos, playlists, and channels.

With YouTubeExplode, we can easily perform a wide variety of tasks, such as downloading videos and playlists, searching for videos by keywords, getting information about videos and channels, and much more.

The library is designed to be intuitive and easy to understand. Furthermore, there is a lot of documentation and examples on the project’s page.

YouTubeExplode is compatible with .NET Standard 2.0, which means it can be used on any platform that supports this version of the framework, including Windows, Linux, and macOS.

Downloading videos from YouTube may be against the terms of use and service in your country. Use it at your own responsibility.

How to Use YouTubeExplode

We can add the library to a .NET project easily, through the corresponding NuGet package.

Install-Package YoutubeExplode

Here are some examples of how to use YouTubeExplode, extracted from the library’s documentation. For example, this is how we can get the metadata of a video:

using YoutubeExplode;

var youtube = new YoutubeClient();

var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
var streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl);
var streamInfo = streamManifest.GetMuxedStreams().GetWithHighestVideoQuality();
var stream = await youtube.Videos.Streams.GetAsync(streamInfo);
await youtube.Videos.Streams.DownloadAsync(streamInfo, $"video.{streamInfo.Container}");
Copied!

While to download the actual video stream we would do:

using YoutubeExplode;

var youtube = new YoutubeClient();

// You can specify either the video URL or its ID
var videoUrl = "https://youtube.com/watch?v=u_yIGGhubZs";
var video = await youtube.Videos.GetAsync(videoUrl);

var title = video.Title; // "Collections - Blender 2.80 Fundamentals"
var author = video.Author.ChannelTitle; // "Blender"
var duration = video.Duration; // 00:07:20
Copied!

And if instead of a video we wanted to work with a playlist, we could do it like this:

using YoutubeExplode;

var youtube = new YoutubeClient();
var playlistUrl = "https://youtube.com/playlist?list=PLa1F2ddGya_-UvuAqHAksYnB0qL9yWDO6";

await foreach (var video in youtube.Playlists.GetVideosAsync(playlistUrl))
{
    var title = video.Title;
    var author = video.Author;
}
Copied!

As we can see, it’s very simple. The library provides many more methods and options. Check the project page for more information.