DEV Community

Cover image for How to get video details from YouTube with .NET Core 3
Davide Bellone
Davide Bellone

Posted on • Originally published at code4it.dev on

How to get video details from YouTube with .NET Core 3

I have already talked about how to retrieve a list of YouTube videos by its channel ID.

Now it's time to check the details of a single video. Let's say that you like a song, you listen to it at least 2 times a day and you want to download its description because it contains the lyrics.

And let's say that this song is Tooth Fairy by Nanowar of Steel.

You can just copy and paste, right? Nah, too easy! 😁

Initial steps

The basic setup is the same I explained in my previous article about how to search for videos on a YouTube channel.

What you need to do is:

  1. Retrieve you API key;
  2. Create a .NET Core application
  3. Install Google.Apis and Google.Apis.YouTube.v3 NuGet packages
  4. Create the class that will hold the video details:
public class YouTubeVideoDetails
{
    public string VideoId { get; set; }
    public string Description { get; set; }
    public string Title { get; set; }
    public string ChannelTitle { get; set; }
    public DateTime? PublicationDate { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Also, since you want to get the details of a video, you need the VideoId. You can retrieve it in 2 ways: programmatically, using the procedure from the previous article, or analysing the YouTube URL: if we have https://www.youtube.com/watch?v=CzvQxQYKO88, the id is CzvQxQYKO88.

Now we have everything we need. Let's go!

Add a YouTube service

Again, we need to instantiate the YouTube service.

using (var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    ApiKey = '<your api key>'
}))
{
    // your code here 
}
Enter fullscreen mode Exit fullscreen mode

Download video details

Since YouTube provides an object for each service, we must use the correct one, and then we need to specify the video ID:

var searchRequest = youtubeService.Videos.List("snippet");
searchRequest.Id = "CzvQxQYKO88";
Enter fullscreen mode Exit fullscreen mode

Once we have created the request, we need to retrieve the result:

var searchResponse = await searchRequest.ExecuteAsync();
Enter fullscreen mode Exit fullscreen mode

The searchResponse object contains various information shared with other services, like pagination. We don't need those info, and we can go straight to the video details:

var youTubeVideo = searchResponse.Items.FirstOrDefault();
Enter fullscreen mode Exit fullscreen mode

Finally, we can populate our YouTueVideoDetails object:

YouTubeVideoDetails videoDetails = new YouTubeVideoDetails()
{
    VideoId = youTubeVideo.Id,
    Description = youTubeVideo.Snippet.Description,
    Title = youTubeVideo.Snippet.Title,
    ChannelTitle = youTubeVideo.Snippet.ChannelTitle,
    PublicationDate = youTubeVideo.Snippet.PublishedAt
};
Enter fullscreen mode Exit fullscreen mode

The youTubeVideo object contains references to the thumbnails. As you might remember, in the article about how to search for videos associated to a YouTube channel, I explained that the images that you get with the Search endpoint are different to the ones here, in the Videos endpoint. My suggestion is to try both the examples on my GitHub page (see below) and find the differences.

Final result

Ok, we are ready to join all the pieces of the puzzle!

public async Task<YouTubeVideoDetails> GetVideoDetails()
{
    YouTubeVideoDetails videoDetails = null;
    using (var youtubeService = new YouTubeService(new BaseClientService.Initializer()
    {
        ApiKey = "<your-api-key>",
    }))
    {
        var searchRequest = youtubeService.Videos.List("snippet");
        searchRequest.Id = "CzvQxQYKO88";
        var searchResponse = await searchRequest.ExecuteAsync();

        var youTubeVideo = searchResponse.Items.FirstOrDefault();
        if(youTubeVideo!=null)
        {
            videoDetails = new YouTubeVideoDetails()
            {
                VideoId = youTubeVideo.Id,
                Description = youTubeVideo.Snippet.Description,
                Title = youTubeVideo.Snippet.Title,
                ChannelTitle = youTubeVideo.Snippet.ChannelTitle
            };
        }
    }
    return videoDetails;
}
Enter fullscreen mode Exit fullscreen mode

You can see a full example on this GitHub repository.

Now we have the lyrics, and we are ready to learn about macroeconomics, power metal and inflation! Enjoy!

Top comments (0)