From 2994b680633b1b0d0e26baf8aa165453334f416f Mon Sep 17 00:00:00 2001 From: imeepos Date: Tue, 2 Sep 2025 18:25:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=9B=BE=E5=83=8F?= =?UTF-8?q?=E8=BD=AC=E8=A7=86=E9=A2=91=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增ImageToVideoParams接口定义 - 实现imageToVideo方法支持图像转视频API调用 - 使用application/x-www-form-urlencoded格式发送POST请求 - 支持img_url和duration参数配置 - 添加完整的错误处理和日志记录 --- src/sdk/index.ts | 65 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/src/sdk/index.ts b/src/sdk/index.ts index e8142b5..7a63da9 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -40,6 +40,16 @@ interface GenerateImageParams { img_file: string; } +/** + * 图像转视频请求参数 + */ +interface ImageToVideoParams { + /** 图像URL地址 */ + img_url: string; + /** 视频时长,默认为0 */ + duration?: number; +} + export function isGetFileInfoSuccessCallbackResult(val: any): val is Taro.getFileInfo.SuccessCallbackResult { return val && Reflect.has(val, 'digest') } @@ -164,7 +174,7 @@ export class BowongAISDK { sourceType }); - console.log({chooseResult}) + console.log({ chooseResult }) // 选择图片完成后触发回调 onImageSelected?.(); @@ -338,6 +348,59 @@ export class BowongAISDK { throw new Error(`查询任务状态失败,已重试${maxRetries}次: ${error instanceof Error ? error.message : String(error)}`); } } + + + + /** + * 图像转视频 + * @param params 图像转视频参数 + * @returns Promise 返回任务ID + * + * 对应 curl 命令: + * curl -X 'POST' \ + * 'https://bowongai-test--text-video-agent-fastapi-app.modal.run/api/custom/transform/i2v' \ + * -H 'accept: application/json' \ + * -H 'Content-Type: application/x-www-form-urlencoded' \ + * -d 'img_url=string&duration=0' + */ + async imageToVideo(params: ImageToVideoParams): Promise { + const { img_url, duration = 0 } = params; + const url = `${this.baseUrl}/api/custom/transform/i2v`; + + try { + // 构建 form data + const formData = new URLSearchParams(); + formData.append('img_url', img_url); + formData.append('duration', duration.toString()); + + const response = await Taro.request({ + url, + method: 'POST', + header: { + 'accept': 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + data: formData.toString() + }); + + if (response.statusCode !== 200) { + throw new Error(`HTTP ${response.statusCode}: 图像转视频请求失败`); + } + + const result = response.data as ApiResponse; + + if (!result.status) { + throw new Error(result.msg || '图像转视频请求失败'); + } + + console.log('图像转视频任务提交成功:', result.data); + return result.data; + + } catch (error) { + console.error('图像转视频失败:', error); + throw error; + } + } } /**