Compare commits

...

3 Commits

Author SHA1 Message Date
49dae65bca update 2023-05-28 18:09:23 +08:00
8472b29247 update 2023-01-28 17:50:46 +08:00
881037a195 update README 2022-12-11 23:53:49 +08:00
2 changed files with 93 additions and 86 deletions

View File

@ -21,23 +21,24 @@
## 参数 ## 参数
```javascript ```javascript
//自动快进 //快进抓取
evanEnv.enableAutoForward = true jsMedia.setSpeed(10);
//完成后自动下载 //完成后自动下载
evanEnv.enableAutoDownload = true jsMedia.enableAutoDownload = true;
//手工触发下载
jsMedia.download();
``` ```
## FFmpeg合并示例 ## FFmpeg合并示例
```shell ```shell
ffmpeg -i mediasource_0_0_0.mp4 -i mediasource_0_1_0.mp4 -c copy -bsf:a aac_adtstoasc out.mp4 ffmpeg -i audio.mp4 -acodec copy audio.aac
ffmpeg -i audio.aac -i video.mp4 -c copy -bsf:a aac_adtstoasc out.mp4
``` ```
## 大文件分段需要先文件合并 ## 大文件分段需要先文件合并
copy /b mediasource_0_0_*.mp4 out.mp4 ```
copy /b media_0_*.mp4 out.mp4
## 常见问题 ```
- 合并后声音播放不正常:由于部分网站使用了HE-AAC v2(AAC+SBR+PS)作为音频编码,大部分播放器还不支持此音频流格式,故声音不正常,使用Windows自带的视频播放器播放正常
## 特别说明 ## 特别说明
- 仅限内部交流使用,请勿用于非法用途 - 仅限内部交流使用,请勿用于非法用途

View File

