// ==UserScript== // @name:en-US CCTV-HLS // @name CCTV视频解析 // @description:en-US parse cctv video to hls url. (stability + UI fixes) // @description 将CCTV视频解析成HLS地址(卡片式UI,仅链接含 /asp/h5e/ 时标注加密) // @namespace https://greasyfork.org/users/135090 // @version 1.7.0 // @author [ZWB](https://greasyfork.org/zh-CN/users/863179) + fixes // @license CC // @grant none // @run-at document-end // @match *://live.ipanda.com/*/*/*/V*.shtml* // @match *://*.cctv.com/*/*/*/V*.shtml* // @match *://*.cctv.com/*/*/*/A*.shtml* // @match *://*.cctv.cn/*/*/*/V*.shtml* // @match *://*.cctv.cn/*/*/*/A*.shtml* // @match *://*.cntv.cn/program/*/*/*.shtml* // @match *://vdn.apps.cntv.cn/api/getHttpVideoInfo* // @icon https://tv.cctv.cn/favicon.ico // ==/UserScript== 'use strict'; /* -------------------- 工具:MD5(原样保留) -------------------- */ const md5 = (() => { var hex_chr = '0123456789abcdef'.split(''); function rhex(num) { var s = ''; for (var j = 0; j < 4; j++) s += hex_chr[(num >> (j * 8 + 4)) & 0x0F] + hex_chr[(num >> (j * 8)) & 0x0F]; return s; } function add(x, y) { var lsw = (x & 0xFFFF) + (y & 0xFFFF); var msw = (x >> 16) + (y >> 16) + (lsw >> 16); return (msw << 16) | (lsw & 0xFFFF); } function cmn(q, a, b, x, s, t) { return add(add(rol(add(add(a, q), add(x, t)), s), b), 0); } function ff(a, b, c, d, x, s, t) { return cmn((b & c) | ((~b) & d), a, b, x, s, t); } function gg(a, b, c, d, x, s, t) { return cmn((b & d) | (c & (~d)), a, b, x, s, t); } function hh(a, b, c, d, x, s, t) { return cmn(b ^ c ^ d, a, b, x, s, t); } function ii(a, b, c, d, x, s, t) { return cmn(c ^ (b | (~d)), a, b, x, s, t); } function rol(num, cnt) { return (num << cnt) | (num >>> (32 - cnt)); } return function (s) { var x = []; for (let i = 0; i < s.length * 8; i += 8) x[i >> 5] |= (s.charCodeAt(i / 8) & 0xFF) << (i % 32); x[s.length * 8 >> 5] |= 0x80 << ((s.length * 8) % 32); x[(((s.length * 8) + 64) >>> 9 << 4) + 14] = s.length * 8; var a = 1732584193, b = -271733879, c = -1732584194, d = 271733878; for (let i = 0; i < x.length; i += 16) { var olda = a, oldb = b, oldc = c, oldd = d; a = ff(a, b, c, d, x[i], 7, -680876936); d = ff(d, a, b, c, x[i + 1], 12, -389564586); c = ff(c, d, a, b, x[i + 2], 17, 606105819); b = ff(b, c, d, a, x[i + 3], 22, -1044525330); a = ff(a, b, c, d, x[i + 4], 7, -176418897); d = ff(d, a, b, c, x[i + 5], 12, 1200080426); c = ff(c, d, a, b, x[i + 6], 17, -1473231341); b = ff(b, c, d, a, x[i + 7], 22, -45705983); a = ff(a, b, c, d, x[i + 8], 7, 1770035416); d = ff(d, a, b, c, x[i + 9], 12, -1958414417); c = ff(c, d, a, b, x[i + 10], 17, -42063); b = ff(b, c, d, a, x[i + 11], 22, -1990404162); a = ff(a, b, c, d, x[i + 12], 7, 1804603682); d = ff(d, a, b, c, x[i + 13], 12, -40341101); c = ff(c, d, a, b, x[i + 14], 17, -1502002290); b = ff(b, c, d, a, x[i + 15], 22, 1236535329); a = gg(a, b, c, d, x[i + 1], 5, -165796510); d = gg(d, a, b, c, x[i + 6], 9, -1069501632); c = gg(c, d, a, b, x[i + 11], 14, 643717713); b = gg(b, c, d, a, x[i], 20, -373897302); a = gg(a, b, c, d, x[i + 5], 5, -701558691); d = gg(d, a, b, c, x[i + 10], 9, 38016083); c = gg(c, d, a, b, x[i + 15], 14, -660478335); b = gg(b, c, d, a, x[i + 4], 20, -405537848); a = gg(a, b, c, d, x[i + 9], 5, 568446438); d = gg(d, a, b, c, x[i + 14], 9, -1019803690); c = gg(c, d, a, b, x[i + 3], 14, -187363961); b = gg(b, c, d, a, x[i + 8], 20, 1163531501); a = gg(a, b, c, d, x[i + 13], 5, -1444681467); d = gg(d, a, b, c, x[i + 2], 9, -51403784); c = gg(c, d, a, b, x[i + 7], 14, 1735328473); b = gg(b, c, d, a, x[i + 12], 20, -1926607734); a = hh(a, b, c, d, x[i + 5], 4, -378558); d = hh(d, a, b, c, x[i + 8], 11, -2022574463); c = hh(c, d, a, b, x[i + 11], 16, 1839030562); b = hh(b, c, d, a, x[i + 14], 23, -35309556); a = hh(a, b, c, d, x[i + 1], 4, -1530992060); d = hh(d, a, b, c, x[i + 4], 11, 1272893353); c = hh(c, d, a, b, x[i + 7], 16, -155497632); b = hh(b, c, d, a, x[i + 10], 23, -1094730640); a = hh(a, b, c, d, x[i + 13], 4, 681279174); d = hh(d, a, b, c, x[i], 11, -358537222); c = hh(c, d, a, b, x[i + 3], 16, -722521979); b = hh(b, c, d, a, x[i + 6], 23, 76029189); a = hh(a, b, c, d, x[i + 9], 4, -640364487); d = hh(d, a, b, c, x[i + 12], 11, -421815835); c = hh(c, d, a, b, x[i + 15], 16, 530742520); b = hh(b, c, d, a, x[i + 2], 23, -995338651); a = ii(a, b, c, d, x[i], 6, -198630844); d = ii(d, a, b, c, x[i + 7], 10, 1126891415); c = ii(c, d, a, b, x[i + 14], 15, -1416354905); b = ii(b, c, d, a, x[i + 5], 21, -57434055); a = ii(a, b, c, d, x[i + 12], 6, 1700485571); d = ii(d, a, b, c, x[i + 3], 10, -1894986606); c = ii(c, d, a, b, x[i + 10], 15, -1051523); b = ii(b, c, d, a, x[i + 1], 21, -2054922799); a = ii(a, b, c, d, x[i + 8], 6, 1873313359); d = ii(d, a, b, c, x[i + 15], 10, -30611744); c = ii(c, d, a, b, x[i + 6], 15, -1560198380); b = ii(b, c, d, a, x[i + 13], 21, 1309151649); a = ii(a, b, c, d, x[i + 4], 6, -145523070); d = ii(d, a, b, c, x[i + 11], 10, -1120210379); c = ii(c, d, a, b, x[i + 2], 15, 718787259); b = ii(b, c, d, a, x[i + 9], 21, -343485551); a = add(a, olda); b = add(b, oldb); c = add(c, oldc); d = add(d, oldd); } return rhex(a) + rhex(b) + rhex(c) + rhex(d); }; })(); /* -------------------- 工具:简易通知 -------------------- */ function showNotice(msg, type = 'error') { const el = document.createElement('div'); el.textContent = msg; el.style.cssText = ` position:fixed; z-index:99999; right:20px; bottom:20px; max-width:420px; padding:12px 14px; font-size:14px; line-height:1.6; color:#fff; background:${type === 'error' ? '#d93025' : '#188038'}; border-radius:8px; box-shadow:0 4px 16px rgba(0,0,0,.3);`; document.body.appendChild(el); if (type !== 'error') setTimeout(() => el.remove(), 5000); return el; } /* -------------------- 工具:结果卡片 -------------------- */ function buildCard({ title = 'CCTV 视频解析', status = 'ok' } = {}) { const card = document.createElement('div'); card.id = 'cctv-card'; card.style.cssText = ` position:fixed; z-index:99998; right:24px; bottom:24px; width:560px; max-width:calc(100vw - 48px); font-family:system-ui,-apple-system,"Segoe UI",sans-serif; background:#fff; color:#202124; border-radius:12px; overflow:hidden; box-shadow:0 8px 32px rgba(0,0,0,.25); font-size:14px; line-height:1.6;`; const accent = status === 'error' ? '#d93025' : '#1a73e8'; const head = document.createElement('div'); head.style.cssText = ` display:flex; align-items:center; justify-content:space-between; padding:12px 16px; background:${accent}; color:#fff; font-weight:600;`; const headTitle = document.createElement('span'); headTitle.textContent = title; const close = document.createElement('span'); close.textContent = '×'; close.style.cssText = 'cursor:pointer; font-size:20px; line-height:1; padding:0 4px;'; close.onclick = () => card.remove(); head.append(headTitle, close); const body = document.createElement('div'); body.className = 'cctv-card-body'; body.style.cssText = 'padding:14px 16px; word-break:break-all; max-height:60vh; overflow:auto;'; card.append(head, body); (document.body || document.documentElement).appendChild(card); return { card, body }; } function cardRow(body, label, value, href) { const row = document.createElement('div'); row.style.cssText = 'margin:8px 0;'; const lab = document.createElement('div'); lab.textContent = label; lab.style.cssText = 'color:#5f6368; font-size:12px; margin-bottom:2px;'; const val = document.createElement(href ? 'a' : 'div'); val.textContent = value; if (href) { val.href = href; val.target = '_blank'; val.style.cssText = 'color:#1a73e8; text-decoration:none;'; } else { val.style.color = '#202124'; } row.append(lab, val); body.appendChild(row); return val; } function cardButton(body, text, onClick) { const btn = document.createElement('button'); btn.textContent = text; btn.style.cssText = ` margin:4px 8px 4px 0; padding:6px 14px; border:none; border-radius:6px; background:#1a73e8; color:#fff; font-size:13px; cursor:pointer;`; btn.onclick = onClick; body.appendChild(btn); return btn; } /* -------------------- 主逻辑 -------------------- */ (async function () { /* ---- 播放页:注入"跳转下载页"按钮 ---- */ if (location.hostname.indexOf('vdn.apps.cntv.cn') === -1) { const READY_KEY = '__cctv_ready_tried__'; if (!window[READY_KEY]) window[READY_KEY] = 0; const buildApiUrl = (newguid) => { const params = { pid: newguid, client: 'flash', im: '0', tsp: Math.floor(Date.now() / 1000).toString(), vn: '2049', vc: null, uid: '826D8646DEBBFD97A82D23CAE45A55BE', wlan: '' }; params.vc = md5(params.tsp + params.vn + '47899B86370B879139C08EA3B5E88267' + params.uid); const sps = new URLSearchParams(); Object.entries(params).forEach(([k, v]) => { if (v !== null && v !== undefined) sps.append(k, v); }); return 'https://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?' + sps.toString(); }; const createJumpBtn = (apihref, n) => { const btn = document.createElement('a'); btn.href = apihref; btn.id = 'btn' + n; btn.type = 'button'; btn.target = '_blank'; btn.textContent = '点击跳转到下载页' + (n > 0 ? n + 1 : ''); btn.style = ` position:fixed; z-index:999; bottom:${n * 40 + 20}px; right:20px; background-color:#f86336; color:#fff; padding:5px; border:none; cursor:pointer; font-size:16px; border-radius:4px;`; document.body.appendChild(btn); }; const tryBuildButtons = () => { try { if (window.flashPlayerList?.length > 0) { window.flashPlayerList.forEach((raw, n) => { if (typeof raw !== 'string' || raw.length < 12) { console.warn('[CCTV解析] 跳过异常 flashPlayer 项:', raw); return; } const key = raw.substring(12); const newguid = window.vodPlayerObjs?.[key]?.videoCenterId; if (!newguid) { console.warn('[CCTV解析] 未取得 videoCenterId, key=', key); return; } createJumpBtn(buildApiUrl(newguid), n); }); return true; } if (window.loading_video) { const m = String(window.loading_video).match(/centerid.*?"([0-9a-f]{6,32})"/i); const newguid = m?.[1]; if (!newguid) return false; createJumpBtn('https://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?client=flash&im=0&pid=' + encodeURIComponent(newguid), 0); return true; } } catch (e) { console.error('[CCTV解析] 构建按钮异常:', e); showNotice('CCTV解析脚本异常(构建按钮): ' + e.message); return true; } return false; }; const MAX_TRIES = 6, INTERVAL = 500; const tick = () => { window[READY_KEY]++; if (tryBuildButtons()) return; if (window[READY_KEY] >= MAX_TRIES) { showNotice( 'CCTV解析:未检测到播放器对象(flashPlayerList / loading_video 均空),' + '可能是页面结构变化或脚本加载时机不对。可刷新后重试。' ); } else { setTimeout(tick, INTERVAL); } }; setTimeout(tick, 1500); } /* ---- API 页面:解析 HLS(保留码率选择 + H5E DRM 地址) ---- */ /* ---- API 页面:解析 HLS(码率选择 + H5E DRM,按钮带清晰度并靠近链接) ---- */ if (location.hostname.indexOf('vdn.apps.cntv.cn') > -1) { let data; try { const prebody = document?.body?.querySelector('pre')?.textContent || document?.body?.textContent || ''; if (!prebody.trim()) throw new Error('接口返回体为空'); data = JSON.parse(prebody); } catch (e) { const { body } = buildCard({ status: 'error', title: 'CCTV 视频解析 - 失败' }); cardRow(body, '错误信息', '接口返回不是合法 JSON(可能被限流/返回了 HTML):' + e.message); return; } let hlsUrl = data?.hls_url; const title = (data?.title || 'video').replaceAll(' ', ''); // ---- 码率选择(保留原逻辑) ---- const brt = [450, 850, 1200, 2000, 4000, 3000]; const brtNames = ['流畅', '标清', '高清', '全高清', '超清 4K', '超清']; const brtnum = data?.video?.validChapterNum; let bi = (brtnum > 0 && brtnum < 3) ? brtnum - 1 : 1; if (typeof hlsUrl !== 'string' || !hlsUrl) { const { body } = buildCard({ status: 'error', title: 'CCTV 视频解析 - 失败' }); cardRow(body, '错误信息', '接口数据中未找到 hls_url,无法继续。'); return; } // 拉主 m3u8 仅用于确定码率分支(不做 EXT-X-KEY 加密检测) try { const mainResponse = await fetch(hlsUrl); if (mainResponse.ok && mainResponse.status === 200) { const m3u8Content = await mainResponse.text(); if (data?.play_channel?.indexOf('CCTV-4K') === 0) { bi = 4; } else if (data?.play_channel?.indexOf('CCTV-16') === 0) { bi = 5; } else if (m3u8Content.includes('1200.m3u8')) { bi = 3; } hlsUrl = hlsUrl.replaceAll('main', brt[bi]); } else { throw new Error('HTTP ' + mainResponse.status); } } catch (e) { const { body } = buildCard({ status: 'error', title: 'CCTV 视频解析 - 版权原因无法获取' }); cardRow(body, '错误信息', '获取 m3u8 失败:' + e.message); return; } // ---- 加密判定:仅当链接含 /asp/h5e/ ---- const encrypted = hlsUrl.includes('/asp/h5e/'); // ---- 复制按钮(带清晰度标注) ---- const makeCopyBtn = (text, getUrl, label) => { const btn = document.createElement('button'); btn.textContent = label; btn.style.cssText = ` margin:4px 8px 4px 0; padding:6px 14px; border:none; border-radius:6px; background:#1a73e8; color:#fff; font-size:13px; cursor:pointer;`; btn.onclick = () => { const url = getUrl(); const done = () => { btn.textContent = '✓ 已复制'; setTimeout(() => { btn.textContent = label; }, 1500); }; const fallback = () => { const ta = document.createElement('textarea'); ta.value = url; ta.style.cssText = 'position:fixed; opacity:0;'; document.body.appendChild(ta); ta.select(); try { document.execCommand('copy'); done(); } catch { showNotice('复制失败,请手动复制'); } ta.remove(); }; if (navigator.clipboard?.writeText) { navigator.clipboard.writeText(url).then(done, fallback); } else { fallback(); } }; return btn; }; // ---- 打开按钮 ---- const makeOpenBtn = (getUrl) => { const btn = document.createElement('button'); btn.textContent = '在新标签页打开'; btn.style.cssText = ` margin:4px 8px 4px 0; padding:6px 14px; border:none; border-radius:6px; background:#5f6368; color:#fff; font-size:13px; cursor:pointer;`; btn.onclick = () => window.open(getUrl(), '_blank'); return btn; }; /* ---- 成功:展示结果卡片 ---- */ const { body } = buildCard({ title: 'CCTV 视频解析 - 成功' }); cardRow(body, '标题', title); // ---- HLS 链接区(按钮紧挨链接下方) ---- const hlsTitle = document.createElement('div'); hlsTitle.textContent = 'HLS 地址(' + brt[bi] + 'kbps / ' + brtNames[bi] + '):'; hlsTitle.style.cssText = 'margin:14px 0 6px; font-weight:600;'; body.appendChild(hlsTitle); cardRow(body, '', hlsUrl, hlsUrl); const hlsBtnBar = document.createElement('div'); hlsBtnBar.style.cssText = 'margin:6px 0 4px;'; hlsBtnBar.append( makeCopyBtn(null, () => hlsUrl, '复制 ' + brt[bi] + 'kbps(' + brtNames[bi] + ')地址'), makeOpenBtn(() => hlsUrl) ); body.appendChild(hlsBtnBar); // ---- H5E DRM 地址区(保留,按钮同样带清晰度并靠近链接) ---- if (brtnum > 3) { const maxbr = data?.play_channel?.indexOf('CCTV-16') === 0 ? '3000' : '2000'; const maxbrName = maxbr === '3000' ? '超清' : '全高清'; const h5e = data?.manifest?.hls_h5e_url?.replaceAll('main', maxbr); if (h5e) { const h5eTitle = document.createElement('div'); h5eTitle.textContent = 'H5E 加密流地址(DRM 专用,' + maxbr + 'kbps / ' + maxbrName + '):'; h5eTitle.style.cssText = 'margin:14px 0 6px; font-weight:600;'; body.appendChild(h5eTitle); const cdn = h5e.split('/')[2]; const guid = h5e.split('/')[10]; cardRow(body, 'CDN <-> GUID', cdn + ' <-> ' + guid); cardRow(body, '', h5e, h5e); const h5eBtnBar = document.createElement('div'); h5eBtnBar.style.cssText = 'margin:6px 0 4px;'; h5eBtnBar.append( makeCopyBtn(null, () => h5e, '复制 ' + maxbr + 'kbps(' + maxbrName + ' 🔒加密)地址'), makeOpenBtn(() => h5e) ); body.appendChild(h5eBtnBar); } } } })();