sledgehammer sledgehammer - 1 year ago 68
C# Question

youtubeextractor getting different resolutions

If anyone is familiar with using youtubeextractor, I am trying to do the following. I am using this example posted on the site for the library.

string link = "youtube link";

IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);

VideoInfo video = videoInfos.First(info => info.VideoType == VideoType.Mp4 && info.Resolution == 360);

This code only looks for a specific resolution, what if that resolution doesn't exist?

If anyone has an example of looking for a resolution of lets say 1080, if 1080 doesn't exist then we look for 720, otherwise grab a 420 resolution, in this priority order:
1080 -> 720 -> 480

Answer Source
VideoInfo video = videoInfos.OrderByDescending(info=>info.Resolution)
                  .First(info => info.VideoType == VideoType.Mp4)

This will return the .MP4 video with best resolution.

If you are interested in only some resolutions then you can do it as

var allowedResolutions = new List<int>() { 1080, 720, 480, 360 };

VideoInfo video = videoInfos.OrderByDescending(info=>info.Resolution)
                  .Where(info => allowedResolutions.Contains(info.Resolution))
                  .First(info => info.VideoType == VideoType.Mp4)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download