全能视频下载器-自用

黑科技!使用MediaSouce的视频下载技术!

当前为 2023-02-05 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         全能视频下载器-自用
// @namespace    https://qinlili.bid
// @version      0.32
// @description  黑科技!使用MediaSouce的视频下载技术!
// @author       原:琴梨梨
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==
//这个下载器也可以用于其他网站,自己改一下match地址就行了捏

(function() {
    'use strict';
    //https://stackoverflow.com/questions/49129643/how-do-i-merge-an-array-of-uint8arrays
    const concat=(arrays)=> {
        // sum of individual array lengths
        let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);

        if (!arrays.length) return null;

        let result = new Uint8Array(totalLength);

        // for each array - copy it over result
        // next array is copied right after the previous one
        let length = 0;
        for(let array of arrays) {
            result.set(array, length);
            length += array.length;
        }

        return result;
    }
    const dlFile = (link, name) => {
        let eleLink = document.createElement('a');
        eleLink.download = name;
        eleLink.style.display = 'none';
        eleLink.href = link;
        document.body.appendChild(eleLink);
        eleLink.click();
        document.body.removeChild(eleLink);
    }



    (function (addSourceBuffer) {
        MediaSource.prototype.addSourceBuffer = function (mime) {
            console.log(mime)
            switch (mime.substr(0,5)){
                case "audio":
                    window.ams=addSourceBuffer.call(this, mime);
                    return window.ams
                    window.audioBuffer=[];
                    break;
                case "video":
                    window.vms=addSourceBuffer.call(this, mime);
                    return window.vms
                    window.videoBuffer=[];
                    break;
                default:
                    return addSourceBuffer.call(this, mime);
            }
        };
    })(MediaSource.prototype.addSourceBuffer);

    window.videoBuffer=[];
    window.audioBuffer=[];

    (function (appendBuffer) {
        SourceBuffer.prototype.appendBuffer = function (source) {
            if(this==window.ams){
                console.log("audio buffer get")
                window.audioBuffer[window.audioBuffer.length]=source
            }
            if(this==window.vms){
                console.log("video buffer get")
                window.videoBuffer[window.videoBuffer.length]=source
            }
            appendBuffer.call(this, source);
        };
    })(SourceBuffer.prototype.appendBuffer);
    const title=document.createElement("button")
    title.style=`
    position:absolute;
    z-index:9999;
    padding:12px 8px;
    `
    title.setAttribute('id','media_download_video_script')
    document.body.insertBefore(title,document.body.firstChild);
    title.innerText+="[点我开始下载视频]"
    title.addEventListener("click",()=>{
        title.innerText = '脚本运行成功';
        try{
           alert("请仔细阅读说明:\n本工具使用MediaSource下载视频\n点击确认后将以16倍速播放视频\n视频会在播放完成后开始下载\n请保持页面前台运行,千万不要拖拽进度条!!!")
        }catch{}
        title.innerText="[正在保存视频,请等待播放完成]"
        const video=document.getElementsByTagName("video")[0]
        video.addEventListener("ended",()=>{
            title.innerText="[正在导出视频]"
            var audioFile=new Blob([concat(window.audioBuffer)])
            dlFile(URL.createObjectURL(audioFile),"音频.m4a")
            var videoFile=new Blob([concat(window.videoBuffer)])
            dlFile(URL.createObjectURL(videoFile),"视频.mp4")
        })
        video.playbackRate=16
    })

    window.setInterval(()=>{
        if(document.querySelector('#media_download_video_script')==null){
            document.body.insertBefore(title,document.body.firstChild);
        }
    },1000)
})();