Created
November 19, 2023 14:02
-
-
Save ivansky/3af5159c532677738acc8ecc4c05aee2 to your computer and use it in GitHub Desktop.
web telegram save cached video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// web telegram stream video from cache | |
// it probably uses shared/webworkers to save bytes chunks into cache storage | |
// so once video is fully cached it could be saved from cache to device | |
// browser could ask to allow parallel files downloads from web telegram domain, just allow it | |
(async function downloadCachedVideos() { | |
// global downloaded set is to prevent downloading videos were alreadt downloaded | |
window.downloaded = window.downloaded || new Set([]) | |
function saveVideo(outname, dataArray) { | |
console.log(`Start downloading ${outname} with ${dataArray.length} chunks`) | |
var blob = new Blob(dataArray, {type: "video/mp4"}); | |
var link = document.createElement('a'); | |
link.href = URL.createObjectURL(blob); | |
var fileName = outname; | |
link.download = fileName; | |
link.click(); | |
URL.revokeObjectURL(link.href) | |
delete link | |
} | |
const cache = await caches.open('cachedStreamChunks'); | |
const requests = await cache.keys(); | |
function getOffset(r) { | |
return parseInt(new URL(r.url).searchParams.get('offset'), 10) | |
} | |
function getPathNum(r) { | |
return new URL(r.url).pathname.slice(1) | |
} | |
requests.sort((a, b) => { | |
const [oA, oB] = [getOffset(a), getOffset(b)] | |
return oA < oB ? -1 : oA > oB ? 1 : 0; | |
}) | |
/** @type {Object.<number, Blob>} */ | |
const bts = {}; | |
for (const req of requests) { | |
const id = getPathNum(req) | |
if (window.downloaded.has(String(id))) continue; | |
bts[id] = bts[id] || [] | |
const response = await cache.match(req) | |
if (!response) continue; | |
bts[id].push(await response.blob()) | |
} | |
for (const [id, blobsArray] of Object.entries(bts)) { | |
window.downloaded.add(String(id)) | |
saveVideo(id+'.mp4',blobsArray) | |
} | |
})() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment