forked from rofafor/vdr-plugin-iptv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3u8handler.cpp
311 lines (255 loc) · 10.2 KB
/
m3u8handler.cpp
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "m3u8handler.h"
#include "log.h"
#include "config.h"
#include "process.hpp"
std::pair<std::string, std::string> splitUri(const std::string& uri) {
auto yturi = uri::parse_uri(uri);
std::string host = yturi.scheme + "://" + yturi.authority.host
+ (yturi.authority.port > 0 ? std::to_string(yturi.authority.port) : "");
std::string query;
for (const auto& a : yturi.query) {
query.append(a.first).append("=").append(a.second).append("&");
}
std::string path = yturi.path + "?" + query;
return std::make_pair(host, path);
}
M3u8Handler::M3u8Handler() = default;
bool M3u8Handler::startsWith(const std::string &str, const std::string &prefix) {
return str.size() >= prefix.size() && 0==str.compare(0, prefix.size(), prefix);
}
std::vector<std::string> M3u8Handler::split(const std::string &s, char delim) {
std::vector<std::string> result;
std::stringstream ss(s);
std::string item;
while (getline(ss, item, delim)) {
result.push_back(item);
}
return result;
}
m3u_stream M3u8Handler::parseYT(const std::string &webUri) {
std::string useUriVideo;
std::string useUriAudio;
std::vector<std::string> callStr{
IptvConfig.GetYtdlpPath(), "--get-url",
"-f", "(bv*[vcodec~='^((he|a)vc|h26[45])']+ba[ext=m4a]/b[ext=mp4])", webUri
};
std::string newUri;
auto handler = new TinyProcessLib::Process(callStr, "",
[&newUri](const char *bytes, size_t n) {
std::string result = std::string(bytes, n);
debug1("yt-dlp found URL %s\n", result.c_str());
newUri = std::string(bytes, n);
},
[](const char *bytes, size_t n) {
std::string msg = std::string(bytes, n);
debug1("yt-dlp Error: %s\n", msg.c_str());
},
true
);
int exitStatus = handler->get_exit_status();
if (exitStatus!=0) {
debug1("yt-dlp throws an error, abort\n");
m3u_stream result;
result.width = result.height = 0;
return result;
}
// check if the uri contains video/audio URL or if it's a simple URL
auto m = newUri.find('\n');
if (m == std::string::npos) {
// only video
useUriVideo = newUri;
useUriAudio = "";
} else {
// audio / video
useUriVideo = newUri.substr(0, m);
useUriAudio = newUri.substr(m+1);
}
// set dummy values
m3u_stream result;
result.width = 1920;
result.height = 1080;
result.url = useUriVideo;
if (!useUriAudio.empty()) {
struct media audio;
audio.uri = useUriAudio;
audio.name = "audio";
audio.language = "C";
result.audio.emplace_back(audio);
}
return result;
}
m3u_stream M3u8Handler::parseM3u(const std::string &webUri, int useYtdlp) {
std::string useUri;
// if useYtdlp == 1 then use yt-dlp to get the real m3u8 URL
// if useYtdlp == 2 then get the page and try to find the real m3u8 URL
if (useYtdlp==1) {
std::vector<std::string> callStr{
IptvConfig.GetYtdlpPath(), "--get-url",
"-f", "(bv*[vcodec~='^((he|a)vc|h26[45])']+ba[ext=m4a]/b[ext=mp4])", webUri
};
std::string newUri;
auto handler = new TinyProcessLib::Process(callStr, "",
[&newUri](const char *bytes, size_t n) {
std::string result = std::string(bytes, n);
debug1("yt-dlp found URL %s\n", result.c_str());
newUri = std::string(bytes, n);
},
[](const char *bytes, size_t n) {
std::string msg = std::string(bytes, n);
debug1("yt-dlp Error: %s\n", msg.c_str());
},
true
);
int exitStatus = handler->get_exit_status();
if (exitStatus!=0) {
debug1("yt-dlp throws an error, abort\n");
m3u_stream result;
result.width = result.height = 0;
return result;
}
useUri = newUri;
} else if (useYtdlp==2) {
auto yturi = splitUri(webUri);
httplib::Client ytcli(yturi.first);
ytcli.set_follow_location(true);
auto ytres = ytcli.Get(yturi.second);
m3u_stream ytresult;
ytresult.width = ytresult.height = 0;
if (ytres==nullptr) {
error("Got no result for request %s\n", webUri.c_str());
return ytresult;
}
if (ytres->status!=200) {
debug1("Got HTTP result code %d\n", ytres->status);
return ytresult;
}
auto m = ytres->body.find(".m3u");
auto index = ytres->body.rfind("http", m);
useUri = ytres->body.substr(index, m + 4 - index);
} else {
useUri = webUri;
}
auto uri = splitUri(useUri);
httplib::Client cli(uri.first);
cli.set_follow_location(true);
auto res = cli.Get(uri.second);
m3u_stream result;
result.width = result.height = 0;
if (res==nullptr) {
return result;
}
if (res->status!=200) {
return result;
}
std::string m3u = res->body;
std::istringstream stream(m3u);
std::string line;
int maxW = 0, maxH = 0;
std::string audioGroup;
std::string m3uMax;
std::string starterStream = "#EXT-X-STREAM-INF:";
std::string starterMedia = "#EXT-X-MEDIA:";
std::string starterIgnore = "#EXTINF:";
while (std::getline(stream, line)) {
if (startsWith(line, starterStream)) {
auto splitted = split(line.substr(starterStream.length()), ',');
for (const auto &t : splitted) {
if (startsWith(t, "RESOLUTION=")) {
int w, h;
sscanf(t.c_str() + 11, "%dx%d", &w, &h);
debug2("Found Resolution: %dx%d\n", w, h);
if (w > maxW) {
maxW = w;
maxH = h;
std::getline(stream, m3uMax);
if (!startsWith(m3uMax, "http://") && !startsWith(m3uMax, "https://")) {
// this is a relative URL -> construct absolute URL
auto last = useUri.find_last_of('/');
m3uMax = useUri.substr(0, last + 1).append(m3uMax);
}
}
} else if (startsWith(t, "AUDIO=")) {
audioGroup = t.substr(7, t.length() - 8);
}
}
} else if (startsWith(line, starterMedia)) {
auto splitted = split(line.substr(starterMedia.length()), ',');
bool addThis = true;
media m;
for (const auto &t : splitted) {
if (startsWith(t, "TYPE=")) {
m.type = t.substr(5);
if (m.type!="AUDIO") {
// no audio -> skip
addThis = false;
break;
}
} else if (startsWith(t, "LANGUAGE=")) {
m.language = t.substr(10, t.length() - 11);
} else if (startsWith(t, "NAME=")) {
m.name = t.substr(6, t.length() - 7);
} else if (startsWith(t, "GROUP-ID=")) {
m.groupId = t.substr(10, t.length() - 11);
} else if (startsWith(t, "URI=")) {
m.uri = t.substr(5, t.length() - 6);
}
}
if (addThis) {
if (!startsWith(m.uri, "http://") && !startsWith(m.uri, "https://")) {
// this is a relative URL -> construct absolute URL
auto last = useUri.find_last_of('/');
m.uri = useUri.substr(0, last + 1).append(m.uri);
}
result.audio.push_back(m);
}
} else if (startsWith(line, starterIgnore)) {
// this is already a usable m3u8. Don't process anymore but return a useful result
result.width = 1920;
result.height = 1080;
result.url = useUri;
return result;
}
}
// remove all unwanted audio streams with wrong group-id
if (!audioGroup.empty() && !result.audio.empty()) {
auto it = result.audio.begin();
while (it!=result.audio.end()) {
if (it->groupId!=audioGroup) {
it = result.audio.erase(it);
} else {
++it;
}
}
}
result.width = maxW;
result.height = maxH;
result.url = m3uMax;
result.channelName = "";
result.vpid = -1;
result.spid = -1;
result.tpid = -1;
result.nid = -1;
result.apids.clear();
return result;
}
void M3u8Handler::printStream(const m3u_stream& stream) {
debug2("Url: %s\n", stream.url.c_str());
debug2(" Width: %d, Height: %d\n", stream.width, stream.height);
if (! stream.audio.empty()) {
debug2(" Audio:\n");
for (const auto& s : stream.audio) {
debug2(" Type: %s\n", s.type.c_str());
debug2(" Lang: %s\n", s.language.c_str());
debug2(" Name: %s\n", s.name.c_str());
debug2(" Uri: %s\n", s.uri.c_str());
}
}
debug2(" Channel information:\n");
debug2(" Name: %s\n", stream.channelName.c_str());
debug2(" VPid: %d\n", stream.vpid);
debug2(" SPid: %d\n", stream.spid);
debug2(" TPid: %d\n", stream.tpid);
for (auto a : stream.apids) {
debug2(" APid: %d\n", a);
}
}