Add proxy-m3u8
This commit is contained in:
+50
-41
@@ -20,6 +20,7 @@
|
||||
scope.OPT_MODE_NOTIFY_ADS_WATCHED_ATTEMPTS = 2;// Larger values might increase load time. Lower values may increase ad chance.
|
||||
scope.OPT_MODE_NOTIFY_ADS_WATCHED_MIN_REQUESTS = true;
|
||||
scope.OPT_MODE_NOTIFY_ADS_WATCHED_RELOAD_PLAYER_ON_AD_SEGMENT = false;
|
||||
scope.OPT_MODE_PROXY_M3U8 = '';
|
||||
scope.OPT_VIDEO_SWAP_PLAYER_TYPE = 'thunderdome';
|
||||
scope.OPT_INITIAL_M3U8_ATTEMPTS = 1;
|
||||
scope.OPT_ACCESS_TOKEN_PLAYER_TYPE = '';
|
||||
@@ -240,52 +241,60 @@
|
||||
};
|
||||
send();
|
||||
});
|
||||
} else if (url.includes('/api/channel/hls/') && !url.includes('picture-by-picture') && OPT_MODE_STRIP_AD_SEGMENTS) {
|
||||
return new Promise(async function(resolve, reject) {
|
||||
// - First m3u8 request is the m3u8 with the video encodings (360p,480p,720p,etc).
|
||||
// - Second m3u8 request is the m3u8 for the given encoding obtained in the first request. At this point we will know if there's ads.
|
||||
var maxAttempts = OPT_INITIAL_M3U8_ATTEMPTS <= 0 ? 1 : OPT_INITIAL_M3U8_ATTEMPTS;
|
||||
var attempts = 0;
|
||||
while(true) {
|
||||
var encodingsM3u8Response = await realFetch(url, options);
|
||||
if (encodingsM3u8Response.status === 200) {
|
||||
var encodingsM3u8 = await encodingsM3u8Response.text();
|
||||
var streamM3u8Url = encodingsM3u8.match(/^https:.*\.m3u8$/m)[0];
|
||||
var streamM3u8Response = await realFetch(streamM3u8Url);
|
||||
var streamM3u8 = await streamM3u8Response.text();
|
||||
if (!streamM3u8.includes(AD_SIGNIFIER) || ++attempts >= maxAttempts) {
|
||||
if (maxAttempts > 1 && attempts >= maxAttempts) {
|
||||
console.log('max skip ad attempts reached (attempt #' + attempts + ')');
|
||||
}
|
||||
var channelName = (new URL(url)).pathname.match(/([^\/]+)(?=\.\w+$)/)[0];
|
||||
var streamInfo = StreamInfos[channelName];
|
||||
if (streamInfo == null) {
|
||||
StreamInfos[channelName] = streamInfo = {};
|
||||
}
|
||||
// This might potentially backfire... maybe just add the new urls
|
||||
streamInfo.ChannelName = channelName;
|
||||
streamInfo.Urls = [];
|
||||
streamInfo.RootM3U8Params = (new URL(url)).search;
|
||||
streamInfo.BackupUrl = null;
|
||||
streamInfo.BackupFailed = false;
|
||||
var lines = encodingsM3u8.replace('\r', '').split('\n');
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
if (!lines[i].startsWith('#') && lines[i].includes('.m3u8')) {
|
||||
streamInfo.Urls.push(lines[i]);
|
||||
StreamInfosByUrl[lines[i]] = streamInfo;
|
||||
}
|
||||
else if (url.includes('/api/channel/hls/') && !url.includes('picture-by-picture')) {
|
||||
if (OPT_MODE_PROXY_M3U8) {
|
||||
var channelName = (new URL(url)).pathname.match(/([^\/]+)(?=\.\w+$)/)[0];
|
||||
url = OPT_MODE_PROXY_M3U8 + channelName;
|
||||
console.log('Proxy: ' + url);
|
||||
}
|
||||
else if (OPT_MODE_STRIP_AD_SEGMENTS) {
|
||||
return new Promise(async function(resolve, reject) {
|
||||
// - First m3u8 request is the m3u8 with the video encodings (360p,480p,720p,etc).
|
||||
// - Second m3u8 request is the m3u8 for the given encoding obtained in the first request. At this point we will know if there's ads.
|
||||
var maxAttempts = OPT_INITIAL_M3U8_ATTEMPTS <= 0 ? 1 : OPT_INITIAL_M3U8_ATTEMPTS;
|
||||
var attempts = 0;
|
||||
while(true) {
|
||||
var encodingsM3u8Response = await realFetch(url, options);
|
||||
if (encodingsM3u8Response.status === 200) {
|
||||
var encodingsM3u8 = await encodingsM3u8Response.text();
|
||||
var streamM3u8Url = encodingsM3u8.match(/^https:.*\.m3u8$/m)[0];
|
||||
var streamM3u8Response = await realFetch(streamM3u8Url);
|
||||
var streamM3u8 = await streamM3u8Response.text();
|
||||
if (!streamM3u8.includes(AD_SIGNIFIER) || ++attempts >= maxAttempts) {
|
||||
if (maxAttempts > 1 && attempts >= maxAttempts) {
|
||||
console.log('max skip ad attempts reached (attempt #' + attempts + ')');
|
||||
}
|
||||
var channelName = (new URL(url)).pathname.match(/([^\/]+)(?=\.\w+$)/)[0];
|
||||
var streamInfo = StreamInfos[channelName];
|
||||
if (streamInfo == null) {
|
||||
StreamInfos[channelName] = streamInfo = {};
|
||||
}
|
||||
// This might potentially backfire... maybe just add the new urls
|
||||
streamInfo.ChannelName = channelName;
|
||||
streamInfo.Urls = [];
|
||||
streamInfo.RootM3U8Params = (new URL(url)).search;
|
||||
streamInfo.BackupUrl = null;
|
||||
streamInfo.BackupFailed = false;
|
||||
var lines = encodingsM3u8.replace('\r', '').split('\n');
|
||||
for (var i = 0; i < lines.length; i++) {
|
||||
if (!lines[i].startsWith('#') && lines[i].includes('.m3u8')) {
|
||||
streamInfo.Urls.push(lines[i]);
|
||||
StreamInfosByUrl[lines[i]] = streamInfo;
|
||||
}
|
||||
}
|
||||
resolve(new Response(encodingsM3u8));
|
||||
break;
|
||||
}
|
||||
resolve(new Response(encodingsM3u8));
|
||||
console.log('attempt to skip ad (attempt #' + attempts + ')');
|
||||
} else {
|
||||
// Stream is offline?
|
||||
resolve(encodingsM3u8Response);
|
||||
break;
|
||||
}
|
||||
console.log('attempt to skip ad (attempt #' + attempts + ')');
|
||||
} else {
|
||||
// Stream is offline?
|
||||
resolve(encodingsM3u8Response);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
return realFetch.apply(this, arguments);
|
||||
|
||||
Reference in New Issue
Block a user