CCTV客户端视频解析

将CCTV视频解析成HLS地址.

// ==UserScript==
// @name:en-US         CCTV-HLS-Client
// @name               CCTV客户端视频解析
// @description:en-US  parse cctv video to hls url.
// @description        将CCTV视频解析成HLS地址.
// @namespace          https://greasyfork.org/users/135090
// @version            1.4.3
// @author             [ZWB](https://greasyfork.org/zh-CN/users/863179)
// @license            CC
// @grant              none
// @run-at             document-end
// @match              https://*.cctv.com/2*/VID*.shtml*
// @match              https://*.cctv.cn/2*/VID*.shtml*
// @match              https://vdn.apps.cntv.cn/api/getHttpVideoInfo*
// @icon               https://tv.cctv.cn/favicon.ico
// ==/UserScript==
 
(function () {
    if (location.hostname.indexOf(".cctv.com") > 0 || location.hostname.indexOf(".cctv.cn") > 0) {
        var base="https://vdn.apps.cntv.cn";
        var apiUrl = base + `/api/getHttpVideoInfo.do?client=flash&im=0&pid=${guid}`;
        location.href = (apiUrl);
    }
 
    if (location.hostname.indexOf("vdn.apps.cntv.cn") > -1) {
        var data = JSON.parse(document.body.textContent);
        var hlsUrl = data?.manifest?.hls_enc2_url;
        var title = data?.title;
        if (data?.play_channel.indexOf("4K") > 0) {
            hlsUrl = hlsUrl.replaceAll("main", "4000");
        } else {
            hlsUrl = hlsUrl.replaceAll("main", "2000");
        }
        document.body.textContent=hlsUrl;
        console.info(hlsUrl);
        var tsn = hlsUrl.split("/")[10];
        console.info(tsn);
        var txtctt=document.createElement("h2");
        txtctt.textContent=title;
        document.body.appendChild(txtctt);
       //tsn换成title会导致下载下来的文件名变成网页标题
        downloadM3U8Video(hlsUrl, tsn + '.ts', {
            onProgress: (current, total) => {
                var cotp = `${Math.round((current / total) * 100)}`;
                txtctt.textContent = title + "---下载进程" + cotp + "%";
                console.info(`Progress: ${current}/${total} (${cotp}%)`);
            }
        });
    }
 
    async function downloadM3U8Video(m3u8Url, outputFilename = 'video.m2ts', options = {}) {
        try {
            // 1. 获取并解析M3U8文件
            void (alert('开始'));
            const response = await fetch(m3u8Url);
            if (!response.ok) throw new Error(`Failed to fetch M3U8: ${response.status}`);
 
            const m3u8Content = await response.text();
            const lines = m3u8Content.split('\n');
            const baseUrl = m3u8Url.substring(0, m3u8Url.lastIndexOf("/") + 1);
            const segments = [];
 
            // 解析TS分片URL
            for (const line of lines) {
                if (line && !line.startsWith('#') && (line.endsWith('.ts') || line.match(/\.ts\?/))) {
                    const segmentUrl = line.startsWith('http') ? line : new URL(line, baseUrl).href;
                    segments.push(segmentUrl);
                }
            }
 
            if (segments.length === 0) throw new Error('No TS segments found in the M3U8 file');
            console.log(`Found ${segments.length} TS segments`);
 
            // 2. 下载所有分片
            console.log('Downloading segments...');
            const blobs = [];
            const { onProgress } = options;
 
            for (let i = 0; i < segments.length; i++) {
                try {
                    const segmentResponse = await fetch(segments[i]);
                    if (!segmentResponse.ok) throw new Error(`Failed to fetch segment: ${segmentResponse.status}`);
 
                    const blob = await segmentResponse.blob();
                    blobs.push(blob);
 
                    // 调用进度回调
                    if (typeof onProgress === 'function') {
                        onProgress(i + 1, segments.length);
                    }
                } catch (error) {
                    console.error(`Error downloading segment ${segments[i]}:`, error);
                    throw error; // 可以选择继续或抛出错误
                }
            }
 
            // 3. 合并并下载
            console.log('Merging and downloading...');
            const mergedBlob = new Blob(blobs, { type: 'video/mp2t' });
            const url = URL.createObjectURL(mergedBlob);
 
            const a = document.createElement('a');
            a.href = url;
            a.download = outputFilename;
            document.body.appendChild(a);
            a.click();
 
            // 清理
            setTimeout(() => {
                document.body.removeChild(a);
                URL.revokeObjectURL(url);
            }, 100);
 
            console.log('Download completed!');
            return true;
        } catch (error) {
            console.error('Error downloading M3U8 video:', error);
            throw error;
        }
    }
})();