var headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', 'Accept': 'application/vnd.apple.mpegurl, video/*; q=0.9', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache', 'Upgrade-Insecure-Requests': '1', 'Referer': 'https://huggingface.co/', "origin": "https://huggingface.co" }; async function fetchM3U8(url) { var response = await fetch(url, { method: 'GET', headers }); var m3u8Text = await response.text(); // 获取 base URL,通常为 M3U8 文件的父目录 var baseUrl = getBaseUrl(url); // 解析 m3u8 内容,提取所有 TS 文件的 URL var tsUrls = parseM3U8(m3u8Text, baseUrl); var errorTsUrls = [] // 进一步处理每个 TS 文件,使用 fetch 加载 for (var tsUrl of tsUrls) { try { await fetch(tsUrl); } catch (error) { console.log(tsUrl, '请求失败') errorTsUrls.push(tsUrl) } } while (errorTsUrls.length) { var tsUrl = errorTsUrls.shift() try { await fetch(tsUrl); } catch (error) { console.log(tsUrl, '请求失败') errorTsUrls.push(tsUrl) } } } // 获取 M3U8 文件的 base URL function getBaseUrl(m3u8Url) { var url = new URL(m3u8Url); return url.origin + url.pathname.substring(0, url.pathname.lastIndexOf('/')); } // 解析 M3U8 内容,提取所有 TS 文件的 URL function parseM3U8(m3u8Text, baseUrl) { var tsUrls = []; var lines = m3u8Text.split('\n'); for (var line of lines) { if (line && line.endsWith('.ts')) { // 拼接相对路径与 base URL,构建完整的 TS 文件 URL var tsUrl = line.startsWith('http') ? line : new URL(line, baseUrl).href; tsUrls.push(tsUrl); } } return tsUrls; } var search = new URLSearchParams(location.search) // 示例调用 fetchM3U8(location.origin + search.get('url'));