-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM3u8Parser.cs
110 lines (104 loc) · 4.46 KB
/
M3u8Parser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace M3u8Parser
{
public class M3u8Parser : IM3u8Parser
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async Task<List<M3u8Media>> Parse()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
if (m3u8 == null)
throw new Exception("run Load first");
var lines = m3u8.Replace("\r", "").Split('\n');
var result = new List<M3u8Media>();
if (lines.Any())
{
var firstLine = lines[0];
if (firstLine != "#EXTM3U")
{
throw new InvalidOperationException(
"The provided URL does not link to a well-formed M3U8 playlist.");
}
bool mediaDetected = false;
M3u8Media streamInfo = null;
for (var i = 1; i < lines.Length; i++)
{
var line = lines[i];
if (line.StartsWith("#"))
{
var lineData = line.Substring(1);
var split = lineData.Split(':');
var name = split[0];
var suffix = split[1];
if (name == "EXT-X-MEDIA")
{
mediaDetected = true;
streamInfo = new M3u8Media();
}
var attributes = suffix.Split(',');
foreach (var item in attributes)
{
var keyvalue = item.Split('=');
if (keyvalue.Any())
switch (keyvalue[0])
{
case "TYPE":
streamInfo.Type = keyvalue[1].Trim('"');
break;
case "NAME":
streamInfo.Name = keyvalue[1].Trim('"');
break;
case "BANDWIDTH":
streamInfo.Bandwidth = long.Parse(keyvalue[1], CultureInfo.InvariantCulture);
break;
case "RESOLUTION":
try
{
streamInfo.Resolution = new Resolution();
var size = keyvalue[1].Split('x');
if (size != null)
{
streamInfo.Resolution.Width = int.Parse(size[0], CultureInfo.InvariantCulture);
streamInfo.Resolution.Height = int.Parse(size[1], CultureInfo.InvariantCulture);
}
}
catch
{
}
break;
case "CODECS":
streamInfo.Codecs = keyvalue[1].Trim('"');
break;
case "VIDEO":
streamInfo.Video = keyvalue[1].Trim('"');
break;
}
}
}
else
{
//Url ?
if (mediaDetected)
{
streamInfo.Url = line;
mediaDetected = false;
result.Add(streamInfo);
}
}
}
}
return result;
}
string m3u8;
public void Load(string text)
{
if (text == null)
throw new Exception("can not load null");
m3u8 = text;
}
}
}