diff --git a/README.md b/README.md index 0d9d407..e9fc25b 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,12 @@ evanEnv.enableAutoDownload = true ## FFmpeg合并示例 ```shell -ffmpeg -i mediasource_0_0.mp4 -i mediasource_0_1.mp4 -c copy -bsf:a aac_adtstoasc out.mp4 +ffmpeg -i mediasource_0_0_0.mp4 -i mediasource_0_1_0.mp4 -c copy -bsf:a aac_adtstoasc out.mp4 ``` +## 大文件分段需要先文件合并 +copy /b mediasource_0_0_*.mp4 out.mp4 + ## 常见问题 - 合并后声音播放不正常:由于部分网站使用了HE-AAC v2(AAC+SBR+PS)作为音频编码,大部分播放器还不支持此音频流格式,故声音不正常,使用Windows自带的视频播放器播放正常 diff --git a/downloadMedia.js b/downloadMedia.js index 11b80c6..d907510 100644 --- a/downloadMedia.js +++ b/downloadMedia.js @@ -19,29 +19,44 @@ evanEnv.enableAutoDownload = true; (function () { evanEnv.videos = document.getElementsByTagName('video'); - evanEnv.buildArrayBuffer = function (bufferList) { + evanEnv.downloadRawData = function (bufferList, fileName = 'out', ext = '.mp4') { + var buffList = new Array(); var totalLength = 0; + for (var i = 0; i < bufferList.length; i++) { totalLength += bufferList[i].byteLength; + if (totalLength > 1073741824 || i == bufferList.length - 1) { + buffList.push(new Uint8Array(totalLength)); + totalLength = 0; + } } - var tmp = new Uint8Array(totalLength); + var lastLength = 0; + var part = 0; for (var ix = 0; ix < bufferList.length; ix++) { - tmp.set(new Uint8Array(bufferList[ix]), lastLength); + //10485760 + //1073741824 + + buffList[part].set(new Uint8Array(bufferList[ix]), lastLength); lastLength += bufferList[ix].byteLength; + if (lastLength > 1073741824) { + part++; + lastLength = 0 + } } - return tmp.buffer; - }; - evanEnv.downloadRawData = function (bufferList, fileName = 'out.mp4') { - var buff = evanEnv.buildArrayBuffer(bufferList); - 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); - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); + + 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 () { @@ -49,7 +64,7 @@ evanEnv.enableAutoDownload = true; var currentItem = evanEnv.mediaSources[i]; if (currentItem.downloaded == undefined || currentItem.downloaded == false) { for (var x = 0; x < currentItem.rawSources.length; x++) { - evanEnv.downloadRawData(currentItem.rawSources[x], 'mediasource_' + i + '_' + x + '.mp4'); + evanEnv.downloadRawData(currentItem.rawSources[x], 'mediasource_' + i + '_' + x); } } currentItem.downloaded = true; @@ -57,7 +72,7 @@ evanEnv.enableAutoDownload = true; }; evanEnv.downloadMediaSource = function (mediaSource) { for (var x = 0; x < mediaSource.rawSources.length; x++) { - evanEnv.downloadRawData(mediaSource.rawSources[x], 'mediasource_' + x + '.mp4'); + evanEnv.downloadRawData(mediaSource.rawSources[x], 'mediasource_' + x); mediaSource.downloaded = true; } };