Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cli/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
videoGenerate,
videoEdit,
videoRef,
videoRetalk,
videoStyle,
videoTaskGet,
videoDownload,
visionDescribe,
Expand Down Expand Up @@ -414,6 +416,8 @@ export const commands: Record<string, AnyCommand> = {
"video generate": videoGenerate,
"video edit": videoEdit,
"video ref": videoRef,
"video retalk": videoRetalk,
"video style": videoStyle,
"video task get": videoTaskGet,
"video download": videoDownload,
"vision describe": visionDescribe,
Expand Down
65 changes: 57 additions & 8 deletions packages/commands/src/commands/shared/dashscope-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ function isTimeoutLikeError(error: unknown): boolean {
return false;
}

function joinHints(...parts: Array<string | undefined>): string | undefined {
const joined = parts
.map((part) => part?.trim())
.filter((part): part is string => Boolean(part))
.join(" ");
return joined.length > 0 ? joined : undefined;
}

/**
* Terminal FAILED/CANCELED/UNKNOWN thrown from load() set api without httpStatus.
* HTTP failures from the poll GET go through mapApiError and set api.httpStatus.
*/
function isTerminalTaskStatusError(error: BailianError): boolean {
return (
error.exitCode === ExitCode.GENERAL &&
error.api !== undefined &&
error.api.httpStatus === undefined
);
}

/** Poll a DashScope async task until SUCCEEDED, FAILED, CANCELED, or UNKNOWN. */
export async function pollDashScopeTask(
ctx: {
Expand All @@ -48,6 +68,15 @@ export async function pollDashScopeTask(
extraCheck?: (data: DashScopeTaskResponse) => void;
},
): Promise<DashScopeTaskResponse> {
const pollInterruptHint = ctx.localize({
"en-US": `Task may still be running. Query or re-download with task_id ${taskId}; do not resubmit.`,
"zh-CN": `任务可能仍在执行。请用 task_id ${taskId} 查询或重新下载,勿重新提交。`,
});
const terminalHint = ctx.localize({
"en-US": `Inspect with task_id ${taskId}.`,
"zh-CN": `可用 task_id ${taskId} 查询任务。`,
});

try {
return await poll<DashScopeTaskResponse>(ctx.client, {
output: ctx.output,
Expand Down Expand Up @@ -93,22 +122,42 @@ export async function pollDashScopeTask(
getStatus: (data) => (data as DashScopeTaskResponse).output.task_status,
});
} catch (error) {
// Include submitted task_id for both poll-loop TIMEOUT and hung single GETs (AbortError).
// Keep messages intact for AEM; put task_id only in hint (timeout / interrupt / terminal).
if (isTimeoutLikeError(error)) {
throw new BailianError(
ctx.localize({
"en-US": `Polling timed out. task_id=${taskId}`,
"zh-CN": `轮询超时。task_id=${taskId}`,
"en-US": "Polling timed out.",
"zh-CN": "轮询超时。",
}),
ExitCode.TIMEOUT,
ctx.localize({
"en-US": `Try increasing --timeout, or query the task with id ${taskId}.`,
"zh-CN": `可增大 --timeout,或用 task_id ${taskId} 查询任务状态。`,
}),
joinHints(
ctx.localize({
"en-US": `Try increasing --timeout, or query the task with id ${taskId}.`,
"zh-CN": `可增大 --timeout,或用 task_id ${taskId} 查询任务状态。`,
}),
pollInterruptHint,
),
{ cause: error },
);
}
throw error;
if (error instanceof BailianError) {
if (isTerminalTaskStatusError(error)) {
throw new BailianError(error.message, error.exitCode, joinHints(error.hint, terminalHint), {
cause: error.cause ?? error,
api: error.api,
rawResponse: error.rawResponse,
});
}
// Poll interrupt: preserve message; append do-not-resubmit hint (includes task_id).
throw new BailianError(
error.message,
error.exitCode,
joinHints(error.hint, pollInterruptHint),
{ cause: error.cause ?? error, api: error.api, rawResponse: error.rawResponse },
);
}
const originalMessage = error instanceof Error ? error.message : String(error);
throw new BailianError(originalMessage, ExitCode.GENERAL, pollInterruptHint, { cause: error });
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/commands/src/commands/video/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export default defineCommand({
}

const downloadUrl =
taskInfo.output.video_url || (taskInfo.output.results && taskInfo.output.results[0]?.url);
taskInfo.output.video_url ||
taskInfo.output.output_video_url ||
(taskInfo.output.results && taskInfo.output.results[0]?.url);

if (!downloadUrl) {
throw new BailianError("No download URL available for this task.", ExitCode.GENERAL);
Expand Down
5 changes: 3 additions & 2 deletions packages/commands/src/commands/video/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import { BOOL_FLAG_PROMPT_EXTEND_API_DEFAULT, BOOL_FLAG_WATERMARK } from "bailia
export default defineCommand({
description: {
"en-US":
"Edit a video with happyhorse-1.0-video-edit (style transfer, object replacement, etc.)",
"zh-CN": "使用 happyhorse-1.0-video-edit 编辑视频(风格转换、对象替换等)",
"Edit a video with happyhorse-1.0-video-edit (prompt-based style/object edits; preset comic/ink restyle → video style)",
"zh-CN":
"使用 happyhorse-1.0-video-edit 编辑视频(基于提示词的风格/对象编辑;预设漫画/水墨重绘请用 video style)",
},
auth: "apiKey",
usageArgs: "--video <url> --prompt <text> [flags]",
Expand Down
175 changes: 175 additions & 0 deletions packages/commands/src/commands/video/retalk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import {
defineCommand,
image2videoPath,
stripUndefined,
type DashScopeVideoRetalkRequest,
type FlagsDef,
} from "bailian-cli-core";
import {
extractVideoUrlField,
mediaInputError,
previewMediaUrl,
resolveMediaUrl,
runSpecializedVideoLifecycle,
SPECIALIZED_VIDEO_SHARED_FLAGS,
specializedLifecycleFlagError,
} from "./specialized-shared.ts";

const MODEL = "videoretalk";

const RETALK_FLAGS = {
video: {
type: "string",
valueHint: "<url-or-path>",
description: {
"en-US": "Input video URL or local file (mp4/avi/mov)",
"zh-CN": "输入视频 URL 或本地文件(mp4/avi/mov)",
},
required: true,
},
audio: {
type: "string",
valueHint: "<url-or-path>",
description: {
"en-US": "Driving audio URL or local file (wav/mp3/aac)",
"zh-CN": "驱动音频 URL 或本地文件(wav/mp3/aac)",
},
required: true,
},
refImage: {
type: "string",
valueHint: "<url-or-path>",
description: {
"en-US": "Optional reference face image when the video has multiple people",
"zh-CN": "多人视频中可选的参考人脸图",
},
},
videoExtension: {
type: "boolean",
valueHint: "<bool>",
description: {
"en-US":
"Extend video by alternating reverse/forward playback when audio is longer (default: false)",
"zh-CN": "音频长于视频时是否倒放/正放交替延展(默认 false)",
},
},
faceThreshold: {
type: "number",
valueHint: "<n>",
description: {
"en-US": "Face match threshold 120–200 (default: 170; only with --ref-image)",
"zh-CN": "人脸匹配阈值 120–200(默认 170;仅与 --ref-image 联用)",
},
},
...SPECIALIZED_VIDEO_SHARED_FLAGS,
} satisfies FlagsDef;

export default defineCommand({
description: {
"en-US": "Drive a talking-head video with audio (VideoRetalk)",
"zh-CN": "用音频驱动人像口型视频(VideoRetalk)",
},
auth: "apiKey",
usageArgs: "--video <url-or-path> --audio <url-or-path> [flags]",
flags: RETALK_FLAGS,
exampleArgs: [
{
"en-US":
"--video https://example.com/face.mp4 --audio https://example.com/speech.wav --download out.mp4",
"zh-CN":
"--video https://example.com/face.mp4 --audio https://example.com/speech.wav --download out.mp4",
},
{
"en-US":
"--video ./face.mp4 --audio ./speech.wav --ref-image ./person.jpg --face-threshold 170",
"zh-CN":
"--video ./face.mp4 --audio ./speech.wav --ref-image ./person.jpg --face-threshold 170",
},
],
notes: [
{
"en-US":
"Fixed model videoretalk (Beijing only). Wait-mode polling uses at least 600s (--timeout lower values are raised).",
"zh-CN":
"固定模型 videoretalk(仅北京)。等待模式轮询至少 600 秒(更小的 --timeout 会被抬高)。",
},
],
validate: (flags) => {
const sharedError = specializedLifecycleFlagError(flags);
if (sharedError) return sharedError;
if (flags.faceThreshold !== undefined) {
if (
!Number.isInteger(flags.faceThreshold) ||
flags.faceThreshold < 120 ||
flags.faceThreshold > 200
) {
return {
"en-US": "--face-threshold must be an integer between 120 and 200.",
"zh-CN": "--face-threshold 必须是 120–200 的整数。",
};
}
if (!flags.refImage) {
return {
"en-US": "--face-threshold requires --ref-image.",
"zh-CN": "--face-threshold 需要同时提供 --ref-image。",
};
}
}
const videoError = mediaInputError(flags.video, "--video", { allowLocal: true });
if (videoError) return videoError;
const audioError = mediaInputError(flags.audio, "--audio", { allowLocal: true });
if (audioError) return audioError;
if (flags.refImage !== undefined) {
const refError = mediaInputError(flags.refImage, "--ref-image", { allowLocal: true });
if (refError) return refError;
}
return undefined;
},
async run(ctx) {
const { flags } = ctx;

function buildRequest(media: {
videoUrl: string;
audioUrl: string;
refImageUrl?: string;
}): DashScopeVideoRetalkRequest {
const parameters = stripUndefined({
video_extension: flags.videoExtension,
query_face_threshold: flags.faceThreshold,
});
return {
model: MODEL,
input: stripUndefined({
video_url: media.videoUrl,
audio_url: media.audioUrl,
ref_image_url: media.refImageUrl,
}) as DashScopeVideoRetalkRequest["input"],
parameters: Object.keys(parameters).length > 0 ? parameters : undefined,
};
}

const previewRequest = buildRequest({
videoUrl: previewMediaUrl(flags.video),
audioUrl: previewMediaUrl(flags.audio),
refImageUrl: flags.refImage !== undefined ? previewMediaUrl(flags.refImage) : undefined,
});

await runSpecializedVideoLifecycle(ctx, {
endpointPath: image2videoPath(),
previewRequest,
resolveLiveRequest: async () => {
const videoUrl = await resolveMediaUrl(ctx.client, flags.video, MODEL);
const audioUrl = await resolveMediaUrl(ctx.client, flags.audio, MODEL);
const refImageUrl =
flags.refImage !== undefined
? await resolveMediaUrl(ctx.client, flags.refImage, MODEL)
: undefined;
return buildRequest({ videoUrl, audioUrl, refImageUrl });
},
extractVideoUrl: extractVideoUrlField,
pollInterval: flags.pollInterval,
download: flags.download,
async: flags.async,
});
},
});
Loading