@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/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 };
|
|
@@ -3112,14 +3147,13 @@ function extractTextFromVTT(vttContent) {
|
|
|
3112
3147
|
textLines.join(" ").replace(/\s+/g, " ").trim()
|
|
3113
3148
|
);
|
|
3114
3149
|
}
|
|
3150
|
+
var VTT_TIMESTAMP_RE = /^(?:(\d+):)?(\d{2}):(\d{2})(?:\.(\d{3}))?$/;
|
|
3115
3151
|
function vttTimestampToSeconds(timestamp) {
|
|
3116
|
-
const
|
|
3117
|
-
if (
|
|
3118
|
-
return 0;
|
|
3119
|
-
const hours =
|
|
3120
|
-
|
|
3121
|
-
const seconds = Number.parseFloat(parts[2]) || 0;
|
|
3122
|
-
return hours * 3600 + minutes * 60 + seconds;
|
|
3152
|
+
const match = VTT_TIMESTAMP_RE.exec(timestamp);
|
|
3153
|
+
if (!match)
|
|
3154
|
+
return void 0;
|
|
3155
|
+
const [, hours = "0", minutes, seconds, millis = "0"] = match;
|
|
3156
|
+
return Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds) + Number(millis) / 1e3;
|
|
3123
3157
|
}
|
|
3124
3158
|
function secondsToTimestamp(seconds) {
|
|
3125
3159
|
const rounded = Math.max(0, Math.floor(seconds));
|
|
@@ -3142,6 +3176,8 @@ function extractTimestampedTranscript(vttContent) {
|
|
|
3142
3176
|
if (line.includes("-->")) {
|
|
3143
3177
|
const startTime = line.split(" --> ")[0].trim();
|
|
3144
3178
|
const timeInSeconds = vttTimestampToSeconds(startTime);
|
|
3179
|
+
if (timeInSeconds === void 0)
|
|
3180
|
+
continue;
|
|
3145
3181
|
let j = i + 1;
|
|
3146
3182
|
while (j < lines.length && !lines[j].trim()) {
|
|
3147
3183
|
j++;
|
|
@@ -3170,13 +3206,17 @@ function parseVTTCues(vttContent, options = {}) {
|
|
|
3170
3206
|
if (isTimingLine(line)) {
|
|
3171
3207
|
const [startStr, endStr] = line.split(" --> ").map((s) => s.trim());
|
|
3172
3208
|
const startTime = vttTimestampToSeconds(startStr);
|
|
3173
|
-
const endTime = vttTimestampToSeconds(endStr
|
|
3209
|
+
const endTime = vttTimestampToSeconds(endStr?.split(/\s+/)[0] ?? "");
|
|
3174
3210
|
const currentCueIdentifierLine = lines[i - 1]?.trim() ?? "";
|
|
3175
3211
|
const currentCueIdentifier = isLikelyCueIdentifier({
|
|
3176
3212
|
line: currentCueIdentifierLine,
|
|
3177
3213
|
nextLine: line,
|
|
3178
3214
|
previousCueIdentifier
|
|
3179
3215
|
}) ? parseNumericCueIdentifier(currentCueIdentifierLine) : null;
|
|
3216
|
+
if (startTime === void 0 || endTime === void 0) {
|
|
3217
|
+
previousCueIdentifier = currentCueIdentifier;
|
|
3218
|
+
continue;
|
|
3219
|
+
}
|
|
3180
3220
|
const rawTextLines = [];
|
|
3181
3221
|
let j = i + 1;
|
|
3182
3222
|
while (j < lines.length && lines[j].trim() && !isTimingLine(lines[j].trim())) {
|
|
@@ -5655,7 +5695,7 @@ function transformCueText(rawVtt, transform) {
|
|
|
5655
5695
|
const transformed = lines.map((line) => {
|
|
5656
5696
|
if (line.includes("-->")) {
|
|
5657
5697
|
const startTimestamp = line.split("-->")[0].trim();
|
|
5658
|
-
currentCueStartTime = vttTimestampToSeconds(startTimestamp);
|
|
5698
|
+
currentCueStartTime = vttTimestampToSeconds(startTimestamp) ?? currentCueStartTime;
|
|
5659
5699
|
inCueText = true;
|
|
5660
5700
|
return line;
|
|
5661
5701
|
}
|
|
@@ -5687,7 +5727,7 @@ function transformCueTextBlocks(rawVtt, transform) {
|
|
|
5687
5727
|
if (line.includes("-->")) {
|
|
5688
5728
|
flushCueText();
|
|
5689
5729
|
const startTimestamp = line.split("-->")[0].trim();
|
|
5690
|
-
currentCueStartTime = vttTimestampToSeconds(startTimestamp);
|
|
5730
|
+
currentCueStartTime = vttTimestampToSeconds(startTimestamp) ?? currentCueStartTime;
|
|
5691
5731
|
transformed.push(line);
|
|
5692
5732
|
cueTextLines = [];
|
|
5693
5733
|
continue;
|
|
@@ -9204,48 +9244,73 @@ import { sleep as sleep2 } from "workflow";
|
|
|
9204
9244
|
|
|
9205
9245
|
// src/lib/elevenlabs-errors.ts
|
|
9206
9246
|
var MAX_ERROR_BODY_CHARS = 500;
|
|
9207
|
-
async function elevenLabsResponseError(context, response) {
|
|
9208
|
-
const error = new Error(`${context}: ${await describeElevenLabsErrorResponse(response)}`);
|
|
9209
|
-
if (!isRetryableElevenLabsStatus(response.status)) {
|
|
9210
|
-
error.name = "FatalError";
|
|
9211
|
-
}
|
|
9212
|
-
return error;
|
|
9213
|
-
}
|
|
9214
9247
|
function isRetryableElevenLabsStatus(status) {
|
|
9215
9248
|
return status === 408 || status === 425 || status === 429 || status >= 500;
|
|
9216
9249
|
}
|
|
9217
|
-
async function
|
|
9218
|
-
const statusLine = [response.status, response.statusText].filter(Boolean).join(" ");
|
|
9250
|
+
async function elevenLabsResponseFailure(response) {
|
|
9219
9251
|
const body = await response.text().catch(() => "");
|
|
9220
|
-
|
|
9221
|
-
|
|
9252
|
+
return httpFailure(response.status, response.statusText, body);
|
|
9253
|
+
}
|
|
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
|
+
}
|
|
9222
9274
|
}
|
|
9223
|
-
function
|
|
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) {
|
|
9224
9286
|
const trimmed = body.trim();
|
|
9225
9287
|
if (!trimmed) {
|
|
9226
|
-
return "";
|
|
9288
|
+
return { detail: "" };
|
|
9227
9289
|
}
|
|
9228
9290
|
let detail;
|
|
9229
9291
|
try {
|
|
9230
9292
|
detail = JSON.parse(trimmed).detail;
|
|
9231
9293
|
} catch {
|
|
9232
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
9294
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
9233
9295
|
}
|
|
9234
9296
|
if (typeof detail === "string" && detail) {
|
|
9235
|
-
return detail;
|
|
9297
|
+
return { detail };
|
|
9236
9298
|
}
|
|
9237
9299
|
if (Array.isArray(detail) && detail.length > 0) {
|
|
9238
|
-
return
|
|
9300
|
+
return {
|
|
9301
|
+
detail: detail.map((item) => [item.loc?.join("."), item.msg].filter(Boolean).join(": ")).join("; ")
|
|
9302
|
+
};
|
|
9239
9303
|
}
|
|
9240
9304
|
if (detail && typeof detail === "object") {
|
|
9241
9305
|
const { status, code, message, param, request_id: requestId } = detail;
|
|
9242
|
-
const
|
|
9306
|
+
const reasonCode = [status, code].find((value) => typeof value === "string" && value !== "");
|
|
9307
|
+
const reasonText = [reasonCode, message].filter(Boolean).join(": ");
|
|
9243
9308
|
const context = [param && `param: ${param}`, requestId && `request_id: ${requestId}`].filter(Boolean).join(", ");
|
|
9244
|
-
if (
|
|
9245
|
-
return context ? `${
|
|
9309
|
+
if (reasonText) {
|
|
9310
|
+
return { reason: reasonCode, detail: context ? `${reasonText} (${context})` : reasonText };
|
|
9246
9311
|
}
|
|
9247
9312
|
}
|
|
9248
|
-
return trimmed.slice(0, MAX_ERROR_BODY_CHARS);
|
|
9313
|
+
return { detail: trimmed.slice(0, MAX_ERROR_BODY_CHARS) };
|
|
9249
9314
|
}
|
|
9250
9315
|
|
|
9251
9316
|
// src/workflows/translate-audio.ts
|
|
@@ -9253,6 +9318,9 @@ var STATIC_RENDITION_POLL_INTERVAL_MS = 1e4;
|
|
|
9253
9318
|
var STATIC_RENDITION_MAX_ATTEMPTS = 180;
|
|
9254
9319
|
var MAX_DUBBING_SOURCE_BYTES = 1e9;
|
|
9255
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;
|
|
9256
9324
|
var DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS = 7200;
|
|
9257
9325
|
function getReadyAudioStaticRendition(asset) {
|
|
9258
9326
|
const files = asset.static_renditions?.files;
|
|
@@ -9386,64 +9454,128 @@ async function waitForLegacyMp4SupportRendition({
|
|
|
9386
9454
|
}
|
|
9387
9455
|
return rendition.name;
|
|
9388
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
|
+
}
|
|
9389
9491
|
async function createElevenLabsDubbingJob({
|
|
9390
9492
|
sourceUrl,
|
|
9391
9493
|
assetId,
|
|
9392
9494
|
elevenLabsLangCode,
|
|
9393
9495
|
elevenLabsSourceLangCode,
|
|
9394
|
-
numSpeakers,
|
|
9395
9496
|
credentials
|
|
9396
9497
|
}) {
|
|
9397
9498
|
"use step";
|
|
9398
|
-
const
|
|
9399
|
-
const
|
|
9400
|
-
|
|
9401
|
-
|
|
9402
|
-
|
|
9403
|
-
|
|
9404
|
-
|
|
9405
|
-
|
|
9406
|
-
|
|
9407
|
-
|
|
9408
|
-
|
|
9409
|
-
|
|
9410
|
-
|
|
9411
|
-
|
|
9412
|
-
|
|
9413
|
-
|
|
9414
|
-
}
|
|
9415
|
-
|
|
9416
|
-
})
|
|
9417
|
-
|
|
9418
|
-
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) };
|
|
9419
9519
|
}
|
|
9420
|
-
const dubbingData = await dubbingResponse.json();
|
|
9421
|
-
return dubbingData.dubbing_id;
|
|
9422
9520
|
}
|
|
9423
9521
|
async function checkElevenLabsDubbingStatus({
|
|
9424
9522
|
dubbingId,
|
|
9523
|
+
languageId,
|
|
9425
9524
|
credentials
|
|
9426
9525
|
}) {
|
|
9427
9526
|
"use step";
|
|
9428
|
-
const
|
|
9429
|
-
const
|
|
9430
|
-
|
|
9431
|
-
|
|
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) };
|
|
9432
9572
|
}
|
|
9433
|
-
});
|
|
9434
|
-
if (!statusResponse.ok) {
|
|
9435
|
-
throw await elevenLabsResponseError("Status check failed", statusResponse);
|
|
9436
9573
|
}
|
|
9437
|
-
|
|
9438
|
-
return {
|
|
9439
|
-
status: statusData.status,
|
|
9440
|
-
targetLanguages: statusData.target_languages ?? [],
|
|
9441
|
-
error: typeof statusData.error === "string" && statusData.error ? statusData.error : void 0
|
|
9442
|
-
};
|
|
9574
|
+
return { kind: "failed", failure: { retryable: false, detail: `Failed to fetch dubbed ${output}` } };
|
|
9443
9575
|
}
|
|
9444
9576
|
async function downloadAndUploadDubbedAudio({
|
|
9445
9577
|
dubbingId,
|
|
9446
|
-
|
|
9578
|
+
languageId,
|
|
9447
9579
|
assetId,
|
|
9448
9580
|
toLanguageCode,
|
|
9449
9581
|
s3Endpoint,
|
|
@@ -9454,20 +9586,32 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9454
9586
|
credentials
|
|
9455
9587
|
}) {
|
|
9456
9588
|
"use step";
|
|
9457
|
-
const
|
|
9458
|
-
|
|
9459
|
-
|
|
9460
|
-
|
|
9461
|
-
|
|
9462
|
-
|
|
9463
|
-
|
|
9464
|
-
|
|
9465
|
-
|
|
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
|
+
};
|
|
9466
9610
|
}
|
|
9467
9611
|
const dubbedAudio = new Uint8Array(await audioResponse.arrayBuffer());
|
|
9468
9612
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9469
9613
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
9470
|
-
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}
|
|
9614
|
+
const audioKey = `audio-translations/${assetId}/auto-to-${toLanguageCode}-${Date.now()}.${extension}`;
|
|
9471
9615
|
await putObjectWithStorageAdapter({
|
|
9472
9616
|
accessKeyId: s3AccessKeyId,
|
|
9473
9617
|
secretAccessKey: s3SecretAccessKey,
|
|
@@ -9476,7 +9620,7 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9476
9620
|
bucket: s3Bucket,
|
|
9477
9621
|
key: audioKey,
|
|
9478
9622
|
body: dubbedAudio,
|
|
9479
|
-
contentType:
|
|
9623
|
+
contentType: Object.keys(formats).find((type) => formats[type] === extension)
|
|
9480
9624
|
}, storageAdapter);
|
|
9481
9625
|
const presignedUrl = await createPresignedGetUrlWithStorageAdapter({
|
|
9482
9626
|
accessKeyId: s3AccessKeyId,
|
|
@@ -9490,11 +9634,11 @@ async function downloadAndUploadDubbedAudio({
|
|
|
9490
9634
|
const expiryHours = Math.round((s3SignedUrlExpirySeconds ?? 86400) / 3600);
|
|
9491
9635
|
console.warn(`\u2705 Audio uploaded successfully to: ${audioKey}`);
|
|
9492
9636
|
console.warn(`\u{1F517} Generated presigned URL (expires in ${expiryHours} hour${expiryHours === 1 ? "" : "s"})`);
|
|
9493
|
-
return presignedUrl;
|
|
9637
|
+
return { kind: "staged", presignedUrl };
|
|
9494
9638
|
}
|
|
9495
9639
|
async function downloadAndUploadDubTranscript({
|
|
9496
9640
|
dubbingId,
|
|
9497
|
-
|
|
9641
|
+
languageId,
|
|
9498
9642
|
assetId,
|
|
9499
9643
|
toLanguageCode,
|
|
9500
9644
|
s3Endpoint,
|
|
@@ -9505,20 +9649,13 @@ async function downloadAndUploadDubTranscript({
|
|
|
9505
9649
|
credentials
|
|
9506
9650
|
}) {
|
|
9507
9651
|
"use step";
|
|
9508
|
-
const
|
|
9509
|
-
|
|
9510
|
-
|
|
9511
|
-
headers: {
|
|
9512
|
-
"xi-api-key": elevenLabsApiKey
|
|
9513
|
-
}
|
|
9514
|
-
});
|
|
9515
|
-
if (!transcriptResponse.ok) {
|
|
9516
|
-
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;
|
|
9517
9655
|
}
|
|
9518
|
-
const
|
|
9519
|
-
|
|
9520
|
-
|
|
9521
|
-
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" } };
|
|
9522
9659
|
}
|
|
9523
9660
|
const s3AccessKeyId = env_default.S3_ACCESS_KEY_ID;
|
|
9524
9661
|
const s3SecretAccessKey = env_default.S3_SECRET_ACCESS_KEY;
|
|
@@ -9543,7 +9680,7 @@ async function downloadAndUploadDubTranscript({
|
|
|
9543
9680
|
expiresInSeconds: s3SignedUrlExpirySeconds ?? 86400
|
|
9544
9681
|
}, storageAdapter);
|
|
9545
9682
|
console.warn(`\u2705 Dub transcript uploaded successfully to: ${vttKey}`);
|
|
9546
|
-
return presignedUrl;
|
|
9683
|
+
return { kind: "staged", presignedUrl };
|
|
9547
9684
|
}
|
|
9548
9685
|
async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
9549
9686
|
"use workflow";
|
|
@@ -9565,12 +9702,16 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9565
9702
|
if (provider !== "elevenlabs") {
|
|
9566
9703
|
throw new MuxAiError("Only ElevenLabs provider is currently supported for audio translation.", { type: "validation_error" });
|
|
9567
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
|
+
}
|
|
9568
9709
|
const credentials = providedCredentials;
|
|
9569
9710
|
const effectiveStorageAdapter = storageAdapter;
|
|
9570
9711
|
const trackPassthrough = validateTrackPassthrough(providedTrackPassthrough) ?? buildMuxAiTrackPassthrough("translate-audio");
|
|
9571
9712
|
const uploadToMux = uploadToMuxOption !== false;
|
|
9572
9713
|
const uploadToS3 = uploadToS3Option || uploadToMux || uploadCaptionsToMux;
|
|
9573
|
-
const muxLangCode =
|
|
9714
|
+
const muxLangCode = targetLanguageCode;
|
|
9574
9715
|
const trackName = providedTrackName ?? `${getLanguageName(muxLangCode) ?? muxLangCode.toUpperCase()} (Auto-dubbed)`;
|
|
9575
9716
|
const audioTrackTarget = { type: "audio", languageCode: muxLangCode, name: trackName };
|
|
9576
9717
|
const captionsTrackTarget = { type: "text", languageCode: muxLangCode, name: trackName };
|
|
@@ -9584,6 +9725,13 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9584
9725
|
}
|
|
9585
9726
|
const { asset: initialAsset, playbackId, policy } = await getPlaybackIdForAsset(assetId, credentials, options.assetSnapshot);
|
|
9586
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
|
+
}
|
|
9587
9735
|
for (const target of [
|
|
9588
9736
|
...uploadToMux ? [audioTrackTarget] : [],
|
|
9589
9737
|
...uploadCaptionsToMux ? [captionsTrackTarget] : []
|
|
@@ -9607,6 +9755,7 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9607
9755
|
}
|
|
9608
9756
|
let staticRenditionCleanup = "not_created";
|
|
9609
9757
|
let dubbingId;
|
|
9758
|
+
let languageId;
|
|
9610
9759
|
let presignedUrl;
|
|
9611
9760
|
let uploadedTrackId;
|
|
9612
9761
|
let replacedTracks;
|
|
@@ -9641,75 +9790,114 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9641
9790
|
audioUrl = await signUrl(audioUrl, playbackId, "video", void 0, credentials);
|
|
9642
9791
|
}
|
|
9643
9792
|
console.warn("\u{1F399}\uFE0F Creating dubbing job in ElevenLabs...");
|
|
9644
|
-
const elevenLabsLangCode =
|
|
9645
|
-
const
|
|
9646
|
-
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;
|
|
9647
9796
|
console.warn(
|
|
9648
9797
|
`\u{1F50D} Creating dubbing job for asset ${assetId}: ${elevenLabsSourceLangCode ?? "auto"} -> ${elevenLabsLangCode}`
|
|
9649
9798
|
);
|
|
9799
|
+
let created;
|
|
9650
9800
|
try {
|
|
9651
|
-
|
|
9801
|
+
created = await createElevenLabsDubbingJob({
|
|
9652
9802
|
sourceUrl: audioUrl,
|
|
9653
9803
|
assetId,
|
|
9654
9804
|
elevenLabsLangCode,
|
|
9655
9805
|
elevenLabsSourceLangCode,
|
|
9656
|
-
numSpeakers,
|
|
9657
9806
|
credentials
|
|
9658
9807
|
});
|
|
9659
|
-
console.warn(`\u2705 Dubbing job created with ID: ${dubbingId}`);
|
|
9660
9808
|
} catch (error) {
|
|
9661
9809
|
wrapError(error, "Failed to create ElevenLabs dubbing job");
|
|
9662
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}`);
|
|
9663
9822
|
console.warn("\u23F3 Waiting for dubbing to complete...");
|
|
9664
9823
|
const dubbingPollTimeoutSeconds = options.dubbingPollTimeoutSeconds ?? DEFAULT_DUBBING_POLL_TIMEOUT_SECONDS;
|
|
9665
9824
|
const maxPollAttempts = Math.max(1, Math.ceil(dubbingPollTimeoutSeconds * 1e3 / DUBBING_POLL_INTERVAL_MS));
|
|
9666
9825
|
let dubbingStatus = "dubbing";
|
|
9667
9826
|
let pollAttempts = 0;
|
|
9668
|
-
let targetLanguages = [];
|
|
9669
9827
|
let dubbingError;
|
|
9670
|
-
|
|
9828
|
+
let consecutiveStatusCheckFailures = 0;
|
|
9829
|
+
while (dubbingStatus !== "completed" && pollAttempts < maxPollAttempts) {
|
|
9671
9830
|
await sleep2(DUBBING_POLL_INTERVAL_MS);
|
|
9672
9831
|
pollAttempts++;
|
|
9832
|
+
let statusResult;
|
|
9673
9833
|
try {
|
|
9674
|
-
|
|
9834
|
+
statusResult = await checkElevenLabsDubbingStatus({
|
|
9675
9835
|
dubbingId,
|
|
9836
|
+
languageId,
|
|
9676
9837
|
credentials
|
|
9677
9838
|
});
|
|
9678
|
-
dubbingStatus = statusResult.status;
|
|
9679
|
-
targetLanguages = statusResult.targetLanguages;
|
|
9680
|
-
dubbingError = statusResult.error;
|
|
9681
9839
|
} catch (error) {
|
|
9682
9840
|
wrapError(error, "Failed to check dubbing status");
|
|
9683
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;
|
|
9684
9854
|
if (dubbingStatus === "failed") {
|
|
9685
|
-
console.warn(`\u274C ElevenLabs dubbing
|
|
9686
|
-
|
|
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
|
+
);
|
|
9687
9863
|
}
|
|
9688
9864
|
}
|
|
9689
|
-
if (dubbingStatus !== "
|
|
9865
|
+
if (dubbingStatus !== "completed") {
|
|
9690
9866
|
throw new MuxAiError("Audio translation timed out or failed. Please try again.", { type: "timeout_error", retryable: true });
|
|
9691
9867
|
}
|
|
9692
9868
|
console.warn("\u2705 Dubbing completed successfully!");
|
|
9693
9869
|
if (uploadToS3) {
|
|
9694
9870
|
console.warn("\u{1F4E5} Downloading dubbed audio from ElevenLabs and staging to S3...");
|
|
9695
|
-
|
|
9696
|
-
|
|
9697
|
-
|
|
9698
|
-
|
|
9699
|
-
|
|
9700
|
-
|
|
9701
|
-
|
|
9702
|
-
|
|
9703
|
-
|
|
9704
|
-
|
|
9705
|
-
|
|
9706
|
-
|
|
9707
|
-
|
|
9708
|
-
|
|
9709
|
-
|
|
9710
|
-
|
|
9711
|
-
|
|
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);
|
|
9712
9899
|
}
|
|
9900
|
+
console.warn("\u2705 Dubbed audio staged to S3 successfully!");
|
|
9713
9901
|
if (uploadToMux) {
|
|
9714
9902
|
console.warn("\u{1F4F9} Adding dubbed audio track to Mux asset...");
|
|
9715
9903
|
let outcome;
|
|
@@ -9738,9 +9926,9 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9738
9926
|
}
|
|
9739
9927
|
try {
|
|
9740
9928
|
console.warn("\u{1F4E5} Downloading dub transcript from ElevenLabs and staging to S3...");
|
|
9741
|
-
|
|
9929
|
+
const transcript = await downloadAndUploadDubTranscript({
|
|
9742
9930
|
dubbingId,
|
|
9743
|
-
|
|
9931
|
+
languageId,
|
|
9744
9932
|
assetId,
|
|
9745
9933
|
toLanguageCode,
|
|
9746
9934
|
s3Endpoint,
|
|
@@ -9750,6 +9938,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9750
9938
|
s3SignedUrlExpirySeconds: options.s3SignedUrlExpirySeconds,
|
|
9751
9939
|
credentials
|
|
9752
9940
|
});
|
|
9941
|
+
if (transcript.kind === "failed") {
|
|
9942
|
+
throw new Error(transcript.failure.detail);
|
|
9943
|
+
}
|
|
9944
|
+
captionsPresignedUrl = transcript.presignedUrl;
|
|
9753
9945
|
if (uploadCaptionsToMux) {
|
|
9754
9946
|
console.warn("\u{1F4F9} Adding dubbed captions track to Mux asset...");
|
|
9755
9947
|
const captionsOutcome = await replaceAndCreateTrack({
|
|
@@ -9789,10 +9981,10 @@ async function translateAudio(assetId, toLanguageCode, options = {}) {
|
|
|
9789
9981
|
}
|
|
9790
9982
|
}
|
|
9791
9983
|
}
|
|
9792
|
-
const targetLanguage = getLanguageCodePair(
|
|
9984
|
+
const targetLanguage = getLanguageCodePair(targetLanguageCode);
|
|
9793
9985
|
return {
|
|
9794
9986
|
assetId,
|
|
9795
|
-
targetLanguageCode
|
|
9987
|
+
targetLanguageCode,
|
|
9796
9988
|
targetLanguage,
|
|
9797
9989
|
dubbingId,
|
|
9798
9990
|
uploadedTrackId,
|