feat: 添加图像转视频功能
- 新增ImageToVideoParams接口定义 - 实现imageToVideo方法支持图像转视频API调用 - 使用application/x-www-form-urlencoded格式发送POST请求 - 支持img_url和duration参数配置 - 添加完整的错误处理和日志记录
This commit is contained in:
parent
a27577dd3c
commit
2994b68063
|
|
@ -40,6 +40,16 @@ interface GenerateImageParams {
|
||||||
img_file: string;
|
img_file: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图像转视频请求参数
|
||||||
|
*/
|
||||||
|
interface ImageToVideoParams {
|
||||||
|
/** 图像URL地址 */
|
||||||
|
img_url: string;
|
||||||
|
/** 视频时长,默认为0 */
|
||||||
|
duration?: number;
|
||||||
|
}
|
||||||
|
|
||||||
export function isGetFileInfoSuccessCallbackResult(val: any): val is Taro.getFileInfo.SuccessCallbackResult {
|
export function isGetFileInfoSuccessCallbackResult(val: any): val is Taro.getFileInfo.SuccessCallbackResult {
|
||||||
return val && Reflect.has(val, 'digest')
|
return val && Reflect.has(val, 'digest')
|
||||||
}
|
}
|
||||||
|
|
@ -338,6 +348,59 @@ export class BowongAISDK {
|
||||||
throw new Error(`查询任务状态失败,已重试${maxRetries}次: ${error instanceof Error ? error.message : String(error)}`);
|
throw new Error(`查询任务状态失败,已重试${maxRetries}次: ${error instanceof Error ? error.message : String(error)}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图像转视频
|
||||||
|
* @param params 图像转视频参数
|
||||||
|
* @returns Promise<string> 返回任务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<string> {
|
||||||
|
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<string>;
|
||||||
|
|
||||||
|
if (!result.status) {
|
||||||
|
throw new Error(result.msg || '图像转视频请求失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('图像转视频任务提交成功:', result.data);
|
||||||
|
return result.data;
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('图像转视频失败:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue