Tampermonkey | blob-视频下载插件

// ==UserScript==
// @name         blob格式视频下载
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  https://github.com/Momo707577045/media-source-extract (原资源, 下面是原demo)
// @author       CY3761
// @include      *
// @exclude      http://blog.luckly-mjw.cn/tool-show/media-source-extract/player/player.html
// @grant        GM_addStyle
// @grant        GM_download
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
// @run-at       document-start
// ==/UserScript==

// tampermonkey 文档地址
// https://www.tampermonkey.net/documentation.php

// 第三方库CDN地址 https://www.bootcdn.cn
// jquery | https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js

// GM_addStyle
(() => {
    GM_addStyle(`
        #blob-video-download-div {
            position: fixed;
            top: 45%;
            right: 10px;
            z-index: 9999;
        }
    `)
})();

(()=>{$(() => {
    'use strict'

    // 感觉一半一半而已

    setInterval(() => {
        const e = $('.pv-log-error')

        if (e) { e.remove() }
    }, 1e3)

    // 如果某个 DOM存在则不以下执行操作
    const sRootSelector = 'blob-video-download-div'
    const sBody = $('body')
    let sRoot = $('#' + sRootSelector)

    if (sRoot.length) {
        return
    }

    $('<div id="' + sRootSelector + '"><button>视频处理中..</button></div>').appendTo(sBody)

    const sDownload = $('#' + sRootSelector + ' button')

    // console.log(sDownload)

    sDownload.prop('disabled', true)

    let _sourceBufferItems = [] // 资源缓存(数组)

    // 下载资源 (函数)
    const download = () => {
        console.log(_sourceBufferItems, _sourceBufferItems.length)

        $.each(_sourceBufferItems, (key, target) => {
            let mime = target.mime.split(';')[0]
            const type = mime.split('/')[1]

            // 创建一个Blob对象,并设置文件的 MIME 类型
            const fileBlob = new Blob(target.bufferItems, { type: mime })

            const a = $('a')[0]

            mime = mime.split('/')[0]

            // a.download      =
            // a.href          = URL.createObjectURL(fileBlob)
            // a.style.display = 'none'
            // a.className = ''
            //
            // $(a).appendTo('body')

            GM_download(URL.createObjectURL(fileBlob),  `${document.title}-${mime}.${type}`) // 分片下载

            // console.log(a)



            // a.click().remove()

        })

        _sourceBufferItems = [] // 这里新增的

        return false
    }

    // 监听资源全部录取成功
    let _endOfStream = window.MediaSource.prototype.endOfStream
    console.log('window.MediaSource', window.MediaSource)

    window.MediaSource.prototype.endOfStream = function () {
        console.log('资源全部捕获成功!') // 这里不进行下载, 使用按钮点击下载

        sDownload.html(sDownload.html() + ' 可以下载了').prop('disabled', false)

        _endOfStream.call(this)
    }

    // 录取资源
    let _addSourceBuffer = window.MediaSource.prototype.addSourceBuffer

    window.MediaSource.prototype.addSourceBuffer = function(mime) {
        console.log('addSourceBuffer')

        let sourceBuffer = _addSourceBuffer.call(this, mime)
        let _append = sourceBuffer.appendBuffer
        let bufferItems = []

        _sourceBufferItems.push({
            mime,
            bufferItems,
        })

        sourceBuffer.appendBuffer = function(buffer) {
            console.log('appendBuffer')

            sDownload.html(`已捕获 ${_sourceBufferItems[0].bufferItems.length} 个片段`).prop('disabled', true)

            bufferItems.push(buffer)
            _append.call(this, buffer)
        }

        console.log('sourceBuffer', sourceBuffer)

        return sourceBuffer
    }

    $(document).on('click', '#' + sRootSelector + ' button', (e) => {
        console.log('click-download-button')

        download()

        return false
    })

})})();

感觉并非完全可行。。可能会出现检测问题而导致下载不完整

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CY3761

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值