Spaces:
Running
Running
JOHN SILLVESTOR commited on
Update Node HTTP service
Browse files
server.js
CHANGED
|
@@ -483,27 +483,39 @@ async function fileFromPool(msgId, prefer, skipIdx) {
|
|
| 483 |
}
|
| 484 |
|
| 485 |
function durationFromMp4(buf) {
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
}
|
|
|
|
| 502 |
}
|
| 503 |
|
| 504 |
async function peekDuration(bot, rec) {
|
| 505 |
if (!bot || !rec || !rec.size) return 0;
|
| 506 |
-
const take = Math.min(rec.size,
|
| 507 |
async function readSpan(start, end) {
|
| 508 |
let acc = Buffer.alloc(0);
|
| 509 |
for await (const c of rangeChunks(bot, rec, start, end, () => acc.length >= take)) {
|
|
@@ -700,7 +712,11 @@ async function handleGenerate(req, res, url) {
|
|
| 700 |
const rec = got.rec;
|
| 701 |
if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
|
| 702 |
const cachedDur = durCache.get(String(msgId));
|
| 703 |
-
if (cachedDur) rec.duration = cachedDur;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 705 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 706 |
const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime });
|
|
@@ -714,7 +730,6 @@ async function handleGenerate(req, res, url) {
|
|
| 714 |
accessLevel: lvl,
|
| 715 |
expiry: exp
|
| 716 |
});
|
| 717 |
-
if (!rec.duration) fillDuration(bot, rec, msgId).catch(function () {});
|
| 718 |
}
|
| 719 |
|
| 720 |
async function handleStream(req, res, url, msgId) {
|
|
|
|
| 483 |
}
|
| 484 |
|
| 485 |
function durationFromMp4(buf) {
|
| 486 |
+
if (!buf || buf.length < 32) return 0;
|
| 487 |
+
const tag = Buffer.from("mvhd");
|
| 488 |
+
let from = 0;
|
| 489 |
+
while (from + 28 < buf.length) {
|
| 490 |
+
const i = buf.indexOf(tag, from);
|
| 491 |
+
if (i < 4 || i + 24 > buf.length) break;
|
| 492 |
+
const ver = buf[i + 4];
|
| 493 |
+
try {
|
| 494 |
+
if (ver === 1) {
|
| 495 |
+
if (i + 36 > buf.length) break;
|
| 496 |
+
const timescale = buf.readUInt32BE(i + 24);
|
| 497 |
+
const duration = Number(buf.readBigUInt64BE(i + 28));
|
| 498 |
+
if (timescale > 0 && duration > 0) {
|
| 499 |
+
const s = duration / timescale;
|
| 500 |
+
if (s > 1 && s < 86400) return s;
|
| 501 |
+
}
|
| 502 |
+
} else if (ver === 0) {
|
| 503 |
+
const timescale = buf.readUInt32BE(i + 16);
|
| 504 |
+
const duration = buf.readUInt32BE(i + 20);
|
| 505 |
+
if (timescale > 0 && duration > 0) {
|
| 506 |
+
const s = duration / timescale;
|
| 507 |
+
if (s > 1 && s < 86400) return s;
|
| 508 |
+
}
|
| 509 |
+
}
|
| 510 |
+
} catch {}
|
| 511 |
+
from = i + 4;
|
| 512 |
}
|
| 513 |
+
return 0;
|
| 514 |
}
|
| 515 |
|
| 516 |
async function peekDuration(bot, rec) {
|
| 517 |
if (!bot || !rec || !rec.size) return 0;
|
| 518 |
+
const take = Math.min(rec.size, MB);
|
| 519 |
async function readSpan(start, end) {
|
| 520 |
let acc = Buffer.alloc(0);
|
| 521 |
for await (const c of rangeChunks(bot, rec, start, end, () => acc.length >= take)) {
|
|
|
|
| 712 |
const rec = got.rec;
|
| 713 |
if (rec.hash && rec.hash !== hash) return json(res, 400, { error: "hash" });
|
| 714 |
const cachedDur = durCache.get(String(msgId));
|
| 715 |
+
if (cachedDur > 1) rec.duration = cachedDur;
|
| 716 |
+
if (!(rec.duration > 1)) {
|
| 717 |
+
const d = await withTime(fillDuration(bot, rec, msgId), 3500, 0);
|
| 718 |
+
if (d > 1) rec.duration = d;
|
| 719 |
+
}
|
| 720 |
const playMime = httpVideoType(rec.mime, rec.name);
|
| 721 |
const exp = Math.floor(Date.now() / 1000) + TOKEN_TTL;
|
| 722 |
const k = sealTicket({ m: msgId, h: hash, u: uid || "anon", l: lvl, b: bot.idx, e: exp, s: rec.size, n: playMime });
|
|
|
|
| 730 |
accessLevel: lvl,
|
| 731 |
expiry: exp
|
| 732 |
});
|
|
|
|
| 733 |
}
|
| 734 |
|
| 735 |
async function handleStream(req, res, url, msgId) {
|