Skip to content

Commit eaaca9d

Browse files
committed
youtube local downloader - WIP
1 parent 379de63 commit eaaca9d

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

scripts/content-scripts/scripts/ufs_global_webpage_context.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ const UsefulScriptGlobalPageContext = {
358358
}
359359
},
360360
async downloadBlobUrlWithProgress(url, progressCallback) {
361-
const response = await fetch(url);
361+
const response = await fetch(url, {});
362362
if (!response.ok) {
363363
throw new Error(`Error: ${response.status} - ${response.statusText}`);
364364
}
@@ -368,11 +368,17 @@ const UsefulScriptGlobalPageContext = {
368368
const reader = response.body.getReader();
369369
const chunks = [];
370370

371+
const startTime = Date.now();
371372
while (true) {
372373
const { done, value } = await reader.read();
373374
if (done) break;
374375
loaded += value.byteLength;
375-
progressCallback?.(loaded, total);
376+
const ds = (Date.now() - startTime + 1) / 1000;
377+
progressCallback?.({
378+
loaded,
379+
total,
380+
speed: loaded / ds,
381+
});
376382
chunks.push(value);
377383
}
378384

@@ -381,7 +387,6 @@ const UsefulScriptGlobalPageContext = {
381387
});
382388

383389
return blob;
384-
UsefulScriptGlobalPageContext.Utils.downloadBlob(blob, fileName);
385390
},
386391
async downloadBlobUrl(url, title) {
387392
try {

scripts/youtube_localDownloader.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,51 @@ export default {
164164
"https://unpkg.com/vue@2.6.10/dist/vue.js"
165165
);
166166

167+
async function downloadBlobUrlWithProgressMultiChunk(
168+
url,
169+
progressCallback,
170+
chunkSize = 65536
171+
) {
172+
const response = await fetch(url);
173+
if (!response.ok) {
174+
throw new Error(`Error: ${response.status} - ${response.statusText}`);
175+
}
176+
const contentLength = response.headers.get("content-length");
177+
const total = parseInt(contentLength, 10);
178+
let loaded = 0;
179+
const startTime = Date.now();
180+
181+
const chunks = [];
182+
const numChunks = Math.ceil(total / chunkSize);
183+
const promises = [];
184+
185+
for (let i = 0; i < numChunks; i++) {
186+
const start = i * chunkSize;
187+
const end = Math.min(start + chunkSize - 1, total - 1);
188+
promises.push(
189+
fetch(url + `&range=${start}-${end}`).then((res) => res.blob())
190+
);
191+
}
192+
193+
await Promise.all(promises)
194+
.then((downloadedChunks) => {
195+
chunks.push(...downloadedChunks);
196+
loaded = total;
197+
progressCallback?.({
198+
loaded,
199+
total,
200+
speed: loaded / ((Date.now() - startTime + 1) / 1000),
201+
});
202+
})
203+
.catch((error) => {
204+
console.error("Download error:", error);
205+
});
206+
207+
return new Blob(chunks, {
208+
type: response.headers.get("content-type"),
209+
});
210+
}
211+
167212
const xhrDownloadUint8Array = async (
168213
{ url, contentLength },
169214
progressCb
@@ -323,6 +368,25 @@ export default {
323368
this.audio.total = (e.total / 1024 / 1024).toFixed(2);
324369
this.audio.speed = (e.speed / 1024).toFixed(2);
325370
});
371+
372+
// const vPromise = downloadBlobUrlWithProgressMultiChunk(
373+
// videoObj.url,
374+
// ({ loaded, total, speed }) => {
375+
// this.video.progress = (loaded / total) * 100;
376+
// this.video.loaded = (loaded / 1024 / 1024).toFixed(2);
377+
// this.video.total = (total / 1024 / 1024).toFixed(2);
378+
// this.video.speed = (speed / 1024).toFixed(2);
379+
// }
380+
// );
381+
// const aPromise = downloadBlobUrlWithProgressMultiChunk(
382+
// audioObj.url,
383+
// ({ loaded, total, speed }) => {
384+
// this.audio.progress = (loaded / total) * 100;
385+
// this.audio.loaded = (loaded / 1024 / 1024).toFixed(2);
386+
// this.audio.total = (total / 1024 / 1024).toFixed(2);
387+
// this.audio.speed = (speed / 1024).toFixed(2);
388+
// }
389+
// );
326390
const [varr, aarr] = await Promise.all([vPromise, aPromise]);
327391
this.merging = true;
328392
win.onunload = () => {

0 commit comments

Comments
 (0)