@mux/ai 0.55.0 → 0.57.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{index-BBk0AcY1.d.ts → index-CuL8ReZ7.d.ts} +40 -8
- package/dist/{index-B-rLy1do.d.ts → index-DiuqBw6I.d.ts} +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +327 -135
- package/dist/index.js.map +1 -1
- package/dist/primitives/index.d.ts +2 -2
- package/dist/primitives/index.js +44 -9
- package/dist/primitives/index.js.map +1 -1
- package/dist/{transcripts-DxSHvTk9.d.ts → transcripts-Brw74qlV.d.ts} +7 -1
- package/dist/workflows/index.d.ts +2 -2
- package/dist/workflows/index.js +326 -134
- package/dist/workflows/index.js.map +1 -1
- package/package.json +3 -2
package/dist/workflows/index.js
CHANGED
|
@@ -2029,6 +2029,11 @@ function isAudioOnlyAsset(asset) {
|
|
|
2029
2029
|
const hasVideoTrack = asset.tracks?.some((track) => track.type === "video") ?? false;
|
|
2030
2030
|
return hasAudioTrack && !hasVideoTrack;
|
|
2031
2031
|
}
|
|
2032
|
+
function isVideoOnlyAsset(asset) {
|
|
2033
|
+
const hasAudioTrack = asset.tracks?.some((track) => track.type === "audio") ?? false;
|
|
2034
|
+
const hasVideoTrack = asset.tracks?.some((track) => track.type === "video") ?? false;
|
|
2035
|
+
return hasVideoTrack && !hasAudioTrack;
|
|
2036
|
+
}
|
|
2032
2037
|
function toPlaybackAsset(asset) {
|
|
2033
2038
|
const { id: playbackId, policy } = getPlaybackId(asset);
|
|
2034
2039
|
return { asset, playbackId, policy };
|
|
@@ -2440,8 +2445,38 @@ var ISO639_1_TO_3 = {
|
|
|
2440
2445
|
// Estonian
|
|
2441
2446
|
lv: "lav",
|
|
2442
2447
|
// Latvian
|
|
2443
|
-
lt: "lit"
|
|
2448
|
+
lt: "lit",
|
|
2444
2449
|
// Lithuanian
|
|
2450
|
+
as: "asm",
|
|
2451
|
+
az: "aze",
|
|
2452
|
+
be: "bel",
|
|
2453
|
+
bs: "bos",
|
|
2454
|
+
cy: "cym",
|
|
2455
|
+
ga: "gle",
|
|
2456
|
+
ha: "hau",
|
|
2457
|
+
hy: "hye",
|
|
2458
|
+
jv: "jav",
|
|
2459
|
+
ka: "kat",
|
|
2460
|
+
kk: "kaz",
|
|
2461
|
+
ky: "kir",
|
|
2462
|
+
lb: "ltz",
|
|
2463
|
+
ln: "lin",
|
|
2464
|
+
mi: "mri",
|
|
2465
|
+
mk: "mkd",
|
|
2466
|
+
mn: "mon",
|
|
2467
|
+
mt: "mlt",
|
|
2468
|
+
my: "mya",
|
|
2469
|
+
ne: "nep",
|
|
2470
|
+
ny: "nya",
|
|
2471
|
+
oc: "oci",
|
|
2472
|
+
or: "ori",
|
|
2473
|
+
ps: "pus",
|
|
2474
|
+
sd: "snd",
|
|
2475
|
+
so: "som",
|
|
2476
|
+
sr: "srp",
|
|
2477
|
+
tg: "tgk",
|
|
2478
|
+
uz: "uzb",
|
|
2479
|
+
yo: "yor"
|
|
2445
2480
|
};
|
|
2446
2481
|
var ISO639_3_TO_1 = Object.fromEntries(
|
|
2447
2482
|
Object.entries(ISO639_1_TO_3).map(([iso1, iso3]) => [iso3, iso1])
|
|
@@ -2458,7 +2493,7 @@ function toISO639_1(code) {
|
|
|
2458
2493
|
if (normalized.length === 2) {
|
|
2459
2494
|
return normalized;
|
|
2460
2495
|
}
|
|
2461
|
-
return ISO639_3_TO_1[normalized]
|
|
2496
|
+
return Object.prototype.hasOwnProperty.call(ISO639_3_TO_1, normalized) ? ISO639_3_TO_1[normalized] : normalized;
|
|
2462
2497
|
}
|
|
2463
2498
|
function getLanguageCodePair(code) {
|
|
2464
2499
|
const normalized = code.toLowerCase().trim();
|
|
@@ -2670,14 +2705,13 @@ function extractTextFromVTT(vttContent) {
|
|
|
2670
2705
|
textLines.join(" ").replace(/\s+/g, " ").trim()
|
|
2671
2706
|
);
|
|
2672
2707
|
}
|
|
2708
|
+
var VTT_TIMESTAMP_RE = /^(?:(\d+):)?(\d{2}):(\d{2})(?:\.(\d{3}))?$/;
|
|
2673
2709
|
function vttTimestampToSeconds(timestamp) {
|
|
2674
|
-
const
|
|
2675
|
-
if (
|
|
2676
|
-
return 0;
|
|
2677
|
-
const hours =
|
|
2678
|
-
|
|
2679
|
-
const seconds = Number.parseFloat(parts[2]) || 0;
|
|
2680
|
-
return hours * 3600 + minutes * 60 + seconds;
|
|
2710
|
+
const match = VTT_TIMESTAMP_RE.exec(timestamp);
|
|
2711
|
+
if (!match)
|
|
2712
|
+
return void 0;
|
|
2713
|
+
const [, hours = "0", minutes, seconds, millis = "0"] = match;
|
|
2714
|
+
return Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds) + Number(millis) / 1e3;
|
|
2681
2715
|
}
|
|
2682
2716
|
function secondsToTimestamp(seconds) {
|
|
2683
2717
|
const rounded = Math.max(0, Math.floor(seconds));
|
|
@@ -2700,6 +2734,8 @@ function extractTimestampedTranscript(vttContent) {
|
|
|
2700
2734
|
if (line.includes("-->")) {
|
|
2701
2735
|
const startTime = line.split(" --> ")[0].trim();
|
|
2702
2736
|
const timeInSeconds = vttTimestampToSeconds(startTime);
|
|
2737
|
+
if (timeInSeconds === void 0)
|
|
2738
|
+
continue;
|
|
2703
2739
|
let j = i + 1;
|
|
2704
2740
|
while (j < lines.length && !lines[j].trim()) {
|
|
2705
2741
|
j++;
|
|
@@ -2728,13 +2764,17 @@ function parseVTTCues(vttContent, options = {}) {
|
|
|
2728
2764
|
if (isTimingLine(line)) {
|
|
2729
2765
|
const [startStr, endStr] = line.split(" --> ").map((s) => s.trim());
|
|
2730
2766
|
const startTime = vttTimestampToSeconds(startStr);
|
|
2731
|
-
const endTime = vttTimestampToSeconds(endStr
|
|
2767
|
+
const endTime = vttTimestampToSeconds(endStr?.split(/\s+/)[0] ?? "");
|
|
2732
2768
|
const currentCueIdentifierLine = lines[i - 1]?.trim() ?? "";
|
|
2733
2769
|
const currentCueIdentifier = isLikelyCueIdentifier({
|
|
2734
2770
|
line: currentCueIdentifierLine,
|
|
2735
2771
|
nextLine: line,
|
|
2736
2772
|
previousCueIdentifier
|
|
2737
2773
|
}) ? parseNumericCueIdentifier(currentCueIdentifierLine) : null;
|
|
2774
|
+
if (startTime === void 0 || endTime === void 0) {
|
|
2775
|
+
previousCueIdentifier = currentCueIdentifier;
|
|
2776
|
+
continue;
|
|
2777
|
+
}
|
|
2738
2778
|
const rawTextLines = [];
|
|
2739
2779
|
let j = i + 1;
|
|
2740
2780
|
while (j < lines.length && lines[j].trim() && !isTimingLine(lines[j].trim())) {
|
|
@@ -4837,7 +4877,7 @@ function transformCueText(rawVtt, transform) {
|
|
|
4837
4877
|
const transformed = lines.map((line) => {
|
|
4838
4878
|
if (line.includes("-->")) {
|
|
4839
4879
|
const startTimestamp = line.split("-->")[0].trim();
|
|
4840
|
-
currentCueStartTime = vttTimestampToSeconds(startTimestamp);
|
|
4880
|
+
currentCueStartTime = vttTimestampToSeconds(startTimestamp) ?? currentCueStartTime;
|
|
4841
4881
|
inCueText = true;
|
|
4842
4882
|
return line;
|
|
4843
4883
|
}
|
|
@@ -4869,7 +4909,7 @@ function transformCueTextBlocks(rawVtt, transform) {
|
|
|
4869
4909
|
if (line.includes("-->")) {
|
|
4870
4910
|
flushCueText();
|
|
4871
4911
|
const startTimestamp = line.split("-->")[0].trim();
|
|
4872
|
-
currentCueStartTime = vttTimestampToSeconds(startTimestamp);
|
|
4912
|
+
currentCueStartTime = vttTimestampToSeconds(startTimestamp) ?? currentCueStartTime;
|
|
4873
4913
|
transformed.push(line);
|
|
4874
4914
|
cueTextLines = [];
|
|
4875
4915
|
continue;
|
|
@@ -8808,48 +8848,73 @@ import { sleep } from "workflow";
|
|
|
8808
8848
|
|
|
8809
8849
|
// src/lib/elevenlabs-errors.ts
|
|
8810
8850
|
var MAX_ERROR_BODY_CHARS = 500;
|
|
8811
|
-
async function elevenLabsResponseError(context, response) {
|
|
8812
|
-
const error = new Error(`${context}: ${await describeElevenLabsErrorResponse(response)}`);
|
|
8813
|
-
if (!isRetryableElevenLabsStatus(response.status)) {
|
|
8814
|
-
error.name = "FatalError";
|
|
8815
|
-
}
|
|
8816
|
-
return error;
|
|
8817
|
-
}
|
|
8818
8851
|
function isRetryableElevenLabsStatus(status) {
|
|
8819
8852
|
return status === 408 || status === 425 || status === 429 || status >= 500;
|
|
8820
8853
|
}
|
|
8821
|
-
async function
|
|
8822
|
-
const statusLine = [response.status, response.statusText].filter(Boolean).join(" ");
|
|
8854
|
+
async function elevenLabsResponseFailure(response) {
|
|
8823
8855
|
const body = await response.text().catch(() => "");
|
|
8824
|
-
|
|
8825
|
-
|
|
8856
|
+
return httpFailure(response.status, response.statusText, body);
|
|
8857
|
+
}
|
|
8858
|
+
function elevenLabsSdkFailure(error) {
|
|
8859
|
+
const failure = error;
|
|
8860
|
+
if (typeof failure?.statusCode === "number") {
|
|
8861
|
+
const body = typeof failure.body === "string" ? failure.body : JSON.stringify(failure.body ?? "");
|
|
8862
|
+
return httpFailure(failure.statusCode, failure.rawResponse?.statusText, body);
|
|
8863
|
+
}
|
|
8864
|
+
const message = failure?.message ?? String(error);
|
|
8865
|
+
switch (failure?.name) {
|
|
8866
|
+
case "ElevenLabsTimeoutError":
|
|
8867
|
+
return { reason: "timeout", retryable: true, detail: message };
|
|
8868
|
+
// The SDK validates responses strictly: the API's response no longer matches
|
|
8869
|
+
// this SDK version, so the same call fails the same way.
|
|
8870
|
+
case "ParseError":
|
|
8871
|
+
return { reason: "unexpected_response", retryable: false, detail: `Unexpected ElevenLabs response: ${message}` };
|
|
8872
|
+
// The SDK's own error without a status: the request got no response.
|
|
8873
|
+
case "ElevenLabsError":
|
|
8874
|
+
return { reason: "network_error", retryable: true, detail: message };
|
|
8875
|
+
default:
|
|
8876
|
+
return { retryable: false, detail: message };
|
|
8877
|
+
}
|
|
8826
8878
|
}
|
|
8827
|
-
function
|
|
8879
|
+
function httpFailure(status, statusText, body) {
|
|
8880
|
+
const statusLine = [status, statusText].filter(Boolean).join(" ");
|
|
8881
|
+
const { reason, detail } = parseElevenLabsErrorBody(body);
|
|
8882
|
+
return {
|
|
8883
|
+
httpStatus: status,
|
|
8884
|
+
reason,
|
|
8885
|
+
retryable: isRetryableElevenLabsStatus(status),
|
|
8886
|
+
detail: detail ? `${statusLine}: ${detail}` : statusLine
|
|
8887
|
+
};
|
|
8888
|
+
}
|
|
8889
|
+
function parseElevenLabsErrorBody(body) {
|
|
8828
8890
|
const trimmed = body.trim();
|
|
8829
8891
|
if (!trimmed) {
|
|
8830
|
-
return "";
|
|
8892
|
+
return { detail: "" };
|
|
8831
8893
|
}
|
|
8832
8894
|
let detail;
|
|
8833
8895
|
try {
|
|
8834
8896
|
detail = JSON.parse(trimmed).detail;
|
|
8835
8897
|
} catch {
|
|
8836
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
8898
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
8837
8899
|
}
|
|
8838
8900
|
if (typeof detail === "string" && detail) {
|
|
8839
|
-
return detail;
|
|
8901
|
+
return { detail };
|
|
8840
8902
|
}
|
|
8841
8903
|
if (Array.isArray(detail) && detail.length > 0) {
|
|
8842
|
-
return
|
|
8904
|
+
return {
|
|
8905
|
+
detail: detail.map((item) => [item.loc?.join("."), item.msg].filter(Boolean).join(": ")).join("; ")
|
|
8906
|
+
};
|
|
8843
8907
|
}
|
|
8844
8908
|
if (detail && typeof detail === "object") {
|
|
8845
8909
|
const { status, code, message, param, request_id: requestId } = detail;
|
|
8846
|
-
const
|
|
8910
|
+
const reasonCode = [status, code].find((value) => typeof value === "string" && value !== "");
|
|
8911
|
+
const reasonText = [reasonCode, message].filter(Boolean).join(": ");
|
|
8847
8912
|
const context = [param && `param: ${param}`, requestId && `request_id: ${requestId}`].filter(Boolean).join(", ");
|
|
8848
|
-
if (
|
|
8849
|
-
return context ? `${
|
|
8913
|
+
if (reasonText) {
|
|
8914
|
+
return { reason: reasonCode, detail: context ? `${reasonText} (${context})` : reasonText };
|
|
8850
8915
|
}
|
|
8851
8916
|
}
|
|
8852
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
8917
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
8853
8918
|
}
|
|
8854
8919
|
|
|
8855
8920
|
// src/lib/mux-static-renditions.ts
|
|
@@ -8909,6 +8974,9 @@ var STATIC_RENDITION_POLL_INTERVAL_MS = 1e4;
|
|
|
8909
8974
|
var STATIC_RENDITION_MAX_ATTEMPTS = 180;
|
|
8910
8975
|
var MAX_DUBBING_SOURCE_BYTES = 1e9;
|
|
8911
8976
|
var DUBBING_POLL_INTERVAL_MS = 1e4;
|
|
8977
|
+
var MAX_CONSECUTIVE_STATUS_CHECK_FAILURES = 6;
|
|
8978
|
+
var DUBBED_AUDIO_DOWNLOAD_ATTEMPTS = 3;
|
|
8979
|
+
var DUBBED_AUDIO_DOWNLOAD_RETRY_DELAY_MS = 1e4;
|
|
8912
8980
|
var DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS = 7200;
|
|
8913
8981
|
function getReadyAudioStaticRendition(asset) {
|
|
8914
8982
|
const files = asset.static_renditions?.files;
|
|
@@ -9042,64 +9110,128 @@ async function waitForLegacyMp4SupportRendition({
|
|
|
9042
9110
|
}
|
|
9043
9111
|
return rendition.name;
|
|
9044
9112
|
}
|
|
9113
|
+
function toBaseLanguageCode(code) {
|
|
9114
|
+
return code.trim().split(/[-_]/)[0];
|
|
9115
|
+
}
|
|
9116
|
+
function audioTrackUnsupportedReason(asset) {
|
|
9117
|
+
const useDubbedFile = "skip uploading to Mux and use the dubbed audio file directly.";
|
|
9118
|
+
if (asset.ingest_type === "on_demand_clip" || asset.source_asset_id) {
|
|
9119
|
+
return `is a clip, and Mux does not support adding audio tracks to clipped assets. Dub the source asset instead, or ${useDubbedFile}`;
|
|
9120
|
+
}
|
|
9121
|
+
if (asset.is_live || asset.live_stream_id) {
|
|
9122
|
+
return `comes from a live stream, and Mux does not support adding audio tracks to live stream assets. Instead, ${useDubbedFile}`;
|
|
9123
|
+
}
|
|
9124
|
+
if (isAudioOnlyAsset(asset)) {
|
|
9125
|
+
return `is audio-only, and Mux does not support adding audio tracks to audio-only assets. Instead, ${useDubbedFile}`;
|
|
9126
|
+
}
|
|
9127
|
+
return void 0;
|
|
9128
|
+
}
|
|
9129
|
+
function dubbingProviderError(message, failure) {
|
|
9130
|
+
if (failure.reason === "timeout") {
|
|
9131
|
+
return new MuxAiError(`${message} The dubbing service timed out. Please try again.`, { type: "timeout_error", retryable: true });
|
|
9132
|
+
}
|
|
9133
|
+
if (failure.retryable) {
|
|
9134
|
+
return new MuxAiError(`${message} The dubbing service is temporarily unavailable. Please try again.`, { type: "processing_error", retryable: true });
|
|
9135
|
+
}
|
|
9136
|
+
return new MuxAiError(message, { type: "processing_error" });
|
|
9137
|
+
}
|
|
9138
|
+
function dubbingStartError(failure, languages) {
|
|
9139
|
+
if (failure.reason === "unsupported_target_language") {
|
|
9140
|
+
return new MuxAiError(`Audio dubbing does not support the target language "${languages.target}".`, { type: "validation_error" });
|
|
9141
|
+
}
|
|
9142
|
+
if (failure.reason === "unsupported_source_language" && languages.source) {
|
|
9143
|
+
return new MuxAiError(`Audio dubbing does not support the source language "${languages.source}".`, { type: "validation_error" });
|
|
9144
|
+
}
|
|
9145
|
+
return dubbingProviderError("Audio dubbing could not be started.", failure);
|
|
9146
|
+
}
|
|
9045
9147
|
async function createElevenLabsDubbingJob({
|
|
9046
9148
|
sourceUrl,
|
|
9047
9149
|
assetId,
|
|
9048
9150
|
elevenLabsLangCode,
|
|
9049
9151
|
elevenLabsSourceLangCode,
|
|
9050
|
-
numSpeakers,
|
|
9051
9152
|
credentials
|
|
9052
9153
|
}) {
|
|
9053
9154
|
"use step";
|
|
9054
|
-
const
|
|
9055
|
-
const
|
|
9056
|
-
|
|
9057
|
-
|
|
9058
|
-
|
|
9059
|
-
|
|
9060
|
-
|
|
9061
|
-
|
|
9062
|
-
|
|
9063
|
-
|
|
9064
|
-
|
|
9065
|
-
|
|
9066
|
-
|
|
9067
|
-
|
|
9068
|
-
|
|
9069
|
-
|
|
9070
|
-
}
|
|
9071
|
-
|
|
9072
|
-
})
|
|
9073
|
-
|
|
9074
|
-
throw await elevenLabsResponseError("ElevenLabs API error", dubbingResponse);
|
|
9155
|
+
const { ElevenLabsClient } = await import("@elevenlabs/elevenlabs-js");
|
|
9156
|
+
const client = new ElevenLabsClient({ apiKey: await getApiKeyFromEnv("elevenlabs", credentials), maxRetries: 0 });
|
|
9157
|
+
try {
|
|
9158
|
+
const project = await client.dubbing.project.create({
|
|
9159
|
+
sourceUrl,
|
|
9160
|
+
sourceLanguage: elevenLabsSourceLangCode,
|
|
9161
|
+
targetLanguage: elevenLabsLangCode,
|
|
9162
|
+
modelId: "dubbing_v1",
|
|
9163
|
+
reference: `Mux Asset ${assetId} - ${elevenLabsSourceLangCode ?? "auto"} to ${elevenLabsLangCode}`
|
|
9164
|
+
});
|
|
9165
|
+
const languageId = project.languageIds?.[0];
|
|
9166
|
+
if (!languageId) {
|
|
9167
|
+
return {
|
|
9168
|
+
kind: "failed",
|
|
9169
|
+
failure: { retryable: false, detail: `ElevenLabs project ${project.projectId} returned no language target. Check the project before retrying.` }
|
|
9170
|
+
};
|
|
9171
|
+
}
|
|
9172
|
+
return { kind: "created", projectId: project.projectId, languageId };
|
|
9173
|
+
} catch (error) {
|
|
9174
|
+
return { kind: "failed", failure: elevenLabsSdkFailure(error) };
|
|
9075
9175
|
}
|
|
9076
|
-
const dubbingData = await dubbingResponse.json();
|
|
9077
|
-
return dubbingData.dubbing_id;
|
|
9078
9176
|
}
|
|
9079
9177
|
async function checkElevenLabsDubbingStatus({
|
|
9080
9178
|
dubbingId,
|
|
9179
|
+
languageId,
|
|
9081
9180
|
credentials
|
|
9082
9181
|
}) {
|
|
9083
9182
|
"use step";
|
|
9084
|
-
const
|
|
9085
|
-
const
|
|
9086
|
-
|
|
9087
|
-
|
|
9183
|
+
const { ElevenLabsClient } = await import("@elevenlabs/elevenlabs-js");
|
|
9184
|
+
const client = new ElevenLabsClient({ apiKey: await getApiKeyFromEnv("elevenlabs", credentials), maxRetries: 0 });
|
|
9185
|
+
try {
|
|
9186
|
+
const [project, language] = await Promise.all([
|
|
9187
|
+
client.dubbing.project.get(dubbingId),
|
|
9188
|
+
client.dubbing.project.language.get(dubbingId, languageId)
|
|
9189
|
+
]);
|
|
9190
|
+
return {
|
|
9191
|
+
kind: "status",
|
|
9192
|
+
status: project.status === "failed" ? "failed" : language.status,
|
|
9193
|
+
// A language error of `project_failed` only points at the project, so prefer the project's.
|
|
9194
|
+
error: project.error ?? language.error
|
|
9195
|
+
};
|
|
9196
|
+
} catch (error) {
|
|
9197
|
+
return { kind: "failed", failure: elevenLabsSdkFailure(error) };
|
|
9198
|
+
}
|
|
9199
|
+
}
|
|
9200
|
+
async function fetchDubbingOutput(dubbingId, languageId, output, credentials) {
|
|
9201
|
+
const { ElevenLabsClient } = await import("@elevenlabs/elevenlabs-js");
|
|
9202
|
+
const client = new ElevenLabsClient({ apiKey: await getApiKeyFromEnv("elevenlabs", credentials), maxRetries: 0 });
|
|
9203
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
9204
|
+
let url;
|
|
9205
|
+
try {
|
|
9206
|
+
const target = await client.dubbing.project.language.get(dubbingId, languageId);
|
|
9207
|
+
url = target.outputs?.[output];
|
|
9208
|
+
} catch (error) {
|
|
9209
|
+
return { kind: "failed", failure: elevenLabsSdkFailure(error) };
|
|
9210
|
+
}
|
|
9211
|
+
if (!url) {
|
|
9212
|
+
return {
|
|
9213
|
+
kind: "failed",
|
|
9214
|
+
failure: { reason: "output_missing", retryable: true, detail: `ElevenLabs language ${languageId} of project ${dubbingId} has no ${output} output` }
|
|
9215
|
+
};
|
|
9216
|
+
}
|
|
9217
|
+
let response;
|
|
9218
|
+
try {
|
|
9219
|
+
response = await fetch(url);
|
|
9220
|
+
} catch (error) {
|
|
9221
|
+
return { kind: "failed", failure: { reason: "network_error", retryable: true, detail: `Failed to fetch dubbed ${output}: ${error instanceof Error ? error.message : String(error)}` } };
|
|
9222
|
+
}
|
|
9223
|
+
if (response.ok) {
|
|
9224
|
+
return { kind: "fetched", response, url };
|
|
9225
|
+
}
|
|
9226
|
+
if (response.status !== 403 || attempt === 1) {
|
|
9227
|
+
return { kind: "failed", failure: await elevenLabsResponseFailure(response) };
|
|
9088
9228
|
}
|
|
9089
|
-
});
|
|
9090
|
-
if (!statusResponse.ok) {
|
|
9091
|
-
throw await elevenLabsResponseError("Status check failed", statusResponse);
|
|
9092
9229
|
}
|
|
9093
|
-
|
|
9094
|
-
return {
|
|
9095
|
-
status: statusData.status,
|
|
9096
|
-
targetLanguages: statusData.target_languages ?? [],
|
|
9097
|
-
error: typeof statusData.error === "string" && statusData.error ? statusData.error : void 0
|
|
9098
|
-
};
|
|
9230
|
+
return { kind: "failed", failure: { retryable: false, detail: `Failed to fetch dubbed ${output}` } };
|
|
9099
9231
|
}
|
|
9100
9232
|
async function downloadAndUploadDubbedAudio({
|
|
9101
9233
|
dubbingId,
|
|
9102
|
-
|
|
9234
|
+
languageId,
|
|
9103
9235
|
assetId,
|
|
9104
9236
|
toLanguageCode,
|
|
9105
9237
|
s3Endpoint,
|
|
@@ -9110,20 +9242,32 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9110
9242
|
credentials
|
|
9111
9243
|
}) {
|
|
9112
9244
|
"use step";
|
|
9113
|
-
const
|
|
9114
|
-
|
|
9115
|
-
|
|
9116
|
-
|
|
9117
|
-
|
|
9118
|
-
|
|
9119
|
-
|
|
9120
|
-
|
|
9121
|
-
|
|
9245
|
+
const fetched = await fetchDubbingOutput(dubbingId, languageId, "losslessAudio", credentials);
|
|
9246
|
+
if (fetched.kind === "failed") {
|
|
9247
|
+
return fetched;
|
|
9248
|
+
}
|
|
9249
|
+
const { response: audioResponse, url } = fetched;
|
|
9250
|
+
const mime = audioResponse.headers.get("content-type")?.split(";")[0].trim();
|
|
9251
|
+
const formats = {
|
|
9252
|
+
"audio/flac": "flac",
|
|
9253
|
+
"audio/x-flac": "flac",
|
|
9254
|
+
"audio/wav": "wav",
|
|
9255
|
+
"audio/x-wav": "wav",
|
|
9256
|
+
"audio/mp4": "m4a",
|
|
9257
|
+
"audio/mpeg": "mp3"
|
|
9258
|
+
};
|
|
9259
|
+
const pathExtension = new URL(url).pathname.split(".").pop()?.toLowerCase();
|
|
9260
|
+
const extension = (mime ? formats[mime] : void 0) ?? Object.values(formats).find((format) => format === pathExtension);
|
|
9261
|
+
if (!extension) {
|
|
9262
|
+
return {
|
|
9263
|
+
kind: "failed",
|
|
9264
|
+
failure: { reason: "unsupported_output_format", retryable: false, detail: `Unsupported dubbed audio content type: ${mime ?? "missing"}` }
|
|
9265
|
+
};
|
|
9122
9266
|
}
|
|
9123
9267
|
const dubbedAudio = new Uint8Array(await audioResponse.arrayBuffer());
|
|
9124
9268
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9125
9269
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
9126
|
-
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}
|
|
9270
|
+
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}.${extension}`;
|
|
9127
9271
|
await putObjectWithStorageAdapter({
|
|
9128
9272
|
accessKeyId: s3AccessKeyId,
|
|
9129
9273
|
secretAccessKey: s3SecretAccessKey,
|
|
@@ -9132,7 +9276,7 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9132
9276
|
bucket: s3Bucket,
|
|
9133
9277
|
key: audioKey,
|
|
9134
9278
|
body: dubbedAudio,
|
|
9135
|
-
contentType:
|
|
9279
|
+
contentType: Object.keys(formats).find((type) => formats[type] === extension)
|
|
9136
9280
|
}, storageAdapter);
|
|
9137
9281
|
const presignedUrl = await createPresignedGetUrlWithStorageAdapter({
|
|
9138
9282
|
accessKeyId: s3AccessKeyId,
|
|
@@ -9146,11 +9290,11 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9146
9290
|
const expiryHours = Math.round((s3SignedUrlExpirySeconds ?? 86400) / 3600);
|
|
9147
9291
|
console.warn(`\u2705 Audio uploaded successfully to: ${audioKey}`);
|
|
9148
9292
|
console.warn(`\u{1F517} Generated presigned URL (expires in ${expiryHours} hour${expiryHours === 1 ? "" : "s"})`);
|
|
9149
|
-
return presignedUrl;
|
|
9293
|
+
return { kind: "staged", presignedUrl };
|
|
9150
9294
|
}
|
|
9151
9295
|
async function downloadAndUploadDubTranscript({
|
|
9152
9296
|
dubbingId,
|
|
9153
|
-
|
|
9297
|
+
languageId,
|
|
9154
9298
|
assetId,
|
|
9155
9299
|
toLanguageCode,
|
|
9156
9300
|
s3Endpoint,
|
|
@@ -9161,20 +9305,13 @@ async function downloadAndUploadDubTranscript({
|
|
|
9161
9305
|
credentials
|
|
9162
9306
|
}) {
|
|
9163
9307
|
"use step";
|
|
9164
|
-
const
|
|
9165
|
-
|
|
9166
|
-
|
|
9167
|
-
headers: {
|
|
9168
|
-
"xi-api-key": elevenLabsApiKey
|
|
9169
|
-
}
|
|
9170
|
-
});
|
|
9171
|
-
if (!transcriptResponse.ok) {
|
|
9172
|
-
throw await elevenLabsResponseError("Failed to fetch dub transcript", transcriptResponse);
|
|
9308
|
+
const fetched = await fetchDubbingOutput(dubbingId, languageId, "subtitles", credentials);
|
|
9309
|
+
if (fetched.kind === "failed") {
|
|
9310
|
+
return fetched;
|
|
9173
9311
|
}
|
|
9174
|
-
const
|
|
9175
|
-
|
|
9176
|
-
|
|
9177
|
-
throw new Error("Dub transcript response did not include webvtt content");
|
|
9312
|
+
const transcriptVtt = await fetched.response.text();
|
|
9313
|
+
if (!transcriptVtt.trim()) {
|
|
9314
|
+
return { kind: "failed", failure: { retryable: false, detail: "Dub transcript response did not include webvtt content" } };
|
|
9178
9315
|
}
|
|
9179
9316
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9180
9317
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
@@ -9199,7 +9336,7 @@ async function downloadAndUploadDubTranscript({
|
|
|
9199
9336
|
expiresInSeconds: s3SignedUrlExpirySeconds ?? 86400
|
|
9200
9337
|
}, storageAdapter);
|
|
9201
9338
|
console.warn(`\u2705 Dub transcript uploaded successfully to: ${vttKey}`);
|
|
9202
|
-
return presignedUrl;
|
|
9339
|
+
return { kind: "staged", presignedUrl };
|
|
9203
9340
|
}
|
|
9204
9341
|
async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
9205
9342
|
"use workflow";
|
|
@@ -9221,12 +9358,16 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9221
9358
|
if (provider !== "elevenlabs") {
|
|
9222
9359
|
throw new MuxAiError("Only ElevenLabs provider is currently supported for audio translation.", { type: "validation_error" });
|
|
9223
9360
|
}
|
|
9361
|
+
const targetLanguageCode = toISO639_1(toLanguageCode);
|
|
9362
|
+
if (numSpeakers !== 0) {
|
|
9363
|
+
throw new MuxAiError("The ElevenLabs project API does not support numSpeakers overrides. Omit it or use 0 for auto-detection.", { type: "validation_error" });
|
|
9364
|
+
}
|
|
9224
9365
|
const credentials = providedCredentials;
|
|
9225
9366
|
const effectiveStorageAdapter = storageAdapter;
|
|
9226
9367
|
const trackPassthrough = validateTrackPassthrough(providedTrackPassthrough) ?? buildMuxAiTrackPassthrough("translate-audio");
|
|
9227
9368
|
const uploadToMux = uploadToMuxOption !== false;
|
|
9228
9369
|
const uploadToS3 = uploadToS3Option || uploadToMux || uploadCaptionsToMux;
|
|
9229
|
-
const muxLangCode =
|
|
9370
|
+
const muxLangCode = targetLanguageCode;
|
|
9230
9371
|
const trackName = providedTrackName ?? `${getLanguageName(muxLangCode) ?? muxLangCode.toUpperCase()} (Auto-dubbed)`;
|
|
9231
9372
|
const audioTrackTarget = { type: "audio", languageCode: muxLangCode, name: trackName };
|
|
9232
9373
|
const captionsTrackTarget = { type: "text", languageCode: muxLangCode, name: trackName };
|
|
@@ -9240,6 +9381,13 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9240
9381
|
}
|
|
9241
9382
|
const { asset: initialAsset, playbackId, policy } = await getPlaybackIdForAsset(assetId, credentials, options.assetSnapshot);
|
|
9242
9383
|
const assetDurationSeconds = getAssetDurationSecondsFromAsset(initialAsset);
|
|
9384
|
+
if (isVideoOnlyAsset(initialAsset)) {
|
|
9385
|
+
throw new MuxAiError(`Asset ${assetId} has no audio track, so there is no audio to dub.`, { type: "validation_error" });
|
|
9386
|
+
}
|
|
9387
|
+
const audioTrackUnsupported = uploadToMux ? audioTrackUnsupportedReason(initialAsset) : void 0;
|
|
9388
|
+
if (audioTrackUnsupported) {
|
|
9389
|
+
throw new MuxAiError(`Asset ${assetId} ${audioTrackUnsupported}`, { type: "validation_error" });
|
|
9390
|
+
}
|
|
9243
9391
|
for (const target of [
|
|
9244
9392
|
...uploadToMux ? [audioTrackTarget] : [],
|
|
9245
9393
|
...uploadCaptionsToMux ? [captionsTrackTarget] : []
|
|
@@ -9263,6 +9411,7 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9263
9411
|
}
|
|
9264
9412
|
let staticRenditionCleanup = "not_created";
|
|
9265
9413
|
let dubbingId;
|
|
9414
|
+
let languageId;
|
|
9266
9415
|
let presignedUrl;
|
|
9267
9416
|
let uploadedTrackId;
|
|
9268
9417
|
let replacedTracks;
|
|
@@ -9297,75 +9446,114 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9297
9446
|
audioUrl = await signUrl(audioUrl, playbackId, "video", void 0, credentials);
|
|
9298
9447
|
}
|
|
9299
9448
|
console.warn("\u{1F399}\uFE0F Creating dubbing job in ElevenLabs...");
|
|
9300
|
-
const elevenLabsLangCode =
|
|
9301
|
-
const
|
|
9302
|
-
const elevenLabsSourceLangCode =
|
|
9449
|
+
const elevenLabsLangCode = toISO639_1(toBaseLanguageCode(toLanguageCode));
|
|
9450
|
+
const sourceLanguageCode = toISO639_1(toBaseLanguageCode(fromLanguageCode ?? "auto"));
|
|
9451
|
+
const elevenLabsSourceLangCode = sourceLanguageCode && sourceLanguageCode !== "auto" ? sourceLanguageCode : void 0;
|
|
9303
9452
|
console.warn(
|
|
9304
9453
|
`\u{1F50D} Creating dubbing job for asset ${assetId}: ${elevenLabsSourceLangCode ?? "auto"} -> ${elevenLabsLangCode}`
|
|
9305
9454
|
);
|
|
9455
|
+
let created;
|
|
9306
9456
|
try {
|
|
9307
|
-
|
|
9457
|
+
created = await createElevenLabsDubbingJob({
|
|
9308
9458
|
sourceUrl: audioUrl,
|
|
9309
9459
|
assetId,
|
|
9310
9460
|
elevenLabsLangCode,
|
|
9311
9461
|
elevenLabsSourceLangCode,
|
|
9312
|
-
numSpeakers,
|
|
9313
9462
|
credentials
|
|
9314
9463
|
});
|
|
9315
|
-
console.warn(`\u2705 Dubbing job created with ID: ${dubbingId}`);
|
|
9316
9464
|
} catch (error) {
|
|
9317
9465
|
wrapError(error, "Failed to create ElevenLabs dubbing job");
|
|
9318
9466
|
}
|
|
9467
|
+
if (created.kind === "failed") {
|
|
9468
|
+
const { failure } = created;
|
|
9469
|
+
const mayHaveCreated = failure.reason === "timeout" || failure.reason === "network_error" || failure.reason === "unexpected_response";
|
|
9470
|
+
console.warn(
|
|
9471
|
+
`Failed to create ElevenLabs dubbing project for asset ${assetId}: ${failure.detail}${mayHaveCreated ? " The project may have been created; check ElevenLabs before retrying." : ""}`
|
|
9472
|
+
);
|
|
9473
|
+
throw dubbingStartError(failure, { target: elevenLabsLangCode, source: elevenLabsSourceLangCode });
|
|
9474
|
+
}
|
|
9475
|
+
dubbingId = created.projectId;
|
|
9476
|
+
languageId = created.languageId;
|
|
9477
|
+
console.warn(`\u2705 Dubbing job created with ID: ${dubbingId}`);
|
|
9319
9478
|
console.warn("\u23F3 Waiting for dubbing to complete...");
|
|
9320
9479
|
const dubbingPollTimeoutSeconds = options.dubbingPollTimeoutSeconds ?? DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS;
|
|
9321
9480
|
const maxPollAttempts = Math.max(1, Math.ceil(dubbingPollTimeoutSeconds * 1e3 / DUBBING_POLL_INTERVAL_MS));
|
|
9322
9481
|
let dubbingStatus = "dubbing";
|
|
9323
9482
|
let pollAttempts = 0;
|
|
9324
|
-
let targetLanguages = [];
|
|
9325
9483
|
let dubbingError;
|
|
9326
|
-
|
|
9484
|
+
let consecutiveStatusCheckFailures = 0;
|
|
9485
|
+
while (dubbingStatus !== "completed" && pollAttempts < maxPollAttempts) {
|
|
9327
9486
|
await sleep(DUBBING_POLL_INTERVAL_MS);
|
|
9328
9487
|
pollAttempts++;
|
|
9488
|
+
let statusResult;
|
|
9329
9489
|
try {
|
|
9330
|
-
|
|
9490
|
+
statusResult = await checkElevenLabsDubbingStatus({
|
|
9331
9491
|
dubbingId,
|
|
9492
|
+
languageId,
|
|
9332
9493
|
credentials
|
|
9333
9494
|
});
|
|
9334
|
-
dubbingStatus = statusResult.status;
|
|
9335
|
-
targetLanguages = statusResult.targetLanguages;
|
|
9336
|
-
dubbingError = statusResult.error;
|
|
9337
9495
|
} catch (error) {
|
|
9338
9496
|
wrapError(error, "Failed to check dubbing status");
|
|
9339
9497
|
}
|
|
9498
|
+
if (statusResult.kind === "failed") {
|
|
9499
|
+
const { failure } = statusResult;
|
|
9500
|
+
consecutiveStatusCheckFailures++;
|
|
9501
|
+
console.warn(`Status check for ElevenLabs dubbing project ${dubbingId} failed: ${failure.detail}`);
|
|
9502
|
+
if (!failure.retryable || consecutiveStatusCheckFailures >= MAX_CONSECUTIVE_STATUS_CHECK_FAILURES) {
|
|
9503
|
+
throw dubbingProviderError("Could not check audio dubbing progress.", failure);
|
|
9504
|
+
}
|
|
9505
|
+
continue;
|
|
9506
|
+
}
|
|
9507
|
+
consecutiveStatusCheckFailures = 0;
|
|
9508
|
+
dubbingStatus = statusResult.status;
|
|
9509
|
+
dubbingError = statusResult.error;
|
|
9340
9510
|
if (dubbingStatus === "failed") {
|
|
9341
|
-
console.warn(`\u274C ElevenLabs dubbing
|
|
9342
|
-
|
|
9511
|
+
console.warn(`\u274C ElevenLabs dubbing project ${dubbingId} (language ${languageId}) failed: ${dubbingError ? `${dubbingError.code}: ${dubbingError.message}` : "no error reported"}`);
|
|
9512
|
+
if (!dubbingError) {
|
|
9513
|
+
throw new MuxAiError("Audio dubbing failed.", { type: "processing_error" });
|
|
9514
|
+
}
|
|
9515
|
+
throw new MuxAiError(
|
|
9516
|
+
`Audio dubbing failed: ${dubbingError.message}${dubbingError.retryable ? " Please try again." : ""}`,
|
|
9517
|
+
{ type: "processing_error", retryable: dubbingError.retryable }
|
|
9518
|
+
);
|
|
9343
9519
|
}
|
|
9344
9520
|
}
|
|
9345
|
-
if (dubbingStatus !== "
|
|
9521
|
+
if (dubbingStatus !== "completed") {
|
|
9346
9522
|
throw new MuxAiError("Audio translation timed out or failed. Please try again.", { type: "timeout_error", retryable: true });
|
|
9347
9523
|
}
|
|
9348
9524
|
console.warn("\u2705 Dubbing completed successfully!");
|
|
9349
9525
|
if (uploadToS3) {
|
|
9350
9526
|
console.warn("\u{1F4E5} Downloading dubbed audio from ElevenLabs and staging to S3...");
|
|
9351
|
-
|
|
9352
|
-
|
|
9353
|
-
|
|
9354
|
-
|
|
9355
|
-
|
|
9356
|
-
|
|
9357
|
-
|
|
9358
|
-
|
|
9359
|
-
|
|
9360
|
-
|
|
9361
|
-
|
|
9362
|
-
|
|
9363
|
-
|
|
9364
|
-
|
|
9365
|
-
|
|
9366
|
-
|
|
9367
|
-
|
|
9527
|
+
for (let attempt = 1; !presignedUrl; attempt++) {
|
|
9528
|
+
let staged;
|
|
9529
|
+
try {
|
|
9530
|
+
staged = await downloadAndUploadDubbedAudio({
|
|
9531
|
+
dubbingId,
|
|
9532
|
+
languageId,
|
|
9533
|
+
assetId,
|
|
9534
|
+
toLanguageCode,
|
|
9535
|
+
s3Endpoint,
|
|
9536
|
+
s3Region,
|
|
9537
|
+
s3Bucket,
|
|
9538
|
+
storageAdapter: effectiveStorageAdapter,
|
|
9539
|
+
s3SignedUrlExpirySeconds: options.s3SignedUrlExpirySeconds,
|
|
9540
|
+
credentials
|
|
9541
|
+
});
|
|
9542
|
+
} catch (error) {
|
|
9543
|
+
wrapError(error, "Failed to download and upload dubbed audio");
|
|
9544
|
+
}
|
|
9545
|
+
if (staged.kind === "staged") {
|
|
9546
|
+
presignedUrl = staged.presignedUrl;
|
|
9547
|
+
break;
|
|
9548
|
+
}
|
|
9549
|
+
const { failure } = staged;
|
|
9550
|
+
console.warn(`Downloading dubbed audio for ElevenLabs dubbing project ${dubbingId} failed (attempt ${attempt}): ${failure.detail}`);
|
|
9551
|
+
if (!failure.retryable || attempt >= DUBBED_AUDIO_DOWNLOAD_ATTEMPTS) {
|
|
9552
|
+
throw dubbingProviderError("Could not download the dubbed audio.", failure);
|
|
9553
|
+
}
|
|
9554
|
+
await sleep(DUBBED_AUDIO_DOWNLOAD_RETRY_DELAY_MS);
|
|
9368
9555
|
}
|
|
9556
|
+
console.warn("\u2705 Dubbed audio staged to S3 successfully!");
|
|
9369
9557
|
if (uploadToMux) {
|
|
9370
9558
|
console.warn("\u{1F4F9} Adding dubbed audio track to Mux asset...");
|
|
9371
9559
|
let outcome;
|
|
@@ -9394,9 +9582,9 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9394
9582
|
}
|
|
9395
9583
|
try {
|
|
9396
9584
|
console.warn("\u{1F4E5} Downloading dub transcript from ElevenLabs and staging to S3...");
|
|
9397
|
-
|
|
9585
|
+
const transcript = await downloadAndUploadDubTranscript({
|
|
9398
9586
|
dubbingId,
|
|
9399
|
-
|
|
9587
|
+
languageId,
|
|
9400
9588
|
assetId,
|
|
9401
9589
|
toLanguageCode,
|
|
9402
9590
|
s3Endpoint,
|
|
@@ -9406,6 +9594,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9406
9594
|
s3SignedUrlExpirySeconds: options.s3SignedUrlExpirySeconds,
|
|
9407
9595
|
credentials
|
|
9408
9596
|
});
|
|
9597
|
+
if (transcript.kind === "failed") {
|
|
9598
|
+
throw new Error(transcript.failure.detail);
|
|
9599
|
+
}
|
|
9600
|
+
captionsPresignedUrl = transcript.presignedUrl;
|
|
9409
9601
|
if (uploadCaptionsToMux) {
|
|
9410
9602
|
console.warn("\u{1F4F9} Adding dubbed captions track to Mux asset...");
|
|
9411
9603
|
const captionsOutcome = await replaceAndCreateTrack({
|
|
@@ -9445,10 +9637,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9445
9637
|
}
|
|
9446
9638
|
}
|
|
9447
9639
|
}
|
|
9448
|
-
const targetLanguage = getLanguageCodePair(
|
|
9640
|
+
const targetLanguage = getLanguageCodePair(targetLanguageCode);
|
|
9449
9641
|
return {
|
|
9450
9642
|
assetId,
|
|
9451
|
-
targetLanguageCode
|
|
9643
|
+
targetLanguageCode,
|
|
9452
9644
|
targetLanguage,
|
|
9453
9645
|
dubbingId,
|
|
9454
9646
|
uploadedTrackId,
|