@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name Universal encrypted video downloader // @name Universal encrypted video downloader
// @namespace http://tampermonkey.net/ // @namespace http://tampermonkey.net/
// @version 0.1 // @version 1.1
// @description Bypass the encrypted video download protection via js hook // @description Bypass the encrypted video download protection via js hook
// @author Evan // @author Evan
// @match https://www.bilibili.com/video/* // @match https://www.bilibili.com/video/*
@ -12,68 +12,84 @@
// @grant none // @grant none
// ==/UserScript== // ==/UserScript==
evanEnv = new Object(); jsMedia = new Object();
evanEnv.mediaSources = []; jsMedia.mediaSources = [];
evanEnv.enableAutoForward = true; jsMedia.enableAutoDownload = true;
evanEnv.enableAutoDownload = true; //1G
jsMedia.fragmentSize = 1024*1024*1024;
(function () { (function () {
evanEnv.videos = document.getElementsByTagName('video'); let mediaSourceSeq = 0;
evanEnv.downloadRawData = function (bufferList, fileName = 'out', ext = '.mp4') { jsMedia.videos = document.getElementsByTagName('video');
var buffList = new Array(); jsMedia.addZero = function(num){
var totalLength = 0; return ('00' + num).slice(-3);
};
for (var i = 0; i < bufferList.length; i++) { jsMedia.downloadBlob = function (buff, fileName) {
totalLength += bufferList[i].byteLength; let url = window.URL.createObjectURL(new Blob(buff, { type: "arraybuffer" }))
if (totalLength > 1073741824 || i == bufferList.length - 1) { const link = document.createElement('a');
buffList.push(new Uint8Array(totalLength)); link.style.display = 'none';
totalLength = 0; link.href = url;
link.setAttribute('download', fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
jsMedia.calcBlobLength = function (buffList) {
let totalLength = 0;
for (let i = 0; i < buffList.length; i++) {
totalLength = totalLength + buffList[i].byteLength;
}
return totalLength;
};
jsMedia.MyMediaSourceBuffer = function () {
this.id = 0;
this.rawData1 = [];
this.rawData2 = [];
let currentSlot = 0;
let downloadedIndex = 0;
this.download = function () {
let lastSlot = currentSlot;
if (currentSlot === 0) {
currentSlot = 1;
} else {
currentSlot = 0;
} }
}
var lastLength = 0; if (lastSlot == 0) {
var part = 0; jsMedia.downloadBlob(this.rawData1, 'media_' + this.id + '_' + jsMedia.addZero(downloadedIndex) + '.mp4');
for (var ix = 0; ix < bufferList.length; ix++) { this.rawData1 = [];
//10485760 } else {
//1073741824 jsMedia.downloadBlob(this.rawData2, 'media_' + this.id + '_' + jsMedia.addZero(downloadedIndex) + '.mp4');
this.rawData2 = [];
buffList[part].set(new Uint8Array(bufferList[ix]), lastLength); }
lastLength += bufferList[ix].byteLength; downloadedIndex++;
if (lastLength > 1073741824) { };
part++; this.appendBuffer = function (buf) {
lastLength = 0 let totalLength = 0;
if (currentSlot === 0) {
this.rawData1.push(buf);
totalLength = jsMedia.calcBlobLength(this.rawData1);
} else {
this.rawData2.push(buf);
totalLength = jsMedia.calcBlobLength(this.rawData2);
}
if (totalLength >= jsMedia.fragmentSize) {
this.download();
} }
}
for (var iz = 0; iz < buffList.length; iz++) { };
var buff = buffList[iz];
let url = window.URL.createObjectURL(new Blob([buff], { type: "arraybuffer" }))
const link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', fileName + "_" + iz + ext);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}; };
evanEnv.download = function () { jsMedia.download = function(){
for (var i = 0; i < evanEnv.mediaSources.length; i++) { for (let i = 0, length = jsMedia.mediaSources.length; i < length; i++) {
var currentItem = evanEnv.mediaSources[i]; jsMedia.mediaSources[i].download();
if (currentItem.downloaded == undefined || currentItem.downloaded == false) {
for (var x = 0; x < currentItem.rawSources.length; x++) {
evanEnv.downloadRawData(currentItem.rawSources[x], 'mediasource_' + i + '_' + x);
}
}
currentItem.downloaded = true;
} }
}; };
evanEnv.downloadMediaSource = function (mediaSource) {
for (var x = 0; x < mediaSource.rawSources.length; x++) { jsMedia.setSpeed = function (speed) {
evanEnv.downloadRawData(mediaSource.rawSources[x], 'mediasource_' + x); for (let i = 0, length = jsMedia.videos.length; i < length; i++) {
mediaSource.downloaded = true; jsMedia.videos[i].playbackRate = speed;
} }
}; };
@ -81,41 +97,31 @@ evanEnv.enableAutoDownload = true;
MediaSource.prototype.addSourceBuffer = function (mimeType) { MediaSource.prototype.addSourceBuffer = function (mimeType) {
var mediaSourceThis = this; var mediaSourceThis = this;
evanEnv.mediaSources.push(this);
this.lastPos = 0;
if (evanEnv.enableAutoDownload) {
this.onsourceended = function () {
evanEnv.download();
};
this.onsourceclose = function () {
evanEnv.downloadMediaSource(this);
};
}
var retSourceBuffer = proxy_addSourceBuffer.apply(this, [].slice.call(arguments)); var retSourceBuffer = proxy_addSourceBuffer.apply(this, [].slice.call(arguments));
if (this.rawSources == undefined) { if (this.rawSources == undefined) {
this.rawSources = []; this.rawSources = [];
} }
var buffList = [];
this.rawSources.push(buffList); retSourceBuffer.mySourceBuffer = new jsMedia.MyMediaSourceBuffer();
retSourceBuffer.rawData = buffList; retSourceBuffer.mySourceBuffer.id = mediaSourceSeq;
if (evanEnv.enableAutoForward) { jsMedia.mediaSources.push(retSourceBuffer.mySourceBuffer);
retSourceBuffer.onupdate = function () {
if (this.buffered != this.buffered.length > 0) {
var endPos = this.buffered.end(0); if (jsMedia.enableAutoDownload) {
if (endPos > 5 && endPos - mediaSourceThis.lastPos > 25) { this.onsourceended = function () {
evanEnv.videos[0].currentTime = endPos - 3; jsMedia.download();
mediaSourceThis.lastPos = evanEnv.videos[0].currentTime; };
} this.onsourceclose = function () {
} jsMedia.download();
}; };
} }
mediaSourceSeq++;
return retSourceBuffer; return retSourceBuffer;
}; };
var proxy_appendBuffer = SourceBuffer.prototype.appendBuffer; var proxy_appendBuffer = SourceBuffer.prototype.appendBuffer;
SourceBuffer.prototype.appendBuffer = function (buf) { SourceBuffer.prototype.appendBuffer = function (buf) {
this.rawData.push(buf); this.mySourceBuffer.appendBuffer(buf);
return proxy_appendBuffer.apply(this, [].slice.call(arguments)); return proxy_appendBuffer.apply(this, [].slice.call(arguments));
}; };
})(); })();