好友
阅读权限10
听众
最后登录1970-1-1
|
哔哩哔哩的比较难办,因为Dash模式的API需要用ffmpeg合并过,而解析单文件的API最高清晰度只有720P,所以比较麻烦.现在这个是dash模式的书签脚本,下载下来的是两个文件,一个是音频,一个是视频,需要自行混流.
[JavaScript] 纯文本查看 复制代码 javascript:(function(){const SCRIPT_VERSION='2.0';if(!window.location.href.match(/https:\/\/www\.bilibili\.com\/video\/(BV|av)/)){alert('请在B站视频页面使用此书签');return}if(window.bilibiliDownloaderLoaded){alert('下载工具已加载,请勿重复点击');return}window.bilibiliDownloaderLoaded=true;function init(){const hasData=window.__playinfo__||window?.__INITIAL_STATE__?.videoData||document.querySelector('[data-player]');if(!document.querySelector("#app")||!hasData){setTimeout(init,500);return}['bvm','bam','bdvd','bdl-container'].forEach(id=>{const el=document.getElementById(id);if(el)el.remove()});createDownloadUI()}function createDownloadUI(){const container=document.createElement('div');container.id='bdl-container';container.style=`position:fixed;top:100px;right:10px;z-index:10000;background:rgba(255,255,255,0.95);border:1px solid#ccc;border-radius:8px;padding:10px;box-shadow:0 2px 10px rgba(0,0,0,0.1);font-family:system-ui,-apple-system,sans-serif;min-width:150px;`;const title=document.createElement('div');title.textContent='B站下载工具';title.style='font-weight: bold; margin-bottom: 8px; border-bottom: 1px solid #eee; padding-bottom: 5px;';container.appendChild(title);const videoInfo=getVideoInfo();if(!videoInfo){alert('无法获取视频信息');return}createDownloadButton(container,'下载视频',videoInfo.videoUrl,videoInfo.bvid+'.m4v');createDownloadButton(container,'下载音频',videoInfo.audioUrl,videoInfo.bvid+'.m4a');createDownloadButton(container,'一键下载',null,null,()=>{downloadBoth(videoInfo.videoUrl,videoInfo.audioUrl,videoInfo.bvid)});const closeBtn=document.createElement('button');closeBtn.textContent='×';closeBtn.style=`position:absolute;top:5px;right:5px;background:none;border:none;font-size:16px;cursor:pointer;padding:0;width:20px;height:20px;`;closeBtn.onclick=()=>container.remove();container.appendChild(closeBtn);document.body.appendChild(container);makeDraggable(container)}function getVideoInfo(){try{const pdata=window?.__playinfo__?.data;if(!pdata?.dash){throw new Error('无法获取视频数据');}const video=pdata.dash.video?.[0];const audio=pdata.dash.audio?.[0];if(!video?.base_url||!audio?.base_url){throw new Error('视频/音频URL无效');}const bvid=window?.__INITIAL_STATE__?.bvid||window?.__INITIAL_STATE__?.videoData?.bvid||'video';return{videoUrl:video.base_url,audioUrl:audio.base_url,bvid:bvid,quality:pdata.accept_description?.[0]||'默认'}}catch(error){console.error('获取视频信息失败:',error);return null}}function createDownloadButton(container,text,url,filename,onClick){const btn=document.createElement('button');btn.textContent=text;btn.style=`display:block;width:100%;margin:5px 0;padding:8px 12px;background:#00a1d6;color:white;border:none;border-radius:4px;cursor:pointer;font-size:12px;`;btn.onclick=onClick||(()=>downloadFile(url,filename));btn.onmouseover=()=>btn.style.background='#008db2';btn.onmouseout=()=>btn.style.background='#00a1d6';container.appendChild(btn)}function downloadBoth(videoUrl,audioUrl,bvid){downloadFile(videoUrl,bvid+'.m4v');setTimeout(()=>{downloadFile(audioUrl,bvid+'.m4a')},1000)}function downloadFile(url,filename){fetch(url,{method:'GET',headers:{'User-Agent':navigator.userAgent,'Referer':location.href}}).then(response=>{if(!response.ok)throw new Error(`HTTP ${response.status}`);return response.blob()}).then(blob=>{const downloadUrl=URL.createObjectURL(blob);const a=document.createElement('a');a.href=downloadUrl;a.download=filename;document.body.appendChild(a);a.click();document.body.removeChild(a);setTimeout(()=>URL.revokeObjectURL(downloadUrl),1000)}).catch(error=>{console.error('下载失败:',error);alert('下载失败: '+error.message)})}function makeDraggable(element){let pos1=0,pos2=0,pos3=0,pos4=0;element.onmousedown=dragMouseDown;function dragMouseDown(e){e=e||window.event;e.preventDefault();pos3=e.clientX;pos4=e.clientY;document.onmouseup=closeDragElement;document.onmousemove=elementDrag}function elementDrag(e){e=e||window.event;e.preventDefault();pos1=pos3-e.clientX;pos2=pos4-e.clientY;pos3=e.clientX;pos4=e.clientY;element.style.top=(element.offsetTop-pos2)+"px";element.style.left=(element.offsetLeft-pos1)+"px"}function closeDragElement(){document.onmouseup=null;document.onmousemove=null}}const timeout=setTimeout(()=>{alert('加载超时,请刷新页面后重试')},8000);function safeInit(){clearTimeout(timeout);try{init()}catch(error){console.error('初始化失败:',error);alert('工具初始化失败: '+error.message)}}if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',safeInit)}else{safeInit()}})(); |
|