Skip to content

一分钟搭建影视站,支持Docker等部署方式

refactor(广告过滤): 优化M3U8广告过滤逻辑

shinya6032408a1924
diff --git a/js/config.js b/js/config.js
index bd02ae2..bf494d7 100644
--- a/js/config.js
+++ b/js/config.js
@@ -24,12 +24,14 @@ const API_SITES = {
heimuer: {
api: 'https://json.heimuer.xyz',
name: '黑木耳',
- detail: 'https://heimuer.tv'
+ detail: 'https://heimuer.tv',
+ filterAdRule: '#EXT-X-DISCONTINUITY\\n#EXTINF:\\d+\\.\\d+,\\n.*?\\n#EXT-X-DISCONTINUITY'
},
ffzy: {
api: 'http://ffzy5.tv',
name: '非凡影视',
- detail: 'http://ffzy5.tv'
+ detail: 'http://ffzy5.tv',
+ filterAdRule: '#EXT-X-DISCONTINUITY[\\r\\n]+(?:#EXTINF:\\d+\\.\\d{6},[\\r\\n]+.*?[\\r\\n]+){4,5}#EXT-X-DISCONTINUITY'
},
tyyszy: {
api: 'https://tyyszy.com',
@@ -76,12 +78,10 @@ const API_SITES = {
mdzy: {
api: 'https://www.mdzyapi.com',
name: '魔都资源',
- filterAd: false,
},
ruyi: {
api: 'https://cj.rycjapi.com',
name: '如意资源',
- filterAd: false,
},
jkun: {
api: 'https://jkunzyapi.com',
diff --git a/player.html b/player.html
index e6ab137..3077c58 100644
--- a/player.html
+++ b/player.html
@@ -525,10 +525,12 @@
function initPlayer(videoUrl, sourceCode) {
if (!videoUrl) return;
+ window.filterAdPattern = API_SITES[sourceCode].filterAdRule
+
// 配置HLS.js选项
const hlsConfig = {
debug: false,
- loader: (adFilteringEnabled && API_SITES[sourceCode].filterAd !== false) ? CustomHlsJsLoader : Hls.DefaultConfig.loader,
+ loader: (adFilteringEnabled && API_SITES[sourceCode].filterAdRule) ? CustomHlsJsLoader : Hls.DefaultConfig.loader,
enableWorker: true,
lowLatencyMode: false,
backBufferLength: 90,
@@ -874,67 +876,9 @@
function filterAdsFromM3U8(m3u8Content, strictMode = false) {
if (!m3u8Content) return '';
- let skipContent = false;
- let discontinuityCount = 0;
- let currentSegmentDuration = 0;
- let totalOriginalDuration = 0;
- let totalFilteredDuration = 0;
- let afterSegmentUrl = false
-
- // 按行分割M3U8内容
- const lines = m3u8Content.split('\n');
- const filteredLines = [];
-
- for (let i = 0; i < lines.length; i++) {
- const line = lines[i].trim();
-
- if (line.includes('#EXT-X-DISCONTINUITY')) {
- discontinuityCount++;
- if (afterSegmentUrl) {
- // 在视频 url 前可能会出现 DISCONTINUITY 的情况,忽略
- skipContent = !skipContent;
- }
- continue;
- }
-
- if (line.startsWith('#EXTINF:')) {
- const durationMatch = line.match(/#EXTINF:(\d+(\.\d+)?)/);
- if (durationMatch) {
- currentSegmentDuration = parseFloat(durationMatch[1]);
- totalOriginalDuration += currentSegmentDuration;
-
- if (!skipContent) {
- totalFilteredDuration += currentSegmentDuration;
- filteredLines.push(line);
- }
- } else {
- if (!skipContent) {
- filteredLines.push(line);
- }
- }
- continue;
- }
- // 片段 URL 行
- if (line.length > 0 && !line.startsWith('#')) {
- if (!skipContent) {
- filteredLines.push(line);
- }
- afterSegmentUrl = true;
- currentSegmentDuration = 0;
- continue;
- }
-
- if (!skipContent || line.startsWith('#EXT')) {
- filteredLines.push(line);
- }
- }
-
- console.log('原始时长:', totalOriginalDuration, '过滤后时长:', totalFilteredDuration);
- // 替换 M3U8 中的总时长标记(如果存在)
- const filteredText = filteredLines.join('\n');
- return filteredText.replace(/#EXT-X-TARGETDURATION:(\d+(\.\d+)?)/, (match, p1) => {
- return `#EXT-X-TARGETDURATION:${p1}`;
- });
+ let pattern = window.filterAdPattern;
+ const regex = new RegExp(pattern, 'g');
+ return m3u8Content.replace(regex, '');
}