GUI-STUDIO commited on
Commit ·
227ec09
1
Parent(s): 7822c17
Add HF deps, bucket router; refactor showtime
Browse filesAdd @huggingface/hub and @huggingface/inference to package.json and introduce a new /bucket sub-router skeleton. Refactor F_handle_process in showtime.ts to improve job/download handling: normalize job_id, add typing for last_failed, track and document 'undocumented' files, centralize serve_download returns, include job_id in responses, and add helper functions check_documented_file/document_the_file. Minor error-handling, probe checks, and formatting fixes applied.
- package.json +2 -0
- src/sub-router/bucket.ts +12 -0
- src/sub-router/showtime.ts +126 -38
package.json
CHANGED
|
@@ -16,6 +16,8 @@
|
|
| 16 |
"@chneau/elysia-logger": "^1.0.10",
|
| 17 |
"@elysiajs/cors": "^1.4.1",
|
| 18 |
"@emotion/css": "^11.13.5",
|
|
|
|
|
|
|
| 19 |
"@sinclair/typebox": "^0.34.48",
|
| 20 |
"@upstash/redis": "^1.38.0",
|
| 21 |
"axios": "^1.13.6",
|
|
|
|
| 16 |
"@chneau/elysia-logger": "^1.0.10",
|
| 17 |
"@elysiajs/cors": "^1.4.1",
|
| 18 |
"@emotion/css": "^11.13.5",
|
| 19 |
+
"@huggingface/hub": "^2.13.0",
|
| 20 |
+
"@huggingface/inference": "^4.13.18",
|
| 21 |
"@sinclair/typebox": "^0.34.48",
|
| 22 |
"@upstash/redis": "^1.38.0",
|
| 23 |
"axios": "^1.13.6",
|
src/sub-router/bucket.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Context, Elysia, t } from "elysia";
|
| 2 |
+
|
| 3 |
+
const APP_ENV = process.env.APP_ENV;
|
| 4 |
+
const APP_ACCOUNT_ABC = process.env.APP_ACCOUNT_ABC;
|
| 5 |
+
const HF_BUCKET_ACCESS_TOKEN = process.env.HF_BUCKET_ACCESS_TOKEN;
|
| 6 |
+
|
| 7 |
+
const sub_router = new Elysia({ prefix: "/bucket" })
|
| 8 |
+
.get("/info", async (ctx) => {}, {})
|
| 9 |
+
|
| 10 |
+
.get("/info", async (ctx) => {}, {});
|
| 11 |
+
|
| 12 |
+
export const FfmpegRouter = sub_router;
|
src/sub-router/showtime.ts
CHANGED
|
@@ -345,25 +345,32 @@ async function F_handle_process(arg0: {
|
|
| 345 |
return {};
|
| 346 |
})();
|
| 347 |
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
if (E_ || !R_) {
|
| 358 |
-
const { status, message } = NS_error.F_collect_error_status_n_message({
|
| 359 |
-
ERR: E_
|
| 360 |
});
|
| 361 |
|
| 362 |
-
throw createError(status, message);
|
| 363 |
-
}
|
| 364 |
|
| 365 |
-
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
|
| 368 |
if (ops === "raw") {
|
| 369 |
return start_data;
|
|
@@ -375,14 +382,18 @@ async function F_handle_process(arg0: {
|
|
| 375 |
const is_download = live === "download" && sbe_id;
|
| 376 |
|
| 377 |
const job_id = is_download
|
| 378 |
-
? `${sbe_id}${picture_quality ? `-${picture_quality.trim()}` : ""}-${output_destination}`
|
| 379 |
: undefined;
|
| 380 |
|
| 381 |
-
let last_failed =
|
|
|
|
|
|
|
| 382 |
|
| 383 |
const in_factory = (() => {
|
| 384 |
if (!job_id) return;
|
|
|
|
| 385 |
const current_job = transcode_jobs.get(job_id);
|
|
|
|
| 386 |
if (!current_job) return;
|
| 387 |
|
| 388 |
const job_status = current_job.status;
|
|
@@ -391,15 +402,14 @@ async function F_handle_process(arg0: {
|
|
| 391 |
const in_progress = job_status === "in-progress";
|
| 392 |
|
| 393 |
if (failed) {
|
| 394 |
-
transcode_jobs.delete(job_id);
|
| 395 |
last_failed = true;
|
|
|
|
| 396 |
return;
|
| 397 |
}
|
| 398 |
|
| 399 |
return true;
|
| 400 |
})();
|
| 401 |
|
| 402 |
-
|
| 403 |
if (in_factory && job_id) {
|
| 404 |
const current_job = transcode_jobs.get(job_id);
|
| 405 |
const job_status = current_job?.status;
|
|
@@ -423,6 +433,7 @@ async function F_handle_process(arg0: {
|
|
| 423 |
transcoded_size,
|
| 424 |
track_process: progress_endpoint,
|
| 425 |
file: og_output_destination,
|
|
|
|
| 426 |
...(ops === "just_check" ? { passed: true } : {})
|
| 427 |
};
|
| 428 |
}
|
|
@@ -436,9 +447,17 @@ async function F_handle_process(arg0: {
|
|
| 436 |
const transcoded_size = current_job?.transcoded_size;
|
| 437 |
const progress_endpoint = current_job?.progress_endpoint;
|
| 438 |
|
| 439 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
|
| 441 |
-
if (_) {
|
| 442 |
if (ops === "just_check") {
|
| 443 |
return {
|
| 444 |
passed: true,
|
|
@@ -446,11 +465,14 @@ async function F_handle_process(arg0: {
|
|
| 446 |
transcoded_size,
|
| 447 |
track_process: progress_endpoint,
|
| 448 |
file: og_output_destination,
|
| 449 |
-
final: true
|
|
|
|
| 450 |
};
|
| 451 |
}
|
|
|
|
| 452 |
console.log("SERVING FILE ============ ", file_path, "\n");
|
| 453 |
-
|
|
|
|
| 454 |
}
|
| 455 |
}
|
| 456 |
|
|
@@ -482,6 +504,7 @@ async function F_handle_process(arg0: {
|
|
| 482 |
|
| 483 |
const file_mode = is_download && tmp_download_path;
|
| 484 |
|
|
|
|
| 485 |
if (file_mode) {
|
| 486 |
const found_file = await find_latest_valid_file({
|
| 487 |
sbe_id,
|
|
@@ -493,26 +516,39 @@ async function F_handle_process(arg0: {
|
|
| 493 |
console.log("FOUND A FILE ============ ", found_file?.path, "\n");
|
| 494 |
|
| 495 |
if (typeof found_file?.path === "string" && found_file.path.trim()) {
|
| 496 |
-
const
|
| 497 |
file_path: found_file.path.trim(),
|
| 498 |
job_id,
|
| 499 |
ctx,
|
| 500 |
-
request,
|
|
|
|
| 501 |
});
|
| 502 |
|
| 503 |
-
if (
|
|
|
|
|
|
|
|
|
|
| 504 |
if (ops === "just_check") {
|
| 505 |
return {
|
| 506 |
file: og_output_destination,
|
| 507 |
passed: true,
|
| 508 |
size: found_file?.size,
|
| 509 |
-
final: true
|
|
|
|
| 510 |
};
|
| 511 |
}
|
| 512 |
-
|
|
|
|
| 513 |
}
|
| 514 |
} else if (last_failed) {
|
| 515 |
throw new Error("Something went wrong");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
}
|
| 517 |
}
|
| 518 |
|
|
@@ -551,6 +587,8 @@ async function F_handle_process(arg0: {
|
|
| 551 |
|
| 552 |
function task_failed() {
|
| 553 |
(() => {
|
|
|
|
|
|
|
| 554 |
if (!job_id) return;
|
| 555 |
|
| 556 |
const current_job = transcode_jobs.get(job_id);
|
|
@@ -594,12 +632,14 @@ async function F_handle_process(arg0: {
|
|
| 594 |
(async () => {
|
| 595 |
if (!progress_update_endpoint) return;
|
| 596 |
|
| 597 |
-
|
| 598 |
promise: ky.post(progress_update_endpoint, {
|
| 599 |
json: { percentage: 100 }
|
| 600 |
})
|
| 601 |
});
|
| 602 |
})();
|
|
|
|
|
|
|
| 603 |
})();
|
| 604 |
}
|
| 605 |
|
|
@@ -666,8 +706,7 @@ async function F_handle_process(arg0: {
|
|
| 666 |
|
| 667 |
if (!file_size || file_size <= 0) return;
|
| 668 |
|
| 669 |
-
|
| 670 |
-
if(!skip_probe){
|
| 671 |
const [E_, R_] = await NS_promise.F_catch_promise({
|
| 672 |
promise: NS_ffmpeg.F_probe_v2({
|
| 673 |
playback_url: new URL("http://localhost:0000/"),
|
|
@@ -680,18 +719,18 @@ async function F_handle_process(arg0: {
|
|
| 680 |
|
| 681 |
if (E_) {
|
| 682 |
try {
|
| 683 |
-
|
| 684 |
|
| 685 |
if (typeof message === "string") {
|
| 686 |
const data = JSON.parse(message);
|
| 687 |
const code = parseInt(`${data?.code}`);
|
| 688 |
|
| 689 |
if (!code || (code !== 143 && code !== 0)) {
|
| 690 |
-
return
|
| 691 |
}
|
| 692 |
}
|
| 693 |
} catch {
|
| 694 |
-
return
|
| 695 |
}
|
| 696 |
} else if (!R_ || !R_?.probe_data) {
|
| 697 |
return;
|
|
@@ -862,11 +901,10 @@ async function F_handle_process(arg0: {
|
|
| 862 |
const message = E_.message;
|
| 863 |
|
| 864 |
if (typeof message === "string") {
|
| 865 |
-
|
| 866 |
const data = JSON.parse(message);
|
| 867 |
const code = parseInt(`${data?.code}`);
|
| 868 |
|
| 869 |
-
if (code &&
|
| 870 |
node_fs.promises.unlink(full_path).catch(() => {});
|
| 871 |
}
|
| 872 |
}
|
|
@@ -888,6 +926,55 @@ async function F_handle_process(arg0: {
|
|
| 888 |
}
|
| 889 |
}
|
| 890 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 891 |
if (ops === "just_check") {
|
| 892 |
if (file_mode) {
|
| 893 |
const found_file = await find_latest_valid_file({
|
|
@@ -910,7 +997,8 @@ async function F_handle_process(arg0: {
|
|
| 910 |
passed,
|
| 911 |
size,
|
| 912 |
file,
|
| 913 |
-
final
|
|
|
|
| 914 |
};
|
| 915 |
})();
|
| 916 |
|
|
|
|
| 345 |
return {};
|
| 346 |
})();
|
| 347 |
|
| 348 |
+
await (async () => {
|
| 349 |
+
// if (typeof probe_data !== "object" || Array.isArray(probe_data)) {
|
| 350 |
+
const [E_, R_] = await NS_promise.F_catch_promise({
|
| 351 |
+
promise: NS_ffmpeg.F_probe_v2({
|
| 352 |
+
playback_url: new URL(playback_url),
|
| 353 |
+
context: ctx as any,
|
| 354 |
+
playback_headers
|
| 355 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
});
|
| 357 |
|
|
|
|
|
|
|
| 358 |
|
| 359 |
+
|
| 360 |
+
if (E_ || !R_ || !R_.probe_data) {
|
| 361 |
+
const { status, message } = NS_error.F_collect_error_status_n_message({
|
| 362 |
+
ERR: E_
|
| 363 |
+
});
|
| 364 |
+
|
| 365 |
+
throw new Error("Something went wrong");
|
| 366 |
+
throw createError(status, message);
|
| 367 |
+
}
|
| 368 |
+
|
| 369 |
+
if (typeof probe_data !== "object" || Array.isArray(probe_data)) {
|
| 370 |
+
probe_data = R_.probe_data;
|
| 371 |
+
}
|
| 372 |
+
// }
|
| 373 |
+
})()
|
| 374 |
|
| 375 |
if (ops === "raw") {
|
| 376 |
return start_data;
|
|
|
|
| 382 |
const is_download = live === "download" && sbe_id;
|
| 383 |
|
| 384 |
const job_id = is_download
|
| 385 |
+
? `${sbe_id}${picture_quality ? `-${picture_quality.trim()}` : ""}-${output_destination.toLowerCase()}`
|
| 386 |
: undefined;
|
| 387 |
|
| 388 |
+
let last_failed: boolean | undefined = undefined;
|
| 389 |
+
|
| 390 |
+
let undocumented_file: Bun.BunFile | undefined = undefined;
|
| 391 |
|
| 392 |
const in_factory = (() => {
|
| 393 |
if (!job_id) return;
|
| 394 |
+
|
| 395 |
const current_job = transcode_jobs.get(job_id);
|
| 396 |
+
|
| 397 |
if (!current_job) return;
|
| 398 |
|
| 399 |
const job_status = current_job.status;
|
|
|
|
| 402 |
const in_progress = job_status === "in-progress";
|
| 403 |
|
| 404 |
if (failed) {
|
|
|
|
| 405 |
last_failed = true;
|
| 406 |
+
transcode_jobs.delete(job_id);
|
| 407 |
return;
|
| 408 |
}
|
| 409 |
|
| 410 |
return true;
|
| 411 |
})();
|
| 412 |
|
|
|
|
| 413 |
if (in_factory && job_id) {
|
| 414 |
const current_job = transcode_jobs.get(job_id);
|
| 415 |
const job_status = current_job?.status;
|
|
|
|
| 433 |
transcoded_size,
|
| 434 |
track_process: progress_endpoint,
|
| 435 |
file: og_output_destination,
|
| 436 |
+
job_id,
|
| 437 |
...(ops === "just_check" ? { passed: true } : {})
|
| 438 |
};
|
| 439 |
}
|
|
|
|
| 447 |
const transcoded_size = current_job?.transcoded_size;
|
| 448 |
const progress_endpoint = current_job?.progress_endpoint;
|
| 449 |
|
| 450 |
+
const _file_to_serve = await serve_download({
|
| 451 |
+
file_path,
|
| 452 |
+
job_id,
|
| 453 |
+
ctx,
|
| 454 |
+
request
|
| 455 |
+
});
|
| 456 |
+
|
| 457 |
+
if (_file_to_serve) {
|
| 458 |
+
undocumented_file = _file_to_serve;
|
| 459 |
+
document_the_file();
|
| 460 |
|
|
|
|
| 461 |
if (ops === "just_check") {
|
| 462 |
return {
|
| 463 |
passed: true,
|
|
|
|
| 465 |
transcoded_size,
|
| 466 |
track_process: progress_endpoint,
|
| 467 |
file: og_output_destination,
|
| 468 |
+
final: true,
|
| 469 |
+
job_id
|
| 470 |
};
|
| 471 |
}
|
| 472 |
+
|
| 473 |
console.log("SERVING FILE ============ ", file_path, "\n");
|
| 474 |
+
|
| 475 |
+
return _file_to_serve;
|
| 476 |
}
|
| 477 |
}
|
| 478 |
|
|
|
|
| 504 |
|
| 505 |
const file_mode = is_download && tmp_download_path;
|
| 506 |
|
| 507 |
+
// SERVE UNDOCUMENTED FILE
|
| 508 |
if (file_mode) {
|
| 509 |
const found_file = await find_latest_valid_file({
|
| 510 |
sbe_id,
|
|
|
|
| 516 |
console.log("FOUND A FILE ============ ", found_file?.path, "\n");
|
| 517 |
|
| 518 |
if (typeof found_file?.path === "string" && found_file.path.trim()) {
|
| 519 |
+
const _file_to_serve = await serve_download({
|
| 520 |
file_path: found_file.path.trim(),
|
| 521 |
job_id,
|
| 522 |
ctx,
|
| 523 |
+
request,
|
| 524 |
+
skip_probe: true
|
| 525 |
});
|
| 526 |
|
| 527 |
+
if (_file_to_serve) {
|
| 528 |
+
undocumented_file = _file_to_serve;
|
| 529 |
+
document_the_file();
|
| 530 |
+
|
| 531 |
if (ops === "just_check") {
|
| 532 |
return {
|
| 533 |
file: og_output_destination,
|
| 534 |
passed: true,
|
| 535 |
size: found_file?.size,
|
| 536 |
+
final: true,
|
| 537 |
+
job_id
|
| 538 |
};
|
| 539 |
}
|
| 540 |
+
|
| 541 |
+
return _file_to_serve;
|
| 542 |
}
|
| 543 |
} else if (last_failed) {
|
| 544 |
throw new Error("Something went wrong");
|
| 545 |
+
} else {
|
| 546 |
+
// CHECK DOCUMENTED FILES
|
| 547 |
+
const _file_data = await check_documented_file();
|
| 548 |
+
|
| 549 |
+
if (_file_data && _file_data.download_link) {
|
| 550 |
+
const download_link = _file_data.download_link;
|
| 551 |
+
}
|
| 552 |
}
|
| 553 |
}
|
| 554 |
|
|
|
|
| 587 |
|
| 588 |
function task_failed() {
|
| 589 |
(() => {
|
| 590 |
+
last_failed = true;
|
| 591 |
+
|
| 592 |
if (!job_id) return;
|
| 593 |
|
| 594 |
const current_job = transcode_jobs.get(job_id);
|
|
|
|
| 632 |
(async () => {
|
| 633 |
if (!progress_update_endpoint) return;
|
| 634 |
|
| 635 |
+
NS_promise.F_catch_promise({
|
| 636 |
promise: ky.post(progress_update_endpoint, {
|
| 637 |
json: { percentage: 100 }
|
| 638 |
})
|
| 639 |
});
|
| 640 |
})();
|
| 641 |
+
|
| 642 |
+
document_the_file();
|
| 643 |
})();
|
| 644 |
}
|
| 645 |
|
|
|
|
| 706 |
|
| 707 |
if (!file_size || file_size <= 0) return;
|
| 708 |
|
| 709 |
+
if (!skip_probe) {
|
|
|
|
| 710 |
const [E_, R_] = await NS_promise.F_catch_promise({
|
| 711 |
promise: NS_ffmpeg.F_probe_v2({
|
| 712 |
playback_url: new URL("http://localhost:0000/"),
|
|
|
|
| 719 |
|
| 720 |
if (E_) {
|
| 721 |
try {
|
| 722 |
+
const message = E_.message;
|
| 723 |
|
| 724 |
if (typeof message === "string") {
|
| 725 |
const data = JSON.parse(message);
|
| 726 |
const code = parseInt(`${data?.code}`);
|
| 727 |
|
| 728 |
if (!code || (code !== 143 && code !== 0)) {
|
| 729 |
+
return;
|
| 730 |
}
|
| 731 |
}
|
| 732 |
} catch {
|
| 733 |
+
return;
|
| 734 |
}
|
| 735 |
} else if (!R_ || !R_?.probe_data) {
|
| 736 |
return;
|
|
|
|
| 901 |
const message = E_.message;
|
| 902 |
|
| 903 |
if (typeof message === "string") {
|
|
|
|
| 904 |
const data = JSON.parse(message);
|
| 905 |
const code = parseInt(`${data?.code}`);
|
| 906 |
|
| 907 |
+
if (code && code !== 143 && code !== 0) {
|
| 908 |
node_fs.promises.unlink(full_path).catch(() => {});
|
| 909 |
}
|
| 910 |
}
|
|
|
|
| 926 |
}
|
| 927 |
}
|
| 928 |
|
| 929 |
+
async function check_documented_file() {
|
| 930 |
+
if (!file_mode) {
|
| 931 |
+
return;
|
| 932 |
+
}
|
| 933 |
+
|
| 934 |
+
const data: any = {};
|
| 935 |
+
|
| 936 |
+
return data;
|
| 937 |
+
}
|
| 938 |
+
|
| 939 |
+
async function document_the_file() {
|
| 940 |
+
if (!file_mode) {
|
| 941 |
+
return;
|
| 942 |
+
}
|
| 943 |
+
|
| 944 |
+
console.log("📃 DOCUMENTING FILE");
|
| 945 |
+
|
| 946 |
+
if (!undocumented_file) {
|
| 947 |
+
const found_file = await find_latest_valid_file({
|
| 948 |
+
sbe_id,
|
| 949 |
+
picture_quality,
|
| 950 |
+
output_destination,
|
| 951 |
+
S3Dir
|
| 952 |
+
});
|
| 953 |
+
|
| 954 |
+
if (typeof found_file?.path !== "string" || found_file.path.trim()) {
|
| 955 |
+
return;
|
| 956 |
+
}
|
| 957 |
+
|
| 958 |
+
const _file_to_serve = await serve_download({
|
| 959 |
+
file_path: found_file.path.trim(),
|
| 960 |
+
job_id,
|
| 961 |
+
ctx,
|
| 962 |
+
request,
|
| 963 |
+
skip_probe: true
|
| 964 |
+
});
|
| 965 |
+
|
| 966 |
+
if (!_file_to_serve) {
|
| 967 |
+
return;
|
| 968 |
+
}
|
| 969 |
+
|
| 970 |
+
undocumented_file = _file_to_serve;
|
| 971 |
+
}
|
| 972 |
+
|
| 973 |
+
if (!undocumented_file) {
|
| 974 |
+
return;
|
| 975 |
+
}
|
| 976 |
+
}
|
| 977 |
+
|
| 978 |
if (ops === "just_check") {
|
| 979 |
if (file_mode) {
|
| 980 |
const found_file = await find_latest_valid_file({
|
|
|
|
| 997 |
passed,
|
| 998 |
size,
|
| 999 |
file,
|
| 1000 |
+
final,
|
| 1001 |
+
job_id
|
| 1002 |
};
|
| 1003 |
})();
|
| 1004 |
|