private YDLVideoFormatEntry?FindMatchingFormatByQuality(YDLVideoEntry videoEntry, int quality = -1) { if (videoEntry.Formats.Count == 0) { return(null); } if (quality == -1) { return(videoEntry.Formats[0]); } //if we find an mp4 video with desired height/resolution return it var fmt = FindOnlyMatchingMp4(videoEntry, quality); if (fmt != null) { return(fmt); } //if no mp4 is found look for other formats like mkv or webm foreach (var format in videoEntry.Formats) { if (!string.IsNullOrEmpty(format.Height) && Int32.TryParse(format.Height, out int height) && height == quality) { return(format); } } //so far no luck, try to find next best resoultion var max = -1; foreach (var format in videoEntry.Formats) { if (!string.IsNullOrEmpty(format.Height) && Int32.TryParse(format.Height, out int height) && height > 0 && quality > height) { if (height > max) { max = height; fmt = format; } } } if (fmt != null) { return(fmt); } //could not found anything as per criteria, return the first format return(videoEntry.Formats[0]); }
private YDLVideoFormatEntry?FindOnlyMatchingMp4(YDLVideoEntry videoEntry, int quality) { if (videoEntry.Formats.Count == 0) { return(null); } foreach (var format in videoEntry.Formats) { if (!string.IsNullOrEmpty(format.Height) && Int32.TryParse(format.Height, out int height) && height == quality && (format.FileExt?.ToLowerInvariant()?.EndsWith("mp4") ?? false)) { return(format); } } return(null); }