[Java 转载]bilibili音视频下载脚本(油猴)

[Java 转载]bilibili音视频下载脚本(油猴)JavaScript UserScript name B 站视频下载器 稳定版 namespace http tampermonkey net version 3 0 description 稳定下载 B 站音视频 提供本地合并指南 AuThor You match https www bilibili

大家好,我是讯享网,很高兴认识大家。这里提供最前沿的Ai技术和互联网信息。



[JavaScript]
// ==UserScript== // @name B站视频下载器(稳定版) // @namespace http://tampermonkey.net/ // @version 3.0 // @description 稳定下载B站音视频,提供本地合并指南 // @AuThor You // @match https://www.bilibili.com/video/* // @match https://www.bilibili.com/bangumi/play/* // @grant none // @run-at document-end // ==/UserScript==

(function() {

'use strict'; console.log(' B站视频下载器启动 - 稳定版'); class BilibiliDownloader { constructor() { this.playInfo = null; this.init(); } init() { this.createDownloadButton(); this.findPlayInfo(); this.addGlobalStyles(); } // 添加全局样式 addGlobalStyles() .bilibili-downloader-btn:hover { transform: scale(1.05) !important; background: #0087b3 !important; } .bilibili-downloader-panel .bilibili-downloader-panel h3 { color: #00a1d6 !important; margin: 0 0 15px 0 !important; text-align: center !important; } .bilibili-downloader-panel .close-btn .bilibili-downloader-panel .close-btn:hover { background: rgba(255,255,255,0.1) !important; } .bilibili-downloader-panel .section { margin-bottom: 20px !important; } .bilibili-downloader-panel .section-title { color: #ffa500 !important; font-weight: bold !important; margin-bottom: 10px !important; font-size: 14px !important; } .bilibili-downloader-panel .file-item { background: rgba(255,255,255,0.05) !important; padding: 10px !important; margin-bottom: 8px !important; border-radius: 5px !important; border-left: 3px solid #00a1d6 !important; } .bilibili-downloader-panel .file-info .bilibili-downloader-panel .quality-badge { background: #ffa500 !important; color: black !important; padding: 2px 6px !important; border-radius: 3px !important; font-size: 10px !important; font-weight: bold !important; } .bilibili-downloader-panel .download-btn { background: #27ae60 !important; color: white !important; border: none !important; padding: 5px 10px !important; border-radius: 3px !important; cursor: pointer !important; font-size: 12px !important; } .bilibili-downloader-panel .download-btn:hover { background: # !important; } .bilibili-downloader-panel .merge-guide { background: rgba(255,255,255,0.1) !important; padding: 15px !important; border-radius: 5px !important; margin-top: 20px !important; font-size: 12px !important; } .bilibili-downloader-panel .code-block { background: #1a1a1a !important; color: #00ff00 !important; padding: 10px !important; border-radius: 3px !important; font-family: 'Courier New', monospace !important; font-size: 11px !important; margin-top: 8px !important; overflow-x: auto !important; } `; document.head.appendChild(style); } // 创建下载按钮 createDownloadButton() { const button = document.createElement('button'); button.className = 'bilibili-downloader-btn'; button.innerHTML = ' 下载视频'; button.title = 'B站视频下载工具'; button.onclick = () => this.showDownloadPanel(); document.body.appendChild(button); console.log('✅ 下载按钮创建成功'); } // 显示下载面板 showDownloadPanel() const panel = document.createElement('div'); panel.id = 'bilibili-downloader-panel'; panel.className = 'bilibili-downloader-panel'; panel.innerHTML = this.generatePanelHTML(); document.body.appendChild(panel); this.bindPanelEvents(panel); console.log('✅ 下载面板创建成功'); } // 生成面板HTML generatePanelHTML() 个) ${videos.length > 0 ? this.generateFileListHTML(videos, 'video') : ' 
  
    
    
暂无视频数据
'}
 音频流 (${audios.length}个)
${audios.length > 0 ? this.generateFileListHTML(audios, 'audio') : '
暂无音频数据
'}
$
 稳定可靠 · 无需额外依赖
`; } // 生成文件列表HTML generateFileListHTML(files, type) { return files.map((file, index) => { const quality = file.id || '未知'; const codecs = file.codecs || '未知编码'; const bandwidth = file.bandwidth ? ` (${Math.round(file.bandwidth/1000)}kbps)` : ''; const url = file.baseUrl || file.base_url || ''; return `
${quality}P ${codecs}${bandwidth}
${url.substring(0, 80)}...
`; }).join(''); } // 绑定面板事件 bindPanelEvents(panel) ; }); } // 下载文件 downloadFile(type, index) const file = files[index]; const url = file.baseUrl || file.base_url; if (!url) const extension = type === 'video' ? '.mp4' : '.m4a'; const filename = this.generateFilename(file, type, index, extension); this.startDownload(url, filename); this.showNotification(`✅ 开始下载: ${filename}`, 'success'); } // 生成文件名 generateFilename(file, type, index, extension) } catch (e) { console.log('获取标题失败,使用默认名称'); } return `${title}_${type}_${file.id || index}${extension}`; } // 开始下载 startDownload(url, filename) { const link = document.createElement('a'); link.href = url; link.download = filename; link.style.display = 'none'; document.body.appendChild(link); link.click(); document.body.removeChild(link); console.log(`⬇️ 下载: ${filename}`); } // 查找playinfo数据(增强版) findPlayInfo() { console.log(' 开始查找视频数据...'); let attempts = 0; const maxAttempts = 8; const search = () => { attempts++; console.log(` 第${attempts}次查找...`); // 方法1: 直接检查window对象 if (window.__playinfo__) { console.log('✅ 从window.__playinfo__找到数据'); this.playInfo = window.__playinfo__; this.onPlayInfoFound(); return; } // 方法2: 搜索所有script标签 const scripts = document.querySelectorAll('script'); console.log(` 扫描 ${scripts.length} 个script标签`); for (let i = 0; i < scripts.length; i++) 个script标签中找到目标`); const patterns = [ /window.__playinfo__s*=s*({[sS]*?})s*;/, //, /window.__playinfo__s*=s*({.*?})(?=window.__||$)/ ]; for (let j = 0; j < patterns.length; j++) 匹配成功`); try { this.playInfo = JSON.parse(match[1]); console.log('✅ JSON解析成功'); this.onPlayInfoFound(); return; } catch (error) { console.error('❌ JSON解析错误,尝试修复...'); // 尝试修复JSON try { const fixed = this.fixJson(match[1]); this.playInfo = JSON.parse(fixed); console.log(' 修复后解析成功'); this.onPlayInfoFound(); return; } catch (e2) { console.error('❌ 修复后仍然解析失败'); continue; } } } } } } // 如果没找到,准备重试 if (attempts < maxAttempts) { const delay = attempts * 1000; console.log(`⏳ 未找到数据,${delay}ms后重试...`); setTimeout(search, delay); } else }; // 延迟启动,确保页面加载完成 setTimeout(search, 1000); } // 修复JSON格式 fixJson(jsonStr) { return jsonStr .replace(/'/g, '"') // 单引号转双引号 .replace(/([a-zA-Z_$][a-zA-Z0-9_$]*):/g, '"$1":') // 属性名加引号 .replace(/,s*}/g, '}') // 移除尾随逗号 .replace(/,s*]/g, ']') // 移除数组尾随逗号 .replace(///.*$/gm, '') // 移除单行注释 .replace(//*[sS]*?*//g, ''); // 移除多行注释 } // 找到数据后的处理 onPlayInfoFound() // 显示成功通知 this.showNotification('✅ 视频数据加载完成,可以下载了', 'success'); } // 获取视频流 getVideos() // 获取音频流 getAudios() // 显示通知 showNotification(message, type = 'info') { const colors = { info: '#3498db', success: '#27ae60', error: '#e74c3c', warning: '#f39c12' }; const notification = document.createElement('div'); notification.innerHTML = message; notification.style.cssText = ` position: fixed; top: 20px; left: 50%; transform: translateX(-50%); background: ${colors[type]}; color: white; padding: 10px 20px; border-radius: 5px; z-index: 10002; font-family: Arial, sans-serif; box-shadow: 0 2px 10px rgba(0,0,0,0.3); max-width: 80vw; text-align: center; `; document.body.appendChild(notification); setTimeout(() => }, 3000); } } // 启动下载器 setTimeout(() => { console.log(' 启动B站视频下载器...'); window.bilibiliDownloader = new BilibiliDownloader(); }, 1000);

})();

小讯
上一篇 2026-04-19 12:20
下一篇 2026-04-19 12:18

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/266090.html