I'm having a little problem with matching the highest bandwidth from m3u8 master file
Here's the content of master.m3u8
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=149000,RESOLUTION=320x180,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 1
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=300000,RESOLUTION=320x180,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 2
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=500000,RESOLUTION=640x360,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 3
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000,RESOLUTION=640x360,CODECS="avc1.66.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 4
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000,RESOLUTION=1280x720,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 5
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1299000,RESOLUTION=1280x720,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 6
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1499000,RESOLUTION=1280x720,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 7
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=62000,CODECS="mp4a.40.2",CLOSED-CAPTIONS=NONE
https://testurl 8
I'm able to get the url by resolution. But the problem is some of the master m3u8 has three url with the same resolution but different bandwidth.
My goal is, if I choose 1280x720 and if there will be multiple result with the same resolution, I want to get only the url with the highest bandwidth like in the example content. the highest is 1499000
Here's my current code.
private static void TEST()
{
var f = File.ReadAllText(@"C:\Users\MARK\Desktop\TEST\master.m3u8");
string result = string.Empty;
result = ParseM3u8Master(f, "1280x720", 2);
if (result == "") {
result = ParseM3u8Master(f, "640x360", 2);
}else if (result == ""){
result = ParseM3u8Master(f, "320x180", 2);
}
Console.WriteLine(result);
}
private const RegexOptions ML = RegexOptions.IgnoreCase | RegexOptions.Multiline;
public static string ParseM3u8Master(string m3u8Master, string resolution, int group)
{
string result = string.Empty;
string pattern = $@"(?:RESOLUTION=({resolution}).*?)\n(https.+|http[-a-zA-Z0-9()@:%_\+.~#?&//=])";
if (Regex.IsMatch(m3u8Master, pattern, ML))
{
result = Regex.Match(m3u8Master, pattern, ML).Groups[group].Value;
}
return result;
}