GUI-STUDIO commited on
Commit ·
ec0f8ba
1
Parent(s): 23209af
Add picture_quality support and FFmpeg fixes
Browse filesPropagate a new picture_quality field through download portal and app-ffmpeg routes and handlers. Include picture_quality in job IDs and temporary filenames, add schema validation, and sanitize filenames (cleanName/cleanNameV2). Improve file discovery to filter by picture_quality and remove corrupted files. Enhance ffprobe error reporting to include JSON with code/message, fix console.error typo, log resource usage, ensure task_failed is called on stdout errors, and return meaningful results for ops=just_check. Misc small refactors: S3 dir/path changes, content-disposition uses original filename, and formatting/typo cleanups.
- src/lib/ffmpeg.ts +7 -1
- src/sub-router/ffmpeg.ts +14 -4
- src/sub-router/showtime.ts +220 -59
src/lib/ffmpeg.ts
CHANGED
|
@@ -407,7 +407,13 @@ async function F_probe_v2(arg0: {
|
|
| 407 |
|
| 408 |
if (exitCode !== 0) {
|
| 409 |
clearTimeout(t);
|
| 410 |
-
throw new Error(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
}
|
| 412 |
|
| 413 |
const metadata: FfprobeData = JSON.parse(txt);
|
|
|
|
| 407 |
|
| 408 |
if (exitCode !== 0) {
|
| 409 |
clearTimeout(t);
|
| 410 |
+
throw new Error(
|
| 411 |
+
JSON.stringify({
|
| 412 |
+
message: `Probe failed with code [${exitCode}]: ${stderr}`,
|
| 413 |
+
code: exitCode,
|
| 414 |
+
stderr: stderr
|
| 415 |
+
})
|
| 416 |
+
);
|
| 417 |
}
|
| 418 |
|
| 419 |
const metadata: FfprobeData = JSON.parse(txt);
|
src/sub-router/ffmpeg.ts
CHANGED
|
@@ -24,6 +24,7 @@ const sub_router = new Elysia({ prefix: "/ffmpeg" })
|
|
| 24 |
let gen = body?.gen;
|
| 25 |
let playback_type = body?.playback_type;
|
| 26 |
const sbe_id = body?.sbe_id;
|
|
|
|
| 27 |
|
| 28 |
const REQ0 = request.url;
|
| 29 |
const REQ_ORIGIN = (ctx as any).publicOrigin || new URL(REQ0).origin;
|
|
@@ -76,7 +77,7 @@ const sub_router = new Elysia({ prefix: "/ffmpeg" })
|
|
| 76 |
|
| 77 |
if (E_ || !R_ || !R_.probe_data) {
|
| 78 |
console.log("\n");
|
| 79 |
-
console.
|
| 80 |
console.log("\n");
|
| 81 |
const { status, message } = NS_error.F_collect_error_status_n_message({
|
| 82 |
ERR: E_
|
|
@@ -99,7 +100,7 @@ const sub_router = new Elysia({ prefix: "/ffmpeg" })
|
|
| 99 |
output_filename,
|
| 100 |
subtitles
|
| 101 |
} = gen;
|
| 102 |
-
|
| 103 |
const is_valid_progress_feedback_url = (() => {
|
| 104 |
if (!progress_feedback_url) return;
|
| 105 |
|
|
@@ -198,7 +199,8 @@ const sub_router = new Elysia({ prefix: "/ffmpeg" })
|
|
| 198 |
probe_data,
|
| 199 |
progress_update_endpoint,
|
| 200 |
playback_type,
|
| 201 |
-
sbe_id
|
|
|
|
| 202 |
};
|
| 203 |
|
| 204 |
const data_str = devalue.stringify(data);
|
|
@@ -218,7 +220,7 @@ const sub_router = new Elysia({ prefix: "/ffmpeg" })
|
|
| 218 |
});
|
| 219 |
|
| 220 |
let key = `ffmpeg:${track_id}`;
|
| 221 |
-
if(APP_ACCOUNT_ABC) key = `${key}:${APP_ACCOUNT_ABC}`
|
| 222 |
|
| 223 |
// 30 days = 30 * 24 * 60 * 60 = 2,592,000 seconds
|
| 224 |
const ONE_MONTH_IN_SECONDS = 2592000;
|
|
@@ -268,6 +270,14 @@ const sub_router = new Elysia({ prefix: "/ffmpeg" })
|
|
| 268 |
}
|
| 269 |
})
|
| 270 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
gen: t.Optional(
|
| 272 |
t.Object(
|
| 273 |
{
|
|
|
|
| 24 |
let gen = body?.gen;
|
| 25 |
let playback_type = body?.playback_type;
|
| 26 |
const sbe_id = body?.sbe_id;
|
| 27 |
+
const picture_quality = body?.picture_quality;
|
| 28 |
|
| 29 |
const REQ0 = request.url;
|
| 30 |
const REQ_ORIGIN = (ctx as any).publicOrigin || new URL(REQ0).origin;
|
|
|
|
| 77 |
|
| 78 |
if (E_ || !R_ || !R_.probe_data) {
|
| 79 |
console.log("\n");
|
| 80 |
+
console.error("E_", E_);
|
| 81 |
console.log("\n");
|
| 82 |
const { status, message } = NS_error.F_collect_error_status_n_message({
|
| 83 |
ERR: E_
|
|
|
|
| 100 |
output_filename,
|
| 101 |
subtitles
|
| 102 |
} = gen;
|
| 103 |
+
|
| 104 |
const is_valid_progress_feedback_url = (() => {
|
| 105 |
if (!progress_feedback_url) return;
|
| 106 |
|
|
|
|
| 199 |
probe_data,
|
| 200 |
progress_update_endpoint,
|
| 201 |
playback_type,
|
| 202 |
+
sbe_id,
|
| 203 |
+
picture_quality
|
| 204 |
};
|
| 205 |
|
| 206 |
const data_str = devalue.stringify(data);
|
|
|
|
| 220 |
});
|
| 221 |
|
| 222 |
let key = `ffmpeg:${track_id}`;
|
| 223 |
+
if (APP_ACCOUNT_ABC) key = `${key}:${APP_ACCOUNT_ABC}`;
|
| 224 |
|
| 225 |
// 30 days = 30 * 24 * 60 * 60 = 2,592,000 seconds
|
| 226 |
const ONE_MONTH_IN_SECONDS = 2592000;
|
|
|
|
| 270 |
}
|
| 271 |
})
|
| 272 |
),
|
| 273 |
+
picture_quality: t.Optional(
|
| 274 |
+
t.String({
|
| 275 |
+
minLength: 1,
|
| 276 |
+
error() {
|
| 277 |
+
return "Invalid picture quality";
|
| 278 |
+
}
|
| 279 |
+
})
|
| 280 |
+
),
|
| 281 |
gen: t.Optional(
|
| 282 |
t.Object(
|
| 283 |
{
|
src/sub-router/showtime.ts
CHANGED
|
@@ -81,11 +81,19 @@ const sub_router = new Elysia({ prefix: "/showtime" })
|
|
| 81 |
})
|
| 82 |
),
|
| 83 |
ops: t.Optional(
|
| 84 |
-
t.Union(
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
-
|
| 89 |
)
|
| 90 |
})
|
| 91 |
}
|
|
@@ -136,6 +144,17 @@ const sub_router = new Elysia({ prefix: "/showtime" })
|
|
| 136 |
}
|
| 137 |
);
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
async function F_handle_process(arg0: {
|
| 140 |
data: string;
|
| 141 |
live: string;
|
|
@@ -197,7 +216,8 @@ async function F_handle_process(arg0: {
|
|
| 197 |
probe_data,
|
| 198 |
progress_update_endpoint,
|
| 199 |
playback_type,
|
| 200 |
-
sbe_id
|
|
|
|
| 201 |
} = start_data;
|
| 202 |
|
| 203 |
const REQ0 = new URL(request.url);
|
|
@@ -225,10 +245,6 @@ async function F_handle_process(arg0: {
|
|
| 225 |
throw createError(400, "Please provide a valid playback URL");
|
| 226 |
}
|
| 227 |
|
| 228 |
-
if (!is_valid_playback_url) {
|
| 229 |
-
throw createError(400, "Please provide a valid playback URL");
|
| 230 |
-
}
|
| 231 |
-
|
| 232 |
if (!output_format) {
|
| 233 |
output_format = "mp4";
|
| 234 |
}
|
|
@@ -258,6 +274,22 @@ async function F_handle_process(arg0: {
|
|
| 258 |
output_destination = `${output_destination}.${output_format}`;
|
| 259 |
}
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
const tcp_progress = is_valid_progress_feedback_url ? true : false;
|
| 262 |
|
| 263 |
const subtitles_ary = (() => {
|
|
@@ -342,7 +374,9 @@ async function F_handle_process(arg0: {
|
|
| 342 |
// -------------------------------
|
| 343 |
const is_download = live === "download" && sbe_id;
|
| 344 |
|
| 345 |
-
const job_id = is_download
|
|
|
|
|
|
|
| 346 |
|
| 347 |
let last_failed = false;
|
| 348 |
|
|
@@ -386,7 +420,8 @@ async function F_handle_process(arg0: {
|
|
| 386 |
percent,
|
| 387 |
transcoded_size,
|
| 388 |
track_process: progress_endpoint,
|
| 389 |
-
|
|
|
|
| 390 |
};
|
| 391 |
}
|
| 392 |
break;
|
|
@@ -394,14 +429,22 @@ async function F_handle_process(arg0: {
|
|
| 394 |
case "done":
|
| 395 |
{
|
| 396 |
const file_path = current_job?.tmp_path;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
const _ = await serve_download({ file_path, job_id, ctx, request });
|
| 399 |
|
| 400 |
if (_) {
|
| 401 |
-
if(ops ===
|
| 402 |
return {
|
| 403 |
-
passed: true
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
}
|
| 406 |
console.log("SERVING FILE ============ ", file_path, "\n");
|
| 407 |
return _;
|
|
@@ -412,20 +455,23 @@ async function F_handle_process(arg0: {
|
|
| 412 |
}
|
| 413 |
}
|
| 414 |
|
| 415 |
-
const
|
| 416 |
|
| 417 |
let tmp_download_path: string | null | undefined = await (async () => {
|
| 418 |
if (!is_download) return;
|
| 419 |
try {
|
| 420 |
-
// await Bun.mkdir(
|
| 421 |
|
| 422 |
-
node_fs.mkdirSync(
|
| 423 |
|
| 424 |
-
const
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
-
console.log("\n", "PATH:",
|
| 427 |
|
| 428 |
-
return
|
| 429 |
} catch (e) {
|
| 430 |
console.error({ e });
|
| 431 |
}
|
|
@@ -436,29 +482,32 @@ async function F_handle_process(arg0: {
|
|
| 436 |
if (file_mode) {
|
| 437 |
const found_file = await find_latest_valid_file({
|
| 438 |
sbe_id,
|
| 439 |
-
|
|
|
|
|
|
|
| 440 |
});
|
| 441 |
|
| 442 |
console.log("FOUND A FILE ============ ", found_file, "\n");
|
| 443 |
|
| 444 |
-
if (typeof found_file === "string" && found_file) {
|
| 445 |
const _ = await serve_download({
|
| 446 |
-
file_path: found_file,
|
| 447 |
job_id,
|
| 448 |
ctx,
|
| 449 |
request
|
| 450 |
});
|
| 451 |
|
| 452 |
if (_) {
|
| 453 |
-
if(ops ===
|
| 454 |
return {
|
|
|
|
| 455 |
passed: true
|
| 456 |
-
}
|
| 457 |
}
|
| 458 |
return _;
|
| 459 |
}
|
| 460 |
} else if (last_failed) {
|
| 461 |
-
throw Error;
|
| 462 |
}
|
| 463 |
}
|
| 464 |
|
|
@@ -549,11 +598,18 @@ async function F_handle_process(arg0: {
|
|
| 549 |
})();
|
| 550 |
}
|
| 551 |
|
| 552 |
-
function task_processing(arg0: {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 553 |
const { percent } = arg0;
|
| 554 |
|
| 555 |
-
const size = typeof arg0.size ===
|
| 556 |
-
const progress_endpoint =
|
|
|
|
|
|
|
|
|
|
| 557 |
|
| 558 |
(() => {
|
| 559 |
if (!job_id) return;
|
|
@@ -565,7 +621,8 @@ async function F_handle_process(arg0: {
|
|
| 565 |
status: "in-progress",
|
| 566 |
tmp_path: tmp_download_path,
|
| 567 |
percent,
|
| 568 |
-
transcoded_size: size,
|
|
|
|
| 569 |
created_at: Date.now()
|
| 570 |
});
|
| 571 |
} else {
|
|
@@ -573,7 +630,8 @@ async function F_handle_process(arg0: {
|
|
| 573 |
...current_job,
|
| 574 |
status: "in-progress",
|
| 575 |
percent,
|
| 576 |
-
transcoded_size: size,
|
|
|
|
| 577 |
});
|
| 578 |
}
|
| 579 |
})();
|
|
@@ -617,7 +675,7 @@ async function F_handle_process(arg0: {
|
|
| 617 |
}
|
| 618 |
|
| 619 |
ctx.set.headers["content-disposition"] =
|
| 620 |
-
`attachment; filename="${output_destination}"`;
|
| 621 |
ctx.set.headers["cache-control"] = "no-cache";
|
| 622 |
ctx.set.headers["accept-ranges"] = "bytes";
|
| 623 |
|
|
@@ -647,20 +705,49 @@ async function F_handle_process(arg0: {
|
|
| 647 |
return file;
|
| 648 |
}
|
| 649 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 650 |
// ── helper (module level) ───────
|
| 651 |
async function find_latest_valid_file(arg0: {
|
| 652 |
sbe_id: string;
|
|
|
|
| 653 |
output_destination: string;
|
|
|
|
| 654 |
}): Promise<string | null | undefined> {
|
| 655 |
-
const { sbe_id, output_destination } = arg0;
|
|
|
|
|
|
|
| 656 |
|
| 657 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 658 |
|
| 659 |
try {
|
| 660 |
const entries = await node_fs.promises.readdir(dir);
|
| 661 |
|
| 662 |
// keep only files that contain output_destination in the name
|
| 663 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
if (!matches.length) return null;
|
| 666 |
|
|
@@ -679,7 +766,6 @@ async function F_handle_process(arg0: {
|
|
| 679 |
try {
|
| 680 |
const stat = await node_fs.promises.stat(full_path);
|
| 681 |
if (!(stat.size > 0)) {
|
| 682 |
-
node_fs.promises.unlink(full_path).catch(() => {});
|
| 683 |
continue;
|
| 684 |
}
|
| 685 |
|
|
@@ -698,7 +784,60 @@ async function F_handle_process(arg0: {
|
|
| 698 |
}
|
| 699 |
|
| 700 |
if (E_) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
node_fs.promises.unlink(full_path).catch(() => {});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
}
|
| 703 |
} catch (e) {
|
| 704 |
continue;
|
|
@@ -711,13 +850,39 @@ async function F_handle_process(arg0: {
|
|
| 711 |
}
|
| 712 |
} catch (e) {
|
| 713 |
// dir doesn't exist yet or unreadable
|
|
|
|
| 714 |
return null;
|
| 715 |
}
|
| 716 |
}
|
| 717 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
if (abort_signal) {
|
| 719 |
if (abort_signal.aborted) {
|
| 720 |
kill_controller();
|
|
|
|
| 721 |
} else {
|
| 722 |
abort_signal.addEventListener(
|
| 723 |
"abort",
|
|
@@ -731,26 +896,6 @@ async function F_handle_process(arg0: {
|
|
| 731 |
}
|
| 732 |
}
|
| 733 |
|
| 734 |
-
if(ops === 'just_check') {
|
| 735 |
-
if (file_mode) {
|
| 736 |
-
const found_file = await find_latest_valid_file({
|
| 737 |
-
sbe_id,
|
| 738 |
-
output_destination
|
| 739 |
-
});
|
| 740 |
-
|
| 741 |
-
return {passed: found_file ? true : (() => {
|
| 742 |
-
if(!last_failed) {
|
| 743 |
-
return {passed: true}
|
| 744 |
-
}
|
| 745 |
-
return false
|
| 746 |
-
})()}
|
| 747 |
-
}
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
|
| 751 |
-
return {}
|
| 752 |
-
}
|
| 753 |
-
|
| 754 |
console.log("\n", "PINGING FFMPEG NOW", "\n");
|
| 755 |
|
| 756 |
// -------------------------------
|
|
@@ -868,6 +1013,8 @@ async function F_handle_process(arg0: {
|
|
| 868 |
return proc;
|
| 869 |
})();
|
| 870 |
|
|
|
|
|
|
|
| 871 |
const logged_proc_errors: string[] = [];
|
| 872 |
|
| 873 |
const has_pipe_3 = (bun_proc as any).extraPipe && bun_proc.stdio[3];
|
|
@@ -963,7 +1110,11 @@ async function F_handle_process(arg0: {
|
|
| 963 |
})();
|
| 964 |
|
| 965 |
if (APP_ENV === "dev") {
|
| 966 |
-
console.log(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 967 |
}
|
| 968 |
|
| 969 |
const ws_client_id = ctx.request.headers.get("x-ws-client-id");
|
|
@@ -982,7 +1133,11 @@ async function F_handle_process(arg0: {
|
|
| 982 |
(() => {
|
| 983 |
const percent = (progress_update as any)?.percentage;
|
| 984 |
if (percent && parseFloat(`${percent}`) && file_mode) {
|
| 985 |
-
task_processing({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 986 |
|
| 987 |
if (parseFloat(`${percent}`) >= 100) {
|
| 988 |
task_finished();
|
|
@@ -1043,12 +1198,15 @@ async function F_handle_process(arg0: {
|
|
| 1043 |
})();
|
| 1044 |
|
| 1045 |
const { done, value } = await reader.read();
|
|
|
|
| 1046 |
if (done) {
|
| 1047 |
task_finished();
|
| 1048 |
|
| 1049 |
console.log("\n");
|
| 1050 |
console.log(
|
| 1051 |
`FFMPEG finished -> ${schemed_data?.current_filesize} ${tmp_download_path}`,
|
|
|
|
|
|
|
| 1052 |
"\n"
|
| 1053 |
);
|
| 1054 |
break;
|
|
@@ -1060,6 +1218,7 @@ async function F_handle_process(arg0: {
|
|
| 1060 |
}
|
| 1061 |
} catch (e) {
|
| 1062 |
console.error("Error reading stdout:", e);
|
|
|
|
| 1063 |
}
|
| 1064 |
return;
|
| 1065 |
}
|
|
@@ -1097,7 +1256,9 @@ async function F_handle_process(arg0: {
|
|
| 1097 |
|
| 1098 |
if (code === 0) {
|
| 1099 |
console.log(
|
| 1100 |
-
`FFMPEG finished -> ${schemed_data?.current_filesize} ${output_destination}`,
|
|
|
|
|
|
|
| 1101 |
"\n"
|
| 1102 |
);
|
| 1103 |
} else {
|
|
|
|
| 81 |
})
|
| 82 |
),
|
| 83 |
ops: t.Optional(
|
| 84 |
+
t.Union(
|
| 85 |
+
[
|
| 86 |
+
t.Literal("cmd"),
|
| 87 |
+
t.Literal("raw"),
|
| 88 |
+
t.Literal("decode"),
|
| 89 |
+
t.Literal("just_check")
|
| 90 |
+
],
|
| 91 |
+
{
|
| 92 |
+
error() {
|
| 93 |
+
return "Invalid operation, must be one of: cmd, raw, decode";
|
| 94 |
+
}
|
| 95 |
}
|
| 96 |
+
)
|
| 97 |
)
|
| 98 |
})
|
| 99 |
}
|
|
|
|
| 144 |
}
|
| 145 |
);
|
| 146 |
|
| 147 |
+
function cleanName(name: string): string {
|
| 148 |
+
// Replace any character that is not a letter, number, bracket, -, or _ with an underscore
|
| 149 |
+
return name.replace(/[^\w\-()[\]{}]/g, "_");
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
function cleanNameV2(name: string): string {
|
| 153 |
+
// Keep only letters, numbers, -, _, (, ), [, ], {, }, and .
|
| 154 |
+
// Replace everything else with _
|
| 155 |
+
return name.replace(/[^\w\-()[\]{}.]/g, "_");
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
async function F_handle_process(arg0: {
|
| 159 |
data: string;
|
| 160 |
live: string;
|
|
|
|
| 216 |
probe_data,
|
| 217 |
progress_update_endpoint,
|
| 218 |
playback_type,
|
| 219 |
+
sbe_id,
|
| 220 |
+
picture_quality
|
| 221 |
} = start_data;
|
| 222 |
|
| 223 |
const REQ0 = new URL(request.url);
|
|
|
|
| 245 |
throw createError(400, "Please provide a valid playback URL");
|
| 246 |
}
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
if (!output_format) {
|
| 249 |
output_format = "mp4";
|
| 250 |
}
|
|
|
|
| 274 |
output_destination = `${output_destination}.${output_format}`;
|
| 275 |
}
|
| 276 |
|
| 277 |
+
const og_output_destination = output_destination;
|
| 278 |
+
|
| 279 |
+
output_destination = cleanNameV2(output_destination);
|
| 280 |
+
|
| 281 |
+
if (typeof picture_quality === "string") {
|
| 282 |
+
picture_quality = picture_quality.trim();
|
| 283 |
+
|
| 284 |
+
if (picture_quality) {
|
| 285 |
+
picture_quality = cleanName(picture_quality);
|
| 286 |
+
} else {
|
| 287 |
+
picture_quality = undefined;
|
| 288 |
+
}
|
| 289 |
+
} else {
|
| 290 |
+
picture_quality = undefined;
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
const tcp_progress = is_valid_progress_feedback_url ? true : false;
|
| 294 |
|
| 295 |
const subtitles_ary = (() => {
|
|
|
|
| 374 |
// -------------------------------
|
| 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 = false;
|
| 382 |
|
|
|
|
| 420 |
percent,
|
| 421 |
transcoded_size,
|
| 422 |
track_process: progress_endpoint,
|
| 423 |
+
file: og_output_destination,
|
| 424 |
+
...(ops === "just_check" ? { passed: true } : {})
|
| 425 |
};
|
| 426 |
}
|
| 427 |
break;
|
|
|
|
| 429 |
case "done":
|
| 430 |
{
|
| 431 |
const file_path = current_job?.tmp_path;
|
| 432 |
+
const percent = current_job?.percent;
|
| 433 |
+
const percentage = typeof percent === "number" ? `${percent} %` : "";
|
| 434 |
+
const transcoded_size = current_job?.transcoded_size;
|
| 435 |
+
const progress_endpoint = current_job?.progress_endpoint;
|
| 436 |
|
| 437 |
const _ = await serve_download({ file_path, job_id, ctx, request });
|
| 438 |
|
| 439 |
if (_) {
|
| 440 |
+
if (ops === "just_check") {
|
| 441 |
return {
|
| 442 |
+
passed: true,
|
| 443 |
+
percent,
|
| 444 |
+
transcoded_size,
|
| 445 |
+
track_process: progress_endpoint,
|
| 446 |
+
file: og_output_destination
|
| 447 |
+
};
|
| 448 |
}
|
| 449 |
console.log("SERVING FILE ============ ", file_path, "\n");
|
| 450 |
return _;
|
|
|
|
| 455 |
}
|
| 456 |
}
|
| 457 |
|
| 458 |
+
const S3Dir = path.join(STORAGE_ROOT, "cache", `${sbe_id}`);
|
| 459 |
|
| 460 |
let tmp_download_path: string | null | undefined = await (async () => {
|
| 461 |
if (!is_download) return;
|
| 462 |
try {
|
| 463 |
+
// await Bun.mkdir(S3Dir, { recursive: true }).catch(() => {});
|
| 464 |
|
| 465 |
+
node_fs.mkdirSync(S3Dir, { recursive: true });
|
| 466 |
|
| 467 |
+
const filePath = path.join(
|
| 468 |
+
S3Dir,
|
| 469 |
+
`${Date.now()}${picture_quality ? `-${picture_quality.trim()}` : ""}-${output_destination}`
|
| 470 |
+
);
|
| 471 |
|
| 472 |
+
console.log("\n", "FILE PATH:", filePath, "\n");
|
| 473 |
|
| 474 |
+
return filePath;
|
| 475 |
} catch (e) {
|
| 476 |
console.error({ e });
|
| 477 |
}
|
|
|
|
| 482 |
if (file_mode) {
|
| 483 |
const found_file = await find_latest_valid_file({
|
| 484 |
sbe_id,
|
| 485 |
+
picture_quality,
|
| 486 |
+
output_destination,
|
| 487 |
+
S3Dir
|
| 488 |
});
|
| 489 |
|
| 490 |
console.log("FOUND A FILE ============ ", found_file, "\n");
|
| 491 |
|
| 492 |
+
if (typeof found_file === "string" && found_file.trim()) {
|
| 493 |
const _ = await serve_download({
|
| 494 |
+
file_path: found_file.trim(),
|
| 495 |
job_id,
|
| 496 |
ctx,
|
| 497 |
request
|
| 498 |
});
|
| 499 |
|
| 500 |
if (_) {
|
| 501 |
+
if (ops === "just_check") {
|
| 502 |
return {
|
| 503 |
+
file: og_output_destination,
|
| 504 |
passed: true
|
| 505 |
+
};
|
| 506 |
}
|
| 507 |
return _;
|
| 508 |
}
|
| 509 |
} else if (last_failed) {
|
| 510 |
+
throw new Error("Something went wrong");
|
| 511 |
}
|
| 512 |
}
|
| 513 |
|
|
|
|
| 598 |
})();
|
| 599 |
}
|
| 600 |
|
| 601 |
+
function task_processing(arg0: {
|
| 602 |
+
percent: number;
|
| 603 |
+
size?: string;
|
| 604 |
+
progress_endpoint?: string;
|
| 605 |
+
}) {
|
| 606 |
const { percent } = arg0;
|
| 607 |
|
| 608 |
+
const size = typeof arg0.size === "string" ? arg0.size : undefined;
|
| 609 |
+
const progress_endpoint =
|
| 610 |
+
arg0.progress_endpoint && NS_misc.isValidUrlFormat(arg0.progress_endpoint)
|
| 611 |
+
? arg0.progress_endpoint
|
| 612 |
+
: undefined;
|
| 613 |
|
| 614 |
(() => {
|
| 615 |
if (!job_id) return;
|
|
|
|
| 621 |
status: "in-progress",
|
| 622 |
tmp_path: tmp_download_path,
|
| 623 |
percent,
|
| 624 |
+
transcoded_size: size,
|
| 625 |
+
progress_endpoint,
|
| 626 |
created_at: Date.now()
|
| 627 |
});
|
| 628 |
} else {
|
|
|
|
| 630 |
...current_job,
|
| 631 |
status: "in-progress",
|
| 632 |
percent,
|
| 633 |
+
transcoded_size: size,
|
| 634 |
+
progress_endpoint
|
| 635 |
});
|
| 636 |
}
|
| 637 |
})();
|
|
|
|
| 675 |
}
|
| 676 |
|
| 677 |
ctx.set.headers["content-disposition"] =
|
| 678 |
+
`attachment; filename="${og_output_destination || output_destination}"`;
|
| 679 |
ctx.set.headers["cache-control"] = "no-cache";
|
| 680 |
ctx.set.headers["accept-ranges"] = "bytes";
|
| 681 |
|
|
|
|
| 705 |
return file;
|
| 706 |
}
|
| 707 |
|
| 708 |
+
function isValidUnixTimestamp(timestamp: number): boolean {
|
| 709 |
+
const date = new Date(timestamp);
|
| 710 |
+
return !isNaN(date.getTime());
|
| 711 |
+
}
|
| 712 |
+
|
| 713 |
// ── helper (module level) ───────
|
| 714 |
async function find_latest_valid_file(arg0: {
|
| 715 |
sbe_id: string;
|
| 716 |
+
picture_quality?: string;
|
| 717 |
output_destination: string;
|
| 718 |
+
S3Dir: string;
|
| 719 |
}): Promise<string | null | undefined> {
|
| 720 |
+
const { sbe_id, picture_quality, output_destination, S3Dir } = arg0;
|
| 721 |
+
|
| 722 |
+
const dir = S3Dir;
|
| 723 |
|
| 724 |
+
console.log(
|
| 725 |
+
"\n",
|
| 726 |
+
`🔎 ==== FIELD SEARCHING FOR: ${picture_quality?.trim() ? picture_quality.trim() : "ALL"} ${output_destination} ====`,
|
| 727 |
+
"\n"
|
| 728 |
+
);
|
| 729 |
|
| 730 |
try {
|
| 731 |
const entries = await node_fs.promises.readdir(dir);
|
| 732 |
|
| 733 |
// keep only files that contain output_destination in the name
|
| 734 |
+
let matches = entries.filter((f) =>
|
| 735 |
+
f.toLowerCase().includes(output_destination.toLowerCase())
|
| 736 |
+
);
|
| 737 |
+
|
| 738 |
+
if (!matches.length) return null;
|
| 739 |
+
|
| 740 |
+
// keep only files that contain picture_quality in the name
|
| 741 |
+
if (picture_quality?.trim()) {
|
| 742 |
+
const query = picture_quality.trim().toLowerCase();
|
| 743 |
+
matches = matches.filter((f) => f.toLowerCase().includes(query));
|
| 744 |
+
} else {
|
| 745 |
+
matches = matches.filter((f) => {
|
| 746 |
+
const _ = f.toLowerCase().split("-");
|
| 747 |
+
const __ = _.slice(1);
|
| 748 |
+
return __.join("-").startsWith(`${output_destination.toLowerCase()}`);
|
| 749 |
+
});
|
| 750 |
+
}
|
| 751 |
|
| 752 |
if (!matches.length) return null;
|
| 753 |
|
|
|
|
| 766 |
try {
|
| 767 |
const stat = await node_fs.promises.stat(full_path);
|
| 768 |
if (!(stat.size > 0)) {
|
|
|
|
| 769 |
continue;
|
| 770 |
}
|
| 771 |
|
|
|
|
| 784 |
}
|
| 785 |
|
| 786 |
if (E_) {
|
| 787 |
+
const message = E_.message;
|
| 788 |
+
|
| 789 |
+
if (typeof message === "string") {
|
| 790 |
+
try {
|
| 791 |
+
const data = JSON.parse(message);
|
| 792 |
+
const code = parseInt(`${data?.code}`);
|
| 793 |
+
|
| 794 |
+
if (code && (code === 143 || code === 0)) {
|
| 795 |
+
return full_path;
|
| 796 |
+
}
|
| 797 |
+
} catch {}
|
| 798 |
+
}
|
| 799 |
+
}
|
| 800 |
+
} catch (e) {
|
| 801 |
+
continue;
|
| 802 |
+
}
|
| 803 |
+
}
|
| 804 |
+
})();
|
| 805 |
+
|
| 806 |
+
// clean corrupted files
|
| 807 |
+
(async () => {
|
| 808 |
+
for (const filename of matches) {
|
| 809 |
+
const full_path = path.join(dir, filename);
|
| 810 |
+
|
| 811 |
+
try {
|
| 812 |
+
const stat = await node_fs.promises.stat(full_path);
|
| 813 |
+
if (!(stat.size > 0)) {
|
| 814 |
node_fs.promises.unlink(full_path).catch(() => {});
|
| 815 |
+
continue;
|
| 816 |
+
}
|
| 817 |
+
|
| 818 |
+
const [E_, R_] = await NS_promise.F_catch_promise({
|
| 819 |
+
promise: NS_ffmpeg.F_probe_v2({
|
| 820 |
+
playback_url: new URL("http://localhost:0000/"),
|
| 821 |
+
context: ctx as any,
|
| 822 |
+
file_mode: {
|
| 823 |
+
path: full_path
|
| 824 |
+
}
|
| 825 |
+
})
|
| 826 |
+
});
|
| 827 |
+
|
| 828 |
+
if (E_) {
|
| 829 |
+
const message = E_.message;
|
| 830 |
+
|
| 831 |
+
if (typeof message === "string") {
|
| 832 |
+
try {
|
| 833 |
+
const data = JSON.parse(message);
|
| 834 |
+
const code = parseInt(`${data?.code}`);
|
| 835 |
+
|
| 836 |
+
if (code && code !== 143) {
|
| 837 |
+
node_fs.promises.unlink(full_path).catch(() => {});
|
| 838 |
+
}
|
| 839 |
+
} catch {}
|
| 840 |
+
}
|
| 841 |
}
|
| 842 |
} catch (e) {
|
| 843 |
continue;
|
|
|
|
| 850 |
}
|
| 851 |
} catch (e) {
|
| 852 |
// dir doesn't exist yet or unreadable
|
| 853 |
+
console.error("HERE >>>", e);
|
| 854 |
return null;
|
| 855 |
}
|
| 856 |
}
|
| 857 |
|
| 858 |
+
if (ops === "just_check") {
|
| 859 |
+
if (file_mode) {
|
| 860 |
+
const found_file = await find_latest_valid_file({
|
| 861 |
+
sbe_id,
|
| 862 |
+
picture_quality,
|
| 863 |
+
output_destination,
|
| 864 |
+
S3Dir: S3Dir
|
| 865 |
+
});
|
| 866 |
+
|
| 867 |
+
return {
|
| 868 |
+
passed: found_file
|
| 869 |
+
? true
|
| 870 |
+
: (() => {
|
| 871 |
+
if (!last_failed) {
|
| 872 |
+
return { passed: true };
|
| 873 |
+
}
|
| 874 |
+
return false;
|
| 875 |
+
})()
|
| 876 |
+
};
|
| 877 |
+
}
|
| 878 |
+
|
| 879 |
+
return {};
|
| 880 |
+
}
|
| 881 |
+
|
| 882 |
if (abort_signal) {
|
| 883 |
if (abort_signal.aborted) {
|
| 884 |
kill_controller();
|
| 885 |
+
return
|
| 886 |
} else {
|
| 887 |
abort_signal.addEventListener(
|
| 888 |
"abort",
|
|
|
|
| 896 |
}
|
| 897 |
}
|
| 898 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 899 |
console.log("\n", "PINGING FFMPEG NOW", "\n");
|
| 900 |
|
| 901 |
// -------------------------------
|
|
|
|
| 1013 |
return proc;
|
| 1014 |
})();
|
| 1015 |
|
| 1016 |
+
bun_proc.killed;
|
| 1017 |
+
|
| 1018 |
const logged_proc_errors: string[] = [];
|
| 1019 |
|
| 1020 |
const has_pipe_3 = (bun_proc as any).extraPipe && bun_proc.stdio[3];
|
|
|
|
| 1110 |
})();
|
| 1111 |
|
| 1112 |
if (APP_ENV === "dev") {
|
| 1113 |
+
console.log(
|
| 1114 |
+
"[ffmpeg PROGRESS]:",
|
| 1115 |
+
{ progress_update, file: og_output_destination },
|
| 1116 |
+
"\n"
|
| 1117 |
+
);
|
| 1118 |
}
|
| 1119 |
|
| 1120 |
const ws_client_id = ctx.request.headers.get("x-ws-client-id");
|
|
|
|
| 1133 |
(() => {
|
| 1134 |
const percent = (progress_update as any)?.percentage;
|
| 1135 |
if (percent && parseFloat(`${percent}`) && file_mode) {
|
| 1136 |
+
task_processing({
|
| 1137 |
+
percent: parseFloat(`${percent}`),
|
| 1138 |
+
size: (forward_progress as any)?.current_filesize,
|
| 1139 |
+
progress_endpoint: progress_update_endpoint
|
| 1140 |
+
});
|
| 1141 |
|
| 1142 |
if (parseFloat(`${percent}`) >= 100) {
|
| 1143 |
task_finished();
|
|
|
|
| 1198 |
})();
|
| 1199 |
|
| 1200 |
const { done, value } = await reader.read();
|
| 1201 |
+
|
| 1202 |
if (done) {
|
| 1203 |
task_finished();
|
| 1204 |
|
| 1205 |
console.log("\n");
|
| 1206 |
console.log(
|
| 1207 |
`FFMPEG finished -> ${schemed_data?.current_filesize} ${tmp_download_path}`,
|
| 1208 |
+
"\n",
|
| 1209 |
+
bun_proc.resourceUsage(),
|
| 1210 |
"\n"
|
| 1211 |
);
|
| 1212 |
break;
|
|
|
|
| 1218 |
}
|
| 1219 |
} catch (e) {
|
| 1220 |
console.error("Error reading stdout:", e);
|
| 1221 |
+
task_failed();
|
| 1222 |
}
|
| 1223 |
return;
|
| 1224 |
}
|
|
|
|
| 1256 |
|
| 1257 |
if (code === 0) {
|
| 1258 |
console.log(
|
| 1259 |
+
`FFMPEG finished -> ${schemed_data?.current_filesize} ${og_output_destination || output_destination}`,
|
| 1260 |
+
"\n",
|
| 1261 |
+
bun_proc.resourceUsage(),
|
| 1262 |
"\n"
|
| 1263 |
);
|
| 1264 |
} else {
|