-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.min.js
1 lines (1 loc) · 5.52 KB
/
main.min.js
1
(function () {var downloading=!1,durationSecond=0,finishList=[],tsUrlList=[],aesConf={method:"",uri:"",iv:"",key:"",decryptor:null,stringToBuffer:function(e){return new TextEncoder().encode(e)}},beginTime="",isPause=!1,downloadIndex=0,errorNum=0,errorList=[],mediaFileList=[],finishNum=0,isComplete=!1;function alertError(e){resetVars(),alert(e)}function applyURL(e,t){if(t=t||location.href,0===e.indexOf("http"))return e;if("/"===e[0]){let n=t.split("/");return n[0]+"//"+n[2]+e}{let r=t.split("/");return r.pop(),r.join("/")+"/"+e}}function ajax(e){e=e||{};let t=new XMLHttpRequest;"file"===e.type&&(t.responseType="arraybuffer"),t.onreadystatechange=function(){if(4===t.readyState){let n=t.status;n>=200&&n<300?e.success&&e.success(t.response):e.fail&&e.fail(n)}},t.open("GET",e.url,!0),t.send(null)}function getM3U8(e,t){if(downloading){alert("Alredy downloading, please wait...");return}downloading=!0,beginTime=new Date,ajax({url:e,success(n){tsUrlList=[],n.split("\n").forEach(t=>{t.toUpperCase().indexOf("#EXTINF:")>-1&&(durationSecond+=parseFloat(t.split("#EXTINF:")[1])),t.toLowerCase().indexOf(".ts")>-1&&(tsUrlList.push(applyURL(t,e)),finishList.push({title:t,status:""}))}),n.indexOf("#EXT-X-KEY")>-1?(aesConf.method=(n.match(/(.*METHOD=([^,\s]+))/)||["","",""])[2],aesConf.uri=(n.match(/(.*URI="([^"]+))"/)||["","",""])[2],aesConf.iv=(n.match(/(.*IV=([^,\s]+))/)||["","",""])[2],aesConf.iv=aesConf.iv?aesConf.stringToBuffer(aesConf.iv):"",aesConf.uri=applyURL(aesConf.uri,e),getAES()):tsUrlList.length>0?downloadTS(t):alertError("The resource is empty, please check whether the link is valid")},fail(){alertError("The resource is empty, please check whether the link is valid")}})}function getAES(){ajax({type:"file",url:aesConf.uri,success(e){aesConf.key=e,aesConf.decryptor=new window.AESDecryptor,aesConf.decryptor.constructor(),aesConf.decryptor.expandKey(aesConf.key),downloadTS()},fail(){alertError("AES error")}})}function downloadTS(e){let t=()=>{let n=isPause,r=downloadIndex;downloadIndex++,finishList[r]&&""===finishList[r].status?ajax({url:tsUrlList[r],type:"file",success(i){dealTS(i,r,e,function(){downloadIndex<tsUrlList.length&&!n&&t()})},fail(){errorNum++,finishList[r].status="error",finishList[r].errorIndex=r,errorList.push(finishList[r]),downloadIndex<tsUrlList.length&&!n&&t()}}):downloadIndex<tsUrlList.length&&!n&&t()};for(let n=0;n<10;n++)t()}function aesDecrypt(e,t){let n=aesConf.iv||new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,t]);return aesConf.decryptor.decrypt(e,0,n.buffer||n,!0)}function dealTS(e,t,n,r){let i=aesConf.uri?aesDecrypt(e,t):e;conversionMp4(i,t,e=>{if(mediaFileList[t]=e,finishList[t].status="finish",++finishNum===tsUrlList.length){downloadFile(mediaFileList,formatTime(beginTime,"YYYY_MM_DD hh_mm_ss"));let i=n.innerHTML;n.innerHTML="Done!",setTimeout(()=>{n.innerHTML=i,n.style.background="transparent"},3e3)}else{let s=Math.ceil(finishNum/tsUrlList.length*100);n.style.background="linear-gradient(to right, #00bf00 "+s+"%, transparent 0%)"}r&&r()})}function formatTime(e,t){let n={Y:e.getFullYear(),M:e.getMonth()+1,D:e.getDate(),h:e.getHours(),m:e.getMinutes(),s:e.getSeconds()};return t.replace(/Y+|M+|D+|h+|m+|s+/g,e=>(Array(e.length).join("0")+n[e[0]]).substr(-e.length))}function conversionMp4(e,t,n){let r=new window.muxjs.Transmuxer({keepOriginalTimestamps:!0,duration:parseInt(durationSecond)});r.on("data",e=>{if(0===t){let r=new Uint8Array(e.initSegment.byteLength+e.data.byteLength);r.set(e.initSegment,0),r.set(e.data,e.initSegment.byteLength),n(r.buffer)}else n(e.data)}),r.push(new Uint8Array(e)),r.flush()}function downloadFile(e,t){let n=null,r=document.createElement("a");n=new Blob(e,{type:"video/mp4"}),r.download=t+".mp4",r.href=URL.createObjectURL(n),r.style.display="none",document.body.appendChild(r),r.click(),r.remove(),resetVars()}function resetVars(){downloading=!1,durationSecond=0,finishList=[],tsUrlList=[],aesConf={method:"",uri:"",iv:"",key:"",decryptor:null,stringToBuffer:function(e){return new TextEncoder().encode(e)}},beginTime="",isPause=!1,downloadIndex=0,errorNum=0,errorList=[],mediaFileList=[],finishNum=0,isComplete=!1}function transformUrl(e){let t=e.indexOf("&thumbnail=");if(-1===t)return null;let n=e.substring(t+11),r=n.indexOf("/version/");if(-1===r)return null;let i=n.substring(0,r+1),s=i.replace("cfvod","cdnapisec").replace("thumbnail","playManifest").replace("entry_id","entryId")+"format/applehttp/protocol/https/a.m3u8?responseFormat=jsonp&callback=";return s.startsWith("http://")&&(s=s.replace("http://","https://")),s}async function fetchM3u8Urls(e){try{let t=await fetch(e);if(!t.ok)return console.log("Could not fetch video URL:",e),null;let n=await t.text(),r=JSON.parse(n.substring(1,n.length-2));return r}catch(i){return console.log("Error while fetching URL:",e),null}}function createDownloadButtons(e,t){let n=document.createElement("div");e.appendChild(n),n.setAttribute("style","display: flex; flex-wrap: wrap;"),t.forEach((e,t)=>{let r=document.createElement("div");n.appendChild(r);let i=e.width+"x"+e.height;r.innerHTML=i,r.setAttribute("style","border: 1px solid #ccc; padding: 10px; margin: 5px; cursor: pointer;"),r.addEventListener("click",()=>{downloading||getM3U8(e.url,r)})})}function main(){scriptUrls=["https://m3u8.dev/js/aes-decryptor.js","https://m3u8.dev/js/mux-mp4.js"];for(var e=0;e<scriptUrls.length;e++){var t=document.createElement("script");t.src=scriptUrls[e],document.head.appendChild(t)}let n=document.querySelectorAll("iframe");n.forEach(e=>{let t=e.getAttribute("src"),n=transformUrl(t);n&&fetchM3u8Urls(n).then(t=>{t&&createDownloadButtons(e.parentNode.parentNode,t.flavors)})})}main();})();