@mux/ai 0.56.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-Bav9fNtf.d.ts → index-CuL8ReZ7.d.ts} +39 -7
- package/dist/index.d.ts +2 -2
- package/dist/index.js +312 -125
- package/dist/index.js.map +1 -1
- package/dist/primitives/index.js +31 -1
- package/dist/primitives/index.js.map +1 -1
- package/dist/workflows/index.d.ts +1 -1
- package/dist/workflows/index.js +311 -124
- package/dist/workflows/index.js.map +1 -1
- package/package.json +2 -1
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();
|
|
@@ -8813,48 +8848,73 @@ import { sleep } from "workflow";
|
|
|
8813
8848
|
|
|
8814
8849
|
// src/lib/elevenlabs-errors.ts
|
|
8815
8850
|
var MAX_ERROR_BODY_CHARS = 500;
|
|
8816
|
-
async function elevenLabsResponseError(context, response) {
|
|
8817
|
-
const error = new Error(`${context}: ${await describeElevenLabsErrorResponse(response)}`);
|
|
8818
|
-
if (!isRetryableElevenLabsStatus(response.status)) {
|
|
8819
|
-
error.name = "FatalError";
|
|
8820
|
-
}
|
|
8821
|
-
return error;
|
|
8822
|
-
}
|
|
8823
8851
|
function isRetryableElevenLabsStatus(status) {
|
|
8824
8852
|
return status === 408 || status === 425 || status === 429 || status >= 500;
|
|
8825
8853
|
}
|
|
8826
|
-
async function
|
|
8827
|
-
const statusLine = [response.status, response.statusText].filter(Boolean).join(" ");
|
|
8854
|
+
async function elevenLabsResponseFailure(response) {
|
|
8828
8855
|
const body = await response.text().catch(() => "");
|
|
8829
|
-
|
|
8830
|
-
return detail ? `${statusLine}: ${detail}` : statusLine;
|
|
8856
|
+
return httpFailure(response.status, response.statusText, body);
|
|
8831
8857
|
}
|
|
8832
|
-
function
|
|
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
|
+
}
|
|
8878
|
+
}
|
|
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) {
|
|
8833
8890
|
const trimmed = body.trim();
|
|
8834
8891
|
if (!trimmed) {
|
|
8835
|
-
return "";
|
|
8892
|
+
return { detail: "" };
|
|
8836
8893
|
}
|
|
8837
8894
|
let detail;
|
|
8838
8895
|
try {
|
|
8839
8896
|
detail = JSON.parse(trimmed).detail;
|
|
8840
8897
|
} catch {
|
|
8841
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
8898
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
8842
8899
|
}
|
|
8843
8900
|
if (typeof detail === "string" && detail) {
|
|
8844
|
-
return detail;
|
|
8901
|
+
return { detail };
|
|
8845
8902
|
}
|
|
8846
8903
|
if (Array.isArray(detail) && detail.length > 0) {
|
|
8847
|
-
return
|
|
8904
|
+
return {
|
|
8905
|
+
detail: detail.map((item) => [item.loc?.join("."), item.msg].filter(Boolean).join(": ")).join("; ")
|
|
8906
|
+
};
|
|
8848
8907
|
}
|
|
8849
8908
|
if (detail && typeof detail === "object") {
|
|
8850
8909
|
const { status, code, message, param, request_id: requestId } = detail;
|
|
8851
|
-
const
|
|
8910
|
+
const reasonCode = [status, code].find((value) => typeof value === "string" && value !== "");
|
|
8911
|
+
const reasonText = [reasonCode, message].filter(Boolean).join(": ");
|
|
8852
8912
|
const context = [param && `param: ${param}`, requestId && `request_id: ${requestId}`].filter(Boolean).join(", ");
|
|
8853
|
-
if (
|
|
8854
|
-
return context ? `${
|
|
8913
|
+
if (reasonText) {
|
|
8914
|
+
return { reason: reasonCode, detail: context ? `${reasonText} (${context})` : reasonText };
|
|
8855
8915
|
}
|
|
8856
8916
|
}
|
|
8857
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
8917
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
8858
8918
|
}
|
|
8859
8919
|
|
|
8860
8920
|
// src/lib/mux-static-renditions.ts
|
|
@@ -8914,6 +8974,9 @@ var STATIC_RENDITION_POLL_INTERVAL_MS = 1e4;
|
|
|
8914
8974
|
var STATIC_RENDITION_MAX_ATTEMPTS = 180;
|
|
8915
8975
|
var MAX_DUBBING_SOURCE_BYTES = 1e9;
|
|
8916
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;
|
|
8917
8980
|
var DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS = 7200;
|
|
8918
8981
|
function getReadyAudioStaticRendition(asset) {
|
|
8919
8982
|
const files = asset.static_renditions?.files;
|
|
@@ -9047,64 +9110,128 @@ async function waitForLegacyMp4SupportRendition({
|
|
|
9047
9110
|
}
|
|
9048
9111
|
return rendition.name;
|
|
9049
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
|
+
}
|
|
9050
9147
|
async function createElevenLabsDubbingJob({
|
|
9051
9148
|
sourceUrl,
|
|
9052
9149
|
assetId,
|
|
9053
9150
|
elevenLabsLangCode,
|
|
9054
9151
|
elevenLabsSourceLangCode,
|
|
9055
|
-
numSpeakers,
|
|
9056
9152
|
credentials
|
|
9057
9153
|
}) {
|
|
9058
9154
|
"use step";
|
|
9059
|
-
const
|
|
9060
|
-
const
|
|
9061
|
-
|
|
9062
|
-
|
|
9063
|
-
|
|
9064
|
-
|
|
9065
|
-
|
|
9066
|
-
|
|
9067
|
-
|
|
9068
|
-
|
|
9069
|
-
|
|
9070
|
-
|
|
9071
|
-
|
|
9072
|
-
|
|
9073
|
-
|
|
9074
|
-
|
|
9075
|
-
}
|
|
9076
|
-
|
|
9077
|
-
})
|
|
9078
|
-
|
|
9079
|
-
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) };
|
|
9080
9175
|
}
|
|
9081
|
-
const dubbingData = await dubbingResponse.json();
|
|
9082
|
-
return dubbingData.dubbing_id;
|
|
9083
9176
|
}
|
|
9084
9177
|
async function checkElevenLabsDubbingStatus({
|
|
9085
9178
|
dubbingId,
|
|
9179
|
+
languageId,
|
|
9086
9180
|
credentials
|
|
9087
9181
|
}) {
|
|
9088
9182
|
"use step";
|
|
9089
|
-
const
|
|
9090
|
-
const
|
|
9091
|
-
|
|
9092
|
-
|
|
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) };
|
|
9093
9228
|
}
|
|
9094
|
-
});
|
|
9095
|
-
if (!statusResponse.ok) {
|
|
9096
|
-
throw await elevenLabsResponseError("Status check failed", statusResponse);
|
|
9097
9229
|
}
|
|
9098
|
-
|
|
9099
|
-
return {
|
|
9100
|
-
status: statusData.status,
|
|
9101
|
-
targetLanguages: statusData.target_languages ?? [],
|
|
9102
|
-
error: typeof statusData.error === "string" && statusData.error ? statusData.error : void 0
|
|
9103
|
-
};
|
|
9230
|
+
return { kind: "failed", failure: { retryable: false, detail: `Failed to fetch dubbed ${output}` } };
|
|
9104
9231
|
}
|
|
9105
9232
|
async function downloadAndUploadDubbedAudio({
|
|
9106
9233
|
dubbingId,
|
|
9107
|
-
|
|
9234
|
+
languageId,
|
|
9108
9235
|
assetId,
|
|
9109
9236
|
toLanguageCode,
|
|
9110
9237
|
s3Endpoint,
|
|
@@ -9115,20 +9242,32 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9115
9242
|
credentials
|
|
9116
9243
|
}) {
|
|
9117
9244
|
"use step";
|
|
9118
|
-
const
|
|
9119
|
-
|
|
9120
|
-
|
|
9121
|
-
|
|
9122
|
-
|
|
9123
|
-
|
|
9124
|
-
|
|
9125
|
-
|
|
9126
|
-
|
|
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
|
+
};
|
|
9127
9266
|
}
|
|
9128
9267
|
const dubbedAudio = new Uint8Array(await audioResponse.arrayBuffer());
|
|
9129
9268
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9130
9269
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
9131
|
-
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}
|
|
9270
|
+
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}.${extension}`;
|
|
9132
9271
|
await putObjectWithStorageAdapter({
|
|
9133
9272
|
accessKeyId: s3AccessKeyId,
|
|
9134
9273
|
secretAccessKey: s3SecretAccessKey,
|
|
@@ -9137,7 +9276,7 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9137
9276
|
bucket: s3Bucket,
|
|
9138
9277
|
key: audioKey,
|
|
9139
9278
|
body: dubbedAudio,
|
|
9140
|
-
contentType:
|
|
9279
|
+
contentType: Object.keys(formats).find((type) => formats[type] === extension)
|
|
9141
9280
|
}, storageAdapter);
|
|
9142
9281
|
const presignedUrl = await createPresignedGetUrlWithStorageAdapter({
|
|
9143
9282
|
accessKeyId: s3AccessKeyId,
|
|
@@ -9151,11 +9290,11 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9151
9290
|
const expiryHours = Math.round((s3SignedUrlExpirySeconds ?? 86400) / 3600);
|
|
9152
9291
|
console.warn(`\u2705 Audio uploaded successfully to: ${audioKey}`);
|
|
9153
9292
|
console.warn(`\u{1F517} Generated presigned URL (expires in ${expiryHours} hour${expiryHours === 1 ? "" : "s"})`);
|
|
9154
|
-
return presignedUrl;
|
|
9293
|
+
return { kind: "staged", presignedUrl };
|
|
9155
9294
|
}
|
|
9156
9295
|
async function downloadAndUploadDubTranscript({
|
|
9157
9296
|
dubbingId,
|
|
9158
|
-
|
|
9297
|
+
languageId,
|
|
9159
9298
|
assetId,
|
|
9160
9299
|
toLanguageCode,
|
|
9161
9300
|
s3Endpoint,
|
|
@@ -9166,20 +9305,13 @@ async function downloadAndUploadDubTranscript({
|
|
|
9166
9305
|
credentials
|
|
9167
9306
|
}) {
|
|
9168
9307
|
"use step";
|
|
9169
|
-
const
|
|
9170
|
-
|
|
9171
|
-
|
|
9172
|
-
headers: {
|
|
9173
|
-
"xi-api-key": elevenLabsApiKey
|
|
9174
|
-
}
|
|
9175
|
-
});
|
|
9176
|
-
if (!transcriptResponse.ok) {
|
|
9177
|
-
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;
|
|
9178
9311
|
}
|
|
9179
|
-
const
|
|
9180
|
-
|
|
9181
|
-
|
|
9182
|
-
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" } };
|
|
9183
9315
|
}
|
|
9184
9316
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9185
9317
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
@@ -9204,7 +9336,7 @@ async function downloadAndUploadDubTranscript({
|
|
|
9204
9336
|
expiresInSeconds: s3SignedUrlExpirySeconds ?? 86400
|
|
9205
9337
|
}, storageAdapter);
|
|
9206
9338
|
console.warn(`\u2705 Dub transcript uploaded successfully to: ${vttKey}`);
|
|
9207
|
-
return presignedUrl;
|
|
9339
|
+
return { kind: "staged", presignedUrl };
|
|
9208
9340
|
}
|
|
9209
9341
|
async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
9210
9342
|
"use workflow";
|
|
@@ -9226,12 +9358,16 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9226
9358
|
if (provider !== "elevenlabs") {
|
|
9227
9359
|
throw new MuxAiError("Only ElevenLabs provider is currently supported for audio translation.", { type: "validation_error" });
|
|
9228
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
|
+
}
|
|
9229
9365
|
const credentials = providedCredentials;
|
|
9230
9366
|
const effectiveStorageAdapter = storageAdapter;
|
|
9231
9367
|
const trackPassthrough = validateTrackPassthrough(providedTrackPassthrough) ?? buildMuxAiTrackPassthrough("translate-audio");
|
|
9232
9368
|
const uploadToMux = uploadToMuxOption !== false;
|
|
9233
9369
|
const uploadToS3 = uploadToS3Option || uploadToMux || uploadCaptionsToMux;
|
|
9234
|
-
const muxLangCode =
|
|
9370
|
+
const muxLangCode = targetLanguageCode;
|
|
9235
9371
|
const trackName = providedTrackName ?? `${getLanguageName(muxLangCode) ?? muxLangCode.toUpperCase()} (Auto-dubbed)`;
|
|
9236
9372
|
const audioTrackTarget = { type: "audio", languageCode: muxLangCode, name: trackName };
|
|
9237
9373
|
const captionsTrackTarget = { type: "text", languageCode: muxLangCode, name: trackName };
|
|
@@ -9245,6 +9381,13 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9245
9381
|
}
|
|
9246
9382
|
const { asset: initialAsset, playbackId, policy } = await getPlaybackIdForAsset(assetId, credentials, options.assetSnapshot);
|
|
9247
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
|
+
}
|
|
9248
9391
|
for (const target of [
|
|
9249
9392
|
...uploadToMux ? [audioTrackTarget] : [],
|
|
9250
9393
|
...uploadCaptionsToMux ? [captionsTrackTarget] : []
|
|
@@ -9268,6 +9411,7 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9268
9411
|
}
|
|
9269
9412
|
let staticRenditionCleanup = "not_created";
|
|
9270
9413
|
let dubbingId;
|
|
9414
|
+
let languageId;
|
|
9271
9415
|
let presignedUrl;
|
|
9272
9416
|
let uploadedTrackId;
|
|
9273
9417
|
let replacedTracks;
|
|
@@ -9302,75 +9446,114 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9302
9446
|
audioUrl = await signUrl(audioUrl, playbackId, "video", void 0, credentials);
|
|
9303
9447
|
}
|
|
9304
9448
|
console.warn("\u{1F399}\uFE0F Creating dubbing job in ElevenLabs...");
|
|
9305
|
-
const elevenLabsLangCode =
|
|
9306
|
-
const
|
|
9307
|
-
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;
|
|
9308
9452
|
console.warn(
|
|
9309
9453
|
`\u{1F50D} Creating dubbing job for asset ${assetId}: ${elevenLabsSourceLangCode ?? "auto"} -> ${elevenLabsLangCode}`
|
|
9310
9454
|
);
|
|
9455
|
+
let created;
|
|
9311
9456
|
try {
|
|
9312
|
-
|
|
9457
|
+
created = await createElevenLabsDubbingJob({
|
|
9313
9458
|
sourceUrl: audioUrl,
|
|
9314
9459
|
assetId,
|
|
9315
9460
|
elevenLabsLangCode,
|
|
9316
9461
|
elevenLabsSourceLangCode,
|
|
9317
|
-
numSpeakers,
|
|
9318
9462
|
credentials
|
|
9319
9463
|
});
|
|
9320
|
-
console.warn(`\u2705 Dubbing job created with ID: ${dubbingId}`);
|
|
9321
9464
|
} catch (error) {
|
|
9322
9465
|
wrapError(error, "Failed to create ElevenLabs dubbing job");
|
|
9323
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}`);
|
|
9324
9478
|
console.warn("\u23F3 Waiting for dubbing to complete...");
|
|
9325
9479
|
const dubbingPollTimeoutSeconds = options.dubbingPollTimeoutSeconds ?? DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS;
|
|
9326
9480
|
const maxPollAttempts = Math.max(1, Math.ceil(dubbingPollTimeoutSeconds * 1e3 / DUBBING_POLL_INTERVAL_MS));
|
|
9327
9481
|
let dubbingStatus = "dubbing";
|
|
9328
9482
|
let pollAttempts = 0;
|
|
9329
|
-
let targetLanguages = [];
|
|
9330
9483
|
let dubbingError;
|
|
9331
|
-
|
|
9484
|
+
let consecutiveStatusCheckFailures = 0;
|
|
9485
|
+
while (dubbingStatus !== "completed" && pollAttempts < maxPollAttempts) {
|
|
9332
9486
|
await sleep(DUBBING_POLL_INTERVAL_MS);
|
|
9333
9487
|
pollAttempts++;
|
|
9488
|
+
let statusResult;
|
|
9334
9489
|
try {
|
|
9335
|
-
|
|
9490
|
+
statusResult = await checkElevenLabsDubbingStatus({
|
|
9336
9491
|
dubbingId,
|
|
9492
|
+
languageId,
|
|
9337
9493
|
credentials
|
|
9338
9494
|
});
|
|
9339
|
-
dubbingStatus = statusResult.status;
|
|
9340
|
-
targetLanguages = statusResult.targetLanguages;
|
|
9341
|
-
dubbingError = statusResult.error;
|
|
9342
9495
|
} catch (error) {
|
|
9343
9496
|
wrapError(error, "Failed to check dubbing status");
|
|
9344
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;
|
|
9345
9510
|
if (dubbingStatus === "failed") {
|
|
9346
|
-
console.warn(`\u274C ElevenLabs dubbing
|
|
9347
|
-
|
|
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
|
+
);
|
|
9348
9519
|
}
|
|
9349
9520
|
}
|
|
9350
|
-
if (dubbingStatus !== "
|
|
9521
|
+
if (dubbingStatus !== "completed") {
|
|
9351
9522
|
throw new MuxAiError("Audio translation timed out or failed. Please try again.", { type: "timeout_error", retryable: true });
|
|
9352
9523
|
}
|
|
9353
9524
|
console.warn("\u2705 Dubbing completed successfully!");
|
|
9354
9525
|
if (uploadToS3) {
|
|
9355
9526
|
console.warn("\u{1F4E5} Downloading dubbed audio from ElevenLabs and staging to S3...");
|
|
9356
|
-
|
|
9357
|
-
|
|
9358
|
-
|
|
9359
|
-
|
|
9360
|
-
|
|
9361
|
-
|
|
9362
|
-
|
|
9363
|
-
|
|
9364
|
-
|
|
9365
|
-
|
|
9366
|
-
|
|
9367
|
-
|
|
9368
|
-
|
|
9369
|
-
|
|
9370
|
-
|
|
9371
|
-
|
|
9372
|
-
|
|
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);
|
|
9373
9555
|
}
|
|
9556
|
+
console.warn("\u2705 Dubbed audio staged to S3 successfully!");
|
|
9374
9557
|
if (uploadToMux) {
|
|
9375
9558
|
console.warn("\u{1F4F9} Adding dubbed audio track to Mux asset...");
|
|
9376
9559
|
let outcome;
|
|
@@ -9399,9 +9582,9 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9399
9582
|
}
|
|
9400
9583
|
try {
|
|
9401
9584
|
console.warn("\u{1F4E5} Downloading dub transcript from ElevenLabs and staging to S3...");
|
|
9402
|
-
|
|
9585
|
+
const transcript = await downloadAndUploadDubTranscript({
|
|
9403
9586
|
dubbingId,
|
|
9404
|
-
|
|
9587
|
+
languageId,
|
|
9405
9588
|
assetId,
|
|
9406
9589
|
toLanguageCode,
|
|
9407
9590
|
s3Endpoint,
|
|
@@ -9411,6 +9594,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9411
9594
|
s3SignedUrlExpirySeconds: options.s3SignedUrlExpirySeconds,
|
|
9412
9595
|
credentials
|
|
9413
9596
|
});
|
|
9597
|
+
if (transcript.kind === "failed") {
|
|
9598
|
+
throw new Error(transcript.failure.detail);
|
|
9599
|
+
}
|
|
9600
|
+
captionsPresignedUrl = transcript.presignedUrl;
|
|
9414
9601
|
if (uploadCaptionsToMux) {
|
|
9415
9602
|
console.warn("\u{1F4F9} Adding dubbed captions track to Mux asset...");
|
|
9416
9603
|
const captionsOutcome = await replaceAndCreateTrack({
|
|
@@ -9450,10 +9637,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9450
9637
|
}
|
|
9451
9638
|
}
|
|
9452
9639
|
}
|
|
9453
|
-
const targetLanguage = getLanguageCodePair(
|
|
9640
|
+
const targetLanguage = getLanguageCodePair(targetLanguageCode);
|
|
9454
9641
|
return {
|
|
9455
9642
|
assetId,
|
|
9456
|
-
targetLanguageCode
|
|
9643
|
+
targetLanguageCode,
|
|
9457
9644
|
targetLanguage,
|
|
9458
9645
|
dubbingId,
|
|
9459
9646
|
uploadedTrackId,
|