@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/index.js
CHANGED
|
@@ -5,7 +5,7 @@ var __export = (target, all) => {
|
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
// package.json
|
|
8
|
-
var version = "0.
|
|
8
|
+
var version = "0.57.0";
|
|
9
9
|
|
|
10
10
|
// src/lib/language-codes.ts
|
|
11
11
|
var ISO639_1_TO_3 = {
|
|
@@ -117,8 +117,38 @@ var ISO639_1_TO_3 = {
|
|
|
117
117
|
// Estonian
|
|
118
118
|
lv: "lav",
|
|
119
119
|
// Latvian
|
|
120
|
-
lt: "lit"
|
|
120
|
+
lt: "lit",
|
|
121
121
|
// Lithuanian
|
|
122
|
+
as: "asm",
|
|
123
|
+
az: "aze",
|
|
124
|
+
be: "bel",
|
|
125
|
+
bs: "bos",
|
|
126
|
+
cy: "cym",
|
|
127
|
+
ga: "gle",
|
|
128
|
+
ha: "hau",
|
|
129
|
+
hy: "hye",
|
|
130
|
+
jv: "jav",
|
|
131
|
+
ka: "kat",
|
|
132
|
+
kk: "kaz",
|
|
133
|
+
ky: "kir",
|
|
134
|
+
lb: "ltz",
|
|
135
|
+
ln: "lin",
|
|
136
|
+
mi: "mri",
|
|
137
|
+
mk: "mkd",
|
|
138
|
+
mn: "mon",
|
|
139
|
+
mt: "mlt",
|
|
140
|
+
my: "mya",
|
|
141
|
+
ne: "nep",
|
|
142
|
+
ny: "nya",
|
|
143
|
+
oc: "oci",
|
|
144
|
+
or: "ori",
|
|
145
|
+
ps: "pus",
|
|
146
|
+
sd: "snd",
|
|
147
|
+
so: "som",
|
|
148
|
+
sr: "srp",
|
|
149
|
+
tg: "tgk",
|
|
150
|
+
uz: "uzb",
|
|
151
|
+
yo: "yor"
|
|
122
152
|
};
|
|
123
153
|
var ISO639_3_TO_1 = Object.fromEntries(
|
|
124
154
|
Object.entries(ISO639_1_TO_3).map(([iso1, iso3]) => [iso3, iso1])
|
|
@@ -135,7 +165,7 @@ function toISO639_1(code) {
|
|
|
135
165
|
if (normalized.length === 2) {
|
|
136
166
|
return normalized;
|
|
137
167
|
}
|
|
138
|
-
return ISO639_3_TO_1[normalized]
|
|
168
|
+
return Object.prototype.hasOwnProperty.call(ISO639_3_TO_1, normalized) ? ISO639_3_TO_1[normalized] : normalized;
|
|
139
169
|
}
|
|
140
170
|
function getLanguageCodePair(code) {
|
|
141
171
|
const normalized = code.toLowerCase().trim();
|
|
@@ -2904,6 +2934,11 @@ function isAudioOnlyAsset(asset) {
|
|
|
2904
2934
|
const hasVideoTrack = asset.tracks?.some((track) => track.type === "video") ?? false;
|
|
2905
2935
|
return hasAudioTrack && !hasVideoTrack;
|
|
2906
2936
|
}
|
|
2937
|
+
function isVideoOnlyAsset(asset) {
|
|
2938
|
+
const hasAudioTrack = asset.tracks?.some((track) => track.type === "audio") ?? false;
|
|
2939
|
+
const hasVideoTrack = asset.tracks?.some((track) => track.type === "video") ?? false;
|
|
2940
|
+
return hasVideoTrack && !hasAudioTrack;
|
|
2941
|
+
}
|
|
2907
2942
|
function toPlaybackAsset(asset) {
|
|
2908
2943
|
const { id: playbackId, policy } = getPlaybackId(asset);
|
|
2909
2944
|
return { asset, playbackId, policy };
|
|
@@ -9209,48 +9244,73 @@ import { sleep as sleep2 } from "workflow";
|
|
|
9209
9244
|
|
|
9210
9245
|
// src/lib/elevenlabs-errors.ts
|
|
9211
9246
|
var MAX_ERROR_BODY_CHARS = 500;
|
|
9212
|
-
async function elevenLabsResponseError(context, response) {
|
|
9213
|
-
const error = new Error(`${context}: ${await describeElevenLabsErrorResponse(response)}`);
|
|
9214
|
-
if (!isRetryableElevenLabsStatus(response.status)) {
|
|
9215
|
-
error.name = "FatalError";
|
|
9216
|
-
}
|
|
9217
|
-
return error;
|
|
9218
|
-
}
|
|
9219
9247
|
function isRetryableElevenLabsStatus(status) {
|
|
9220
9248
|
return status === 408 || status === 425 || status === 429 || status >= 500;
|
|
9221
9249
|
}
|
|
9222
|
-
async function
|
|
9223
|
-
const statusLine = [response.status, response.statusText].filter(Boolean).join(" ");
|
|
9250
|
+
async function elevenLabsResponseFailure(response) {
|
|
9224
9251
|
const body = await response.text().catch(() => "");
|
|
9225
|
-
|
|
9226
|
-
return detail ? `${statusLine}: ${detail}` : statusLine;
|
|
9252
|
+
return httpFailure(response.status, response.statusText, body);
|
|
9227
9253
|
}
|
|
9228
|
-
function
|
|
9254
|
+
function elevenLabsSdkFailure(error) {
|
|
9255
|
+
const failure = error;
|
|
9256
|
+
if (typeof failure?.statusCode === "number") {
|
|
9257
|
+
const body = typeof failure.body === "string" ? failure.body : JSON.stringify(failure.body ?? "");
|
|
9258
|
+
return httpFailure(failure.statusCode, failure.rawResponse?.statusText, body);
|
|
9259
|
+
}
|
|
9260
|
+
const message = failure?.message ?? String(error);
|
|
9261
|
+
switch (failure?.name) {
|
|
9262
|
+
case "ElevenLabsTimeoutError":
|
|
9263
|
+
return { reason: "timeout", retryable: true, detail: message };
|
|
9264
|
+
// The SDK validates responses strictly: the API's response no longer matches
|
|
9265
|
+
// this SDK version, so the same call fails the same way.
|
|
9266
|
+
case "ParseError":
|
|
9267
|
+
return { reason: "unexpected_response", retryable: false, detail: `Unexpected ElevenLabs response: ${message}` };
|
|
9268
|
+
// The SDK's own error without a status: the request got no response.
|
|
9269
|
+
case "ElevenLabsError":
|
|
9270
|
+
return { reason: "network_error", retryable: true, detail: message };
|
|
9271
|
+
default:
|
|
9272
|
+
return { retryable: false, detail: message };
|
|
9273
|
+
}
|
|
9274
|
+
}
|
|
9275
|
+
function httpFailure(status, statusText, body) {
|
|
9276
|
+
const statusLine = [status, statusText].filter(Boolean).join(" ");
|
|
9277
|
+
const { reason, detail } = parseElevenLabsErrorBody(body);
|
|
9278
|
+
return {
|
|
9279
|
+
httpStatus: status,
|
|
9280
|
+
reason,
|
|
9281
|
+
retryable: isRetryableElevenLabsStatus(status),
|
|
9282
|
+
detail: detail ? `${statusLine}: ${detail}` : statusLine
|
|
9283
|
+
};
|
|
9284
|
+
}
|
|
9285
|
+
function parseElevenLabsErrorBody(body) {
|
|
9229
9286
|
const trimmed = body.trim();
|
|
9230
9287
|
if (!trimmed) {
|
|
9231
|
-
return "";
|
|
9288
|
+
return { detail: "" };
|
|
9232
9289
|
}
|
|
9233
9290
|
let detail;
|
|
9234
9291
|
try {
|
|
9235
9292
|
detail = JSON.parse(trimmed).detail;
|
|
9236
9293
|
} catch {
|
|
9237
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
9294
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
9238
9295
|
}
|
|
9239
9296
|
if (typeof detail === "string" && detail) {
|
|
9240
|
-
return detail;
|
|
9297
|
+
return { detail };
|
|
9241
9298
|
}
|
|
9242
9299
|
if (Array.isArray(detail) && detail.length > 0) {
|
|
9243
|
-
return
|
|
9300
|
+
return {
|
|
9301
|
+
detail: detail.map((item) => [item.loc?.join("."), item.msg].filter(Boolean).join(": ")).join("; ")
|
|
9302
|
+
};
|
|
9244
9303
|
}
|
|
9245
9304
|
if (detail && typeof detail === "object") {
|
|
9246
9305
|
const { status, code, message, param, request_id: requestId } = detail;
|
|
9247
|
-
const
|
|
9306
|
+
const reasonCode = [status, code].find((value) => typeof value === "string" && value !== "");
|
|
9307
|
+
const reasonText = [reasonCode, message].filter(Boolean).join(": ");
|
|
9248
9308
|
const context = [param && `param: ${param}`, requestId && `request_id: ${requestId}`].filter(Boolean).join(", ");
|
|
9249
|
-
if (
|
|
9250
|
-
return context ? `${
|
|
9309
|
+
if (reasonText) {
|
|
9310
|
+
return { reason: reasonCode, detail: context ? `${reasonText} (${context})` : reasonText };
|
|
9251
9311
|
}
|
|
9252
9312
|
}
|
|
9253
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
9313
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
9254
9314
|
}
|
|
9255
9315
|
|
|
9256
9316
|
// src/workflows/translate-audio.ts
|
|
@@ -9258,6 +9318,9 @@ var STATIC_RENDITION_POLL_INTERVAL_MS = 1e4;
|
|
|
9258
9318
|
var STATIC_RENDITION_MAX_ATTEMPTS = 180;
|
|
9259
9319
|
var MAX_DUBBING_SOURCE_BYTES = 1e9;
|
|
9260
9320
|
var DUBBING_POLL_INTERVAL_MS = 1e4;
|
|
9321
|
+
var MAX_CONSECUTIVE_STATUS_CHECK_FAILURES = 6;
|
|
9322
|
+
var DUBBED_AUDIO_DOWNLOAD_ATTEMPTS = 3;
|
|
9323
|
+
var DUBBED_AUDIO_DOWNLOAD_RETRY_DELAY_MS = 1e4;
|
|
9261
9324
|
var DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS = 7200;
|
|
9262
9325
|
function getReadyAudioStaticRendition(asset) {
|
|
9263
9326
|
const files = asset.static_renditions?.files;
|
|
@@ -9391,64 +9454,128 @@ async function waitForLegacyMp4SupportRendition({
|
|
|
9391
9454
|
}
|
|
9392
9455
|
return rendition.name;
|
|
9393
9456
|
}
|
|
9457
|
+
function toBaseLanguageCode(code) {
|
|
9458
|
+
return code.trim().split(/[-_]/)[0];
|
|
9459
|
+
}
|
|
9460
|
+
function audioTrackUnsupportedReason(asset) {
|
|
9461
|
+
const useDubbedFile = "skip uploading to Mux and use the dubbed audio file directly.";
|
|
9462
|
+
if (asset.ingest_type === "on_demand_clip" || asset.source_asset_id) {
|
|
9463
|
+
return `is a clip, and Mux does not support adding audio tracks to clipped assets. Dub the source asset instead, or ${useDubbedFile}`;
|
|
9464
|
+
}
|
|
9465
|
+
if (asset.is_live || asset.live_stream_id) {
|
|
9466
|
+
return `comes from a live stream, and Mux does not support adding audio tracks to live stream assets. Instead, ${useDubbedFile}`;
|
|
9467
|
+
}
|
|
9468
|
+
if (isAudioOnlyAsset(asset)) {
|
|
9469
|
+
return `is audio-only, and Mux does not support adding audio tracks to audio-only assets. Instead, ${useDubbedFile}`;
|
|
9470
|
+
}
|
|
9471
|
+
return void 0;
|
|
9472
|
+
}
|
|
9473
|
+
function dubbingProviderError(message, failure) {
|
|
9474
|
+
if (failure.reason === "timeout") {
|
|
9475
|
+
return new MuxAiError(`${message} The dubbing service timed out. Please try again.`, { type: "timeout_error", retryable: true });
|
|
9476
|
+
}
|
|
9477
|
+
if (failure.retryable) {
|
|
9478
|
+
return new MuxAiError(`${message} The dubbing service is temporarily unavailable. Please try again.`, { type: "processing_error", retryable: true });
|
|
9479
|
+
}
|
|
9480
|
+
return new MuxAiError(message, { type: "processing_error" });
|
|
9481
|
+
}
|
|
9482
|
+
function dubbingStartError(failure, languages) {
|
|
9483
|
+
if (failure.reason === "unsupported_target_language") {
|
|
9484
|
+
return new MuxAiError(`Audio dubbing does not support the target language "${languages.target}".`, { type: "validation_error" });
|
|
9485
|
+
}
|
|
9486
|
+
if (failure.reason === "unsupported_source_language" && languages.source) {
|
|
9487
|
+
return new MuxAiError(`Audio dubbing does not support the source language "${languages.source}".`, { type: "validation_error" });
|
|
9488
|
+
}
|
|
9489
|
+
return dubbingProviderError("Audio dubbing could not be started.", failure);
|
|
9490
|
+
}
|
|
9394
9491
|
async function createElevenLabsDubbingJob({
|
|
9395
9492
|
sourceUrl,
|
|
9396
9493
|
assetId,
|
|
9397
9494
|
elevenLabsLangCode,
|
|
9398
9495
|
elevenLabsSourceLangCode,
|
|
9399
|
-
numSpeakers,
|
|
9400
9496
|
credentials
|
|
9401
9497
|
}) {
|
|
9402
9498
|
"use step";
|
|
9403
|
-
const
|
|
9404
|
-
const
|
|
9405
|
-
|
|
9406
|
-
|
|
9407
|
-
|
|
9408
|
-
|
|
9409
|
-
|
|
9410
|
-
|
|
9411
|
-
|
|
9412
|
-
|
|
9413
|
-
|
|
9414
|
-
|
|
9415
|
-
|
|
9416
|
-
|
|
9417
|
-
|
|
9418
|
-
|
|
9419
|
-
}
|
|
9420
|
-
|
|
9421
|
-
})
|
|
9422
|
-
|
|
9423
|
-
throw await elevenLabsResponseError("ElevenLabs API error", dubbingResponse);
|
|
9499
|
+
const { ElevenLabsClient } = await import("@elevenlabs/elevenlabs-js");
|
|
9500
|
+
const client = new ElevenLabsClient({ apiKey: await getApiKeyFromEnv("elevenlabs", credentials), maxRetries: 0 });
|
|
9501
|
+
try {
|
|
9502
|
+
const project = await client.dubbing.project.create({
|
|
9503
|
+
sourceUrl,
|
|
9504
|
+
sourceLanguage: elevenLabsSourceLangCode,
|
|
9505
|
+
targetLanguage: elevenLabsLangCode,
|
|
9506
|
+
modelId: "dubbing_v1",
|
|
9507
|
+
reference: `Mux Asset ${assetId} - ${elevenLabsSourceLangCode ?? "auto"} to ${elevenLabsLangCode}`
|
|
9508
|
+
});
|
|
9509
|
+
const languageId = project.languageIds?.[0];
|
|
9510
|
+
if (!languageId) {
|
|
9511
|
+
return {
|
|
9512
|
+
kind: "failed",
|
|
9513
|
+
failure: { retryable: false, detail: `ElevenLabs project ${project.projectId} returned no language target. Check the project before retrying.` }
|
|
9514
|
+
};
|
|
9515
|
+
}
|
|
9516
|
+
return { kind: "created", projectId: project.projectId, languageId };
|
|
9517
|
+
} catch (error) {
|
|
9518
|
+
return { kind: "failed", failure: elevenLabsSdkFailure(error) };
|
|
9424
9519
|
}
|
|
9425
|
-
const dubbingData = await dubbingResponse.json();
|
|
9426
|
-
return dubbingData.dubbing_id;
|
|
9427
9520
|
}
|
|
9428
9521
|
async function checkElevenLabsDubbingStatus({
|
|
9429
9522
|
dubbingId,
|
|
9523
|
+
languageId,
|
|
9430
9524
|
credentials
|
|
9431
9525
|
}) {
|
|
9432
9526
|
"use step";
|
|
9433
|
-
const
|
|
9434
|
-
const
|
|
9435
|
-
|
|
9436
|
-
|
|
9527
|
+
const { ElevenLabsClient } = await import("@elevenlabs/elevenlabs-js");
|
|
9528
|
+
const client = new ElevenLabsClient({ apiKey: await getApiKeyFromEnv("elevenlabs", credentials), maxRetries: 0 });
|
|
9529
|
+
try {
|
|
9530
|
+
const [project, language] = await Promise.all([
|
|
9531
|
+
client.dubbing.project.get(dubbingId),
|
|
9532
|
+
client.dubbing.project.language.get(dubbingId, languageId)
|
|
9533
|
+
]);
|
|
9534
|
+
return {
|
|
9535
|
+
kind: "status",
|
|
9536
|
+
status: project.status === "failed" ? "failed" : language.status,
|
|
9537
|
+
// A language error of `project_failed` only points at the project, so prefer the project's.
|
|
9538
|
+
error: project.error ?? language.error
|
|
9539
|
+
};
|
|
9540
|
+
} catch (error) {
|
|
9541
|
+
return { kind: "failed", failure: elevenLabsSdkFailure(error) };
|
|
9542
|
+
}
|
|
9543
|
+
}
|
|
9544
|
+
async function fetchDubbingOutput(dubbingId, languageId, output, credentials) {
|
|
9545
|
+
const { ElevenLabsClient } = await import("@elevenlabs/elevenlabs-js");
|
|
9546
|
+
const client = new ElevenLabsClient({ apiKey: await getApiKeyFromEnv("elevenlabs", credentials), maxRetries: 0 });
|
|
9547
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
9548
|
+
let url;
|
|
9549
|
+
try {
|
|
9550
|
+
const target = await client.dubbing.project.language.get(dubbingId, languageId);
|
|
9551
|
+
url = target.outputs?.[output];
|
|
9552
|
+
} catch (error) {
|
|
9553
|
+
return { kind: "failed", failure: elevenLabsSdkFailure(error) };
|
|
9554
|
+
}
|
|
9555
|
+
if (!url) {
|
|
9556
|
+
return {
|
|
9557
|
+
kind: "failed",
|
|
9558
|
+
failure: { reason: "output_missing", retryable: true, detail: `ElevenLabs language ${languageId} of project ${dubbingId} has no ${output} output` }
|
|
9559
|
+
};
|
|
9560
|
+
}
|
|
9561
|
+
let response;
|
|
9562
|
+
try {
|
|
9563
|
+
response = await fetch(url);
|
|
9564
|
+
} catch (error) {
|
|
9565
|
+
return { kind: "failed", failure: { reason: "network_error", retryable: true, detail: `Failed to fetch dubbed ${output}: ${error instanceof Error ? error.message : String(error)}` } };
|
|
9566
|
+
}
|
|
9567
|
+
if (response.ok) {
|
|
9568
|
+
return { kind: "fetched", response, url };
|
|
9569
|
+
}
|
|
9570
|
+
if (response.status !== 403 || attempt === 1) {
|
|
9571
|
+
return { kind: "failed", failure: await elevenLabsResponseFailure(response) };
|
|
9437
9572
|
}
|
|
9438
|
-
});
|
|
9439
|
-
if (!statusResponse.ok) {
|
|
9440
|
-
throw await elevenLabsResponseError("Status check failed", statusResponse);
|
|
9441
9573
|
}
|
|
9442
|
-
|
|
9443
|
-
return {
|
|
9444
|
-
status: statusData.status,
|
|
9445
|
-
targetLanguages: statusData.target_languages ?? [],
|
|
9446
|
-
error: typeof statusData.error === "string" && statusData.error ? statusData.error : void 0
|
|
9447
|
-
};
|
|
9574
|
+
return { kind: "failed", failure: { retryable: false, detail: `Failed to fetch dubbed ${output}` } };
|
|
9448
9575
|
}
|
|
9449
9576
|
async function downloadAndUploadDubbedAudio({
|
|
9450
9577
|
dubbingId,
|
|
9451
|
-
|
|
9578
|
+
languageId,
|
|
9452
9579
|
assetId,
|
|
9453
9580
|
toLanguageCode,
|
|
9454
9581
|
s3Endpoint,
|
|
@@ -9459,20 +9586,32 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9459
9586
|
credentials
|
|
9460
9587
|
}) {
|
|
9461
9588
|
"use step";
|
|
9462
|
-
const
|
|
9463
|
-
|
|
9464
|
-
|
|
9465
|
-
|
|
9466
|
-
|
|
9467
|
-
|
|
9468
|
-
|
|
9469
|
-
|
|
9470
|
-
|
|
9589
|
+
const fetched = await fetchDubbingOutput(dubbingId, languageId, "losslessAudio", credentials);
|
|
9590
|
+
if (fetched.kind === "failed") {
|
|
9591
|
+
return fetched;
|
|
9592
|
+
}
|
|
9593
|
+
const { response: audioResponse, url } = fetched;
|
|
9594
|
+
const mime = audioResponse.headers.get("content-type")?.split(";")[0].trim();
|
|
9595
|
+
const formats = {
|
|
9596
|
+
"audio/flac": "flac",
|
|
9597
|
+
"audio/x-flac": "flac",
|
|
9598
|
+
"audio/wav": "wav",
|
|
9599
|
+
"audio/x-wav": "wav",
|
|
9600
|
+
"audio/mp4": "m4a",
|
|
9601
|
+
"audio/mpeg": "mp3"
|
|
9602
|
+
};
|
|
9603
|
+
const pathExtension = new URL(url).pathname.split(".").pop()?.toLowerCase();
|
|
9604
|
+
const extension = (mime ? formats[mime] : void 0) ?? Object.values(formats).find((format) => format === pathExtension);
|
|
9605
|
+
if (!extension) {
|
|
9606
|
+
return {
|
|
9607
|
+
kind: "failed",
|
|
9608
|
+
failure: { reason: "unsupported_output_format", retryable: false, detail: `Unsupported dubbed audio content type: ${mime ?? "missing"}` }
|
|
9609
|
+
};
|
|
9471
9610
|
}
|
|
9472
9611
|
const dubbedAudio = new Uint8Array(await audioResponse.arrayBuffer());
|
|
9473
9612
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9474
9613
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
9475
|
-
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}
|
|
9614
|
+
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}.${extension}`;
|
|
9476
9615
|
await putObjectWithStorageAdapter({
|
|
9477
9616
|
accessKeyId: s3AccessKeyId,
|
|
9478
9617
|
secretAccessKey: s3SecretAccessKey,
|
|
@@ -9481,7 +9620,7 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9481
9620
|
bucket: s3Bucket,
|
|
9482
9621
|
key: audioKey,
|
|
9483
9622
|
body: dubbedAudio,
|
|
9484
|
-
contentType:
|
|
9623
|
+
contentType: Object.keys(formats).find((type) => formats[type] === extension)
|
|
9485
9624
|
}, storageAdapter);
|
|
9486
9625
|
const presignedUrl = await createPresignedGetUrlWithStorageAdapter({
|
|
9487
9626
|
accessKeyId: s3AccessKeyId,
|
|
@@ -9495,11 +9634,11 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9495
9634
|
const expiryHours = Math.round((s3SignedUrlExpirySeconds ?? 86400) / 3600);
|
|
9496
9635
|
console.warn(`\u2705 Audio uploaded successfully to: ${audioKey}`);
|
|
9497
9636
|
console.warn(`\u{1F517} Generated presigned URL (expires in ${expiryHours} hour${expiryHours === 1 ? "" : "s"})`);
|
|
9498
|
-
return presignedUrl;
|
|
9637
|
+
return { kind: "staged", presignedUrl };
|
|
9499
9638
|
}
|
|
9500
9639
|
async function downloadAndUploadDubTranscript({
|
|
9501
9640
|
dubbingId,
|
|
9502
|
-
|
|
9641
|
+
languageId,
|
|
9503
9642
|
assetId,
|
|
9504
9643
|
toLanguageCode,
|
|
9505
9644
|
s3Endpoint,
|
|
@@ -9510,20 +9649,13 @@ async function downloadAndUploadDubTranscript({
|
|
|
9510
9649
|
credentials
|
|
9511
9650
|
}) {
|
|
9512
9651
|
"use step";
|
|
9513
|
-
const
|
|
9514
|
-
|
|
9515
|
-
|
|
9516
|
-
headers: {
|
|
9517
|
-
"xi-api-key": elevenLabsApiKey
|
|
9518
|
-
}
|
|
9519
|
-
});
|
|
9520
|
-
if (!transcriptResponse.ok) {
|
|
9521
|
-
throw await elevenLabsResponseError("Failed to fetch dub transcript", transcriptResponse);
|
|
9652
|
+
const fetched = await fetchDubbingOutput(dubbingId, languageId, "subtitles", credentials);
|
|
9653
|
+
if (fetched.kind === "failed") {
|
|
9654
|
+
return fetched;
|
|
9522
9655
|
}
|
|
9523
|
-
const
|
|
9524
|
-
|
|
9525
|
-
|
|
9526
|
-
throw new Error("Dub transcript response did not include webvtt content");
|
|
9656
|
+
const transcriptVtt = await fetched.response.text();
|
|
9657
|
+
if (!transcriptVtt.trim()) {
|
|
9658
|
+
return { kind: "failed", failure: { retryable: false, detail: "Dub transcript response did not include webvtt content" } };
|
|
9527
9659
|
}
|
|
9528
9660
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9529
9661
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
@@ -9548,7 +9680,7 @@ async function downloadAndUploadDubTranscript({
|
|
|
9548
9680
|
expiresInSeconds: s3SignedUrlExpirySeconds ?? 86400
|
|
9549
9681
|
}, storageAdapter);
|
|
9550
9682
|
console.warn(`\u2705 Dub transcript uploaded successfully to: ${vttKey}`);
|
|
9551
|
-
return presignedUrl;
|
|
9683
|
+
return { kind: "staged", presignedUrl };
|
|
9552
9684
|
}
|
|
9553
9685
|
async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
9554
9686
|
"use workflow";
|
|
@@ -9570,12 +9702,16 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9570
9702
|
if (provider !== "elevenlabs") {
|
|
9571
9703
|
throw new MuxAiError("Only ElevenLabs provider is currently supported for audio translation.", { type: "validation_error" });
|
|
9572
9704
|
}
|
|
9705
|
+
const targetLanguageCode = toISO639_1(toLanguageCode);
|
|
9706
|
+
if (numSpeakers !== 0) {
|
|
9707
|
+
throw new MuxAiError("The ElevenLabs project API does not support numSpeakers overrides. Omit it or use 0 for auto-detection.", { type: "validation_error" });
|
|
9708
|
+
}
|
|
9573
9709
|
const credentials = providedCredentials;
|
|
9574
9710
|
const effectiveStorageAdapter = storageAdapter;
|
|
9575
9711
|
const trackPassthrough = validateTrackPassthrough(providedTrackPassthrough) ?? buildMuxAiTrackPassthrough("translate-audio");
|
|
9576
9712
|
const uploadToMux = uploadToMuxOption !== false;
|
|
9577
9713
|
const uploadToS3 = uploadToS3Option || uploadToMux || uploadCaptionsToMux;
|
|
9578
|
-
const muxLangCode =
|
|
9714
|
+
const muxLangCode = targetLanguageCode;
|
|
9579
9715
|
const trackName = providedTrackName ?? `${getLanguageName(muxLangCode) ?? muxLangCode.toUpperCase()} (Auto-dubbed)`;
|
|
9580
9716
|
const audioTrackTarget = { type: "audio", languageCode: muxLangCode, name: trackName };
|
|
9581
9717
|
const captionsTrackTarget = { type: "text", languageCode: muxLangCode, name: trackName };
|
|
@@ -9589,6 +9725,13 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9589
9725
|
}
|
|
9590
9726
|
const { asset: initialAsset, playbackId, policy } = await getPlaybackIdForAsset(assetId, credentials, options.assetSnapshot);
|
|
9591
9727
|
const assetDurationSeconds = getAssetDurationSecondsFromAsset(initialAsset);
|
|
9728
|
+
if (isVideoOnlyAsset(initialAsset)) {
|
|
9729
|
+
throw new MuxAiError(`Asset ${assetId} has no audio track, so there is no audio to dub.`, { type: "validation_error" });
|
|
9730
|
+
}
|
|
9731
|
+
const audioTrackUnsupported = uploadToMux ? audioTrackUnsupportedReason(initialAsset) : void 0;
|
|
9732
|
+
if (audioTrackUnsupported) {
|
|
9733
|
+
throw new MuxAiError(`Asset ${assetId} ${audioTrackUnsupported}`, { type: "validation_error" });
|
|
9734
|
+
}
|
|
9592
9735
|
for (const target of [
|
|
9593
9736
|
...uploadToMux ? [audioTrackTarget] : [],
|
|
9594
9737
|
...uploadCaptionsToMux ? [captionsTrackTarget] : []
|
|
@@ -9612,6 +9755,7 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9612
9755
|
}
|
|
9613
9756
|
let staticRenditionCleanup = "not_created";
|
|
9614
9757
|
let dubbingId;
|
|
9758
|
+
let languageId;
|
|
9615
9759
|
let presignedUrl;
|
|
9616
9760
|
let uploadedTrackId;
|
|
9617
9761
|
let replacedTracks;
|
|
@@ -9646,75 +9790,114 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9646
9790
|
audioUrl = await signUrl(audioUrl, playbackId, "video", void 0, credentials);
|
|
9647
9791
|
}
|
|
9648
9792
|
console.warn("\u{1F399}\uFE0F Creating dubbing job in ElevenLabs...");
|
|
9649
|
-
const elevenLabsLangCode =
|
|
9650
|
-
const
|
|
9651
|
-
const elevenLabsSourceLangCode =
|
|
9793
|
+
const elevenLabsLangCode = toISO639_1(toBaseLanguageCode(toLanguageCode));
|
|
9794
|
+
const sourceLanguageCode = toISO639_1(toBaseLanguageCode(fromLanguageCode ?? "auto"));
|
|
9795
|
+
const elevenLabsSourceLangCode = sourceLanguageCode && sourceLanguageCode !== "auto" ? sourceLanguageCode : void 0;
|
|
9652
9796
|
console.warn(
|
|
9653
9797
|
`\u{1F50D} Creating dubbing job for asset ${assetId}: ${elevenLabsSourceLangCode ?? "auto"} -> ${elevenLabsLangCode}`
|
|
9654
9798
|
);
|
|
9799
|
+
let created;
|
|
9655
9800
|
try {
|
|
9656
|
-
|
|
9801
|
+
created = await createElevenLabsDubbingJob({
|
|
9657
9802
|
sourceUrl: audioUrl,
|
|
9658
9803
|
assetId,
|
|
9659
9804
|
elevenLabsLangCode,
|
|
9660
9805
|
elevenLabsSourceLangCode,
|
|
9661
|
-
numSpeakers,
|
|
9662
9806
|
credentials
|
|
9663
9807
|
});
|
|
9664
|
-
console.warn(`\u2705 Dubbing job created with ID: ${dubbingId}`);
|
|
9665
9808
|
} catch (error) {
|
|
9666
9809
|
wrapError(error, "Failed to create ElevenLabs dubbing job");
|
|
9667
9810
|
}
|
|
9811
|
+
if (created.kind === "failed") {
|
|
9812
|
+
const { failure } = created;
|
|
9813
|
+
const mayHaveCreated = failure.reason === "timeout" || failure.reason === "network_error" || failure.reason === "unexpected_response";
|
|
9814
|
+
console.warn(
|
|
9815
|
+
`Failed to create ElevenLabs dubbing project for asset ${assetId}: ${failure.detail}${mayHaveCreated ? " The project may have been created; check ElevenLabs before retrying." : ""}`
|
|
9816
|
+
);
|
|
9817
|
+
throw dubbingStartError(failure, { target: elevenLabsLangCode, source: elevenLabsSourceLangCode });
|
|
9818
|
+
}
|
|
9819
|
+
dubbingId = created.projectId;
|
|
9820
|
+
languageId = created.languageId;
|
|
9821
|
+
console.warn(`\u2705 Dubbing job created with ID: ${dubbingId}`);
|
|
9668
9822
|
console.warn("\u23F3 Waiting for dubbing to complete...");
|
|
9669
9823
|
const dubbingPollTimeoutSeconds = options.dubbingPollTimeoutSeconds ?? DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS;
|
|
9670
9824
|
const maxPollAttempts = Math.max(1, Math.ceil(dubbingPollTimeoutSeconds * 1e3 / DUBBING_POLL_INTERVAL_MS));
|
|
9671
9825
|
let dubbingStatus = "dubbing";
|
|
9672
9826
|
let pollAttempts = 0;
|
|
9673
|
-
let targetLanguages = [];
|
|
9674
9827
|
let dubbingError;
|
|
9675
|
-
|
|
9828
|
+
let consecutiveStatusCheckFailures = 0;
|
|
9829
|
+
while (dubbingStatus !== "completed" && pollAttempts < maxPollAttempts) {
|
|
9676
9830
|
await sleep2(DUBBING_POLL_INTERVAL_MS);
|
|
9677
9831
|
pollAttempts++;
|
|
9832
|
+
let statusResult;
|
|
9678
9833
|
try {
|
|
9679
|
-
|
|
9834
|
+
statusResult = await checkElevenLabsDubbingStatus({
|
|
9680
9835
|
dubbingId,
|
|
9836
|
+
languageId,
|
|
9681
9837
|
credentials
|
|
9682
9838
|
});
|
|
9683
|
-
dubbingStatus = statusResult.status;
|
|
9684
|
-
targetLanguages = statusResult.targetLanguages;
|
|
9685
|
-
dubbingError = statusResult.error;
|
|
9686
9839
|
} catch (error) {
|
|
9687
9840
|
wrapError(error, "Failed to check dubbing status");
|
|
9688
9841
|
}
|
|
9842
|
+
if (statusResult.kind === "failed") {
|
|
9843
|
+
const { failure } = statusResult;
|
|
9844
|
+
consecutiveStatusCheckFailures++;
|
|
9845
|
+
console.warn(`Status check for ElevenLabs dubbing project ${dubbingId} failed: ${failure.detail}`);
|
|
9846
|
+
if (!failure.retryable || consecutiveStatusCheckFailures >= MAX_CONSECUTIVE_STATUS_CHECK_FAILURES) {
|
|
9847
|
+
throw dubbingProviderError("Could not check audio dubbing progress.", failure);
|
|
9848
|
+
}
|
|
9849
|
+
continue;
|
|
9850
|
+
}
|
|
9851
|
+
consecutiveStatusCheckFailures = 0;
|
|
9852
|
+
dubbingStatus = statusResult.status;
|
|
9853
|
+
dubbingError = statusResult.error;
|
|
9689
9854
|
if (dubbingStatus === "failed") {
|
|
9690
|
-
console.warn(`\u274C ElevenLabs dubbing
|
|
9691
|
-
|
|
9855
|
+
console.warn(`\u274C ElevenLabs dubbing project ${dubbingId} (language ${languageId}) failed: ${dubbingError ? `${dubbingError.code}: ${dubbingError.message}` : "no error reported"}`);
|
|
9856
|
+
if (!dubbingError) {
|
|
9857
|
+
throw new MuxAiError("Audio dubbing failed.", { type: "processing_error" });
|
|
9858
|
+
}
|
|
9859
|
+
throw new MuxAiError(
|
|
9860
|
+
`Audio dubbing failed: ${dubbingError.message}${dubbingError.retryable ? " Please try again." : ""}`,
|
|
9861
|
+
{ type: "processing_error", retryable: dubbingError.retryable }
|
|
9862
|
+
);
|
|
9692
9863
|
}
|
|
9693
9864
|
}
|
|
9694
|
-
if (dubbingStatus !== "
|
|
9865
|
+
if (dubbingStatus !== "completed") {
|
|
9695
9866
|
throw new MuxAiError("Audio translation timed out or failed. Please try again.", { type: "timeout_error", retryable: true });
|
|
9696
9867
|
}
|
|
9697
9868
|
console.warn("\u2705 Dubbing completed successfully!");
|
|
9698
9869
|
if (uploadToS3) {
|
|
9699
9870
|
console.warn("\u{1F4E5} Downloading dubbed audio from ElevenLabs and staging to S3...");
|
|
9700
|
-
|
|
9701
|
-
|
|
9702
|
-
|
|
9703
|
-
|
|
9704
|
-
|
|
9705
|
-
|
|
9706
|
-
|
|
9707
|
-
|
|
9708
|
-
|
|
9709
|
-
|
|
9710
|
-
|
|
9711
|
-
|
|
9712
|
-
|
|
9713
|
-
|
|
9714
|
-
|
|
9715
|
-
|
|
9716
|
-
|
|
9871
|
+
for (let attempt = 1; !presignedUrl; attempt++) {
|
|
9872
|
+
let staged;
|
|
9873
|
+
try {
|
|
9874
|
+
staged = await downloadAndUploadDubbedAudio({
|
|
9875
|
+
dubbingId,
|
|
9876
|
+
languageId,
|
|
9877
|
+
assetId,
|
|
9878
|
+
toLanguageCode,
|
|
9879
|
+
s3Endpoint,
|
|
9880
|
+
s3Region,
|
|
9881
|
+
s3Bucket,
|
|
9882
|
+
storageAdapter: effectiveStorageAdapter,
|
|
9883
|
+
s3SignedUrlExpirySeconds: options.s3SignedUrlExpirySeconds,
|
|
9884
|
+
credentials
|
|
9885
|
+
});
|
|
9886
|
+
} catch (error) {
|
|
9887
|
+
wrapError(error, "Failed to download and upload dubbed audio");
|
|
9888
|
+
}
|
|
9889
|
+
if (staged.kind === "staged") {
|
|
9890
|
+
presignedUrl = staged.presignedUrl;
|
|
9891
|
+
break;
|
|
9892
|
+
}
|
|
9893
|
+
const { failure } = staged;
|
|
9894
|
+
console.warn(`Downloading dubbed audio for ElevenLabs dubbing project ${dubbingId} failed (attempt ${attempt}): ${failure.detail}`);
|
|
9895
|
+
if (!failure.retryable || attempt >= DUBBED_AUDIO_DOWNLOAD_ATTEMPTS) {
|
|
9896
|
+
throw dubbingProviderError("Could not download the dubbed audio.", failure);
|
|
9897
|
+
}
|
|
9898
|
+
await sleep2(DUBBED_AUDIO_DOWNLOAD_RETRY_DELAY_MS);
|
|
9717
9899
|
}
|
|
9900
|
+
console.warn("\u2705 Dubbed audio staged to S3 successfully!");
|
|
9718
9901
|
if (uploadToMux) {
|
|
9719
9902
|
console.warn("\u{1F4F9} Adding dubbed audio track to Mux asset...");
|
|
9720
9903
|
let outcome;
|
|
@@ -9743,9 +9926,9 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9743
9926
|
}
|
|
9744
9927
|
try {
|
|
9745
9928
|
console.warn("\u{1F4E5} Downloading dub transcript from ElevenLabs and staging to S3...");
|
|
9746
|
-
|
|
9929
|
+
const transcript = await downloadAndUploadDubTranscript({
|
|
9747
9930
|
dubbingId,
|
|
9748
|
-
|
|
9931
|
+
languageId,
|
|
9749
9932
|
assetId,
|
|
9750
9933
|
toLanguageCode,
|
|
9751
9934
|
s3Endpoint,
|
|
@@ -9755,6 +9938,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9755
9938
|
s3SignedUrlExpirySeconds: options.s3SignedUrlExpirySeconds,
|
|
9756
9939
|
credentials
|
|
9757
9940
|
});
|
|
9941
|
+
if (transcript.kind === "failed") {
|
|
9942
|
+
throw new Error(transcript.failure.detail);
|
|
9943
|
+
}
|
|
9944
|
+
captionsPresignedUrl = transcript.presignedUrl;
|
|
9758
9945
|
if (uploadCaptionsToMux) {
|
|
9759
9946
|
console.warn("\u{1F4F9} Adding dubbed captions track to Mux asset...");
|
|
9760
9947
|
const captionsOutcome = await replaceAndCreateTrack({
|
|
@@ -9794,10 +9981,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9794
9981
|
}
|
|
9795
9982
|
}
|
|
9796
9983
|
}
|
|
9797
|
-
const targetLanguage = getLanguageCodePair(
|
|
9984
|
+
const targetLanguage = getLanguageCodePair(targetLanguageCode);
|
|
9798
9985
|
return {
|
|
9799
9986
|
assetId,
|
|
9800
|
-
targetLanguageCode
|
|
9987
|
+
targetLanguageCode,
|
|
9801
9988
|
targetLanguage,
|
|
9802
9989
|
dubbingId,
|
|
9803
9990
|
uploadedTrackId,
|