Skip to content

🎬 火宝短剧 - 基于AI的一站式短剧生成平台 《一句话生成完整短剧,从剧本到成片全自动化》 Huobao Drama - An AI-Powered End-to-End Short Drama Generator "One Sentence to Complete Drama: Fully Automated from Script to Final Video"

TypeScript

feat(adapters): 新增 MiniMax H3 视频生成适配器

Connorc7a04883e584
diff --git a/backend/src/routes/aiConfigs.ts b/backend/src/routes/aiConfigs.ts
index fd6d2ec..fe8e2db 100644
--- a/backend/src/routes/aiConfigs.ts
+++ b/backend/src/routes/aiConfigs.ts
@@ -69,6 +69,16 @@ function buildProbe(serviceType: string, provider: string, baseUrl: string, mode
}
}
+ if (p === 'minimax') {
+ // MiniMax 仅提供视频服务,空请求体探测鉴权/端点连通性
+ return {
+ method: 'POST',
+ url: joinProviderUrl(baseUrl, '/v2', '/video_generation'),
+ headers: bearerHeaders(apiKey, true),
+ body: {},
+ }
+ }
+
return {
method: 'GET',
url: joinProviderUrl(baseUrl, '', m ? `/${m}` : '/'),
diff --git a/backend/src/services/adapters/minimax-video.ts b/backend/src/services/adapters/minimax-video.ts
new file mode 100644
index 0000000..3a27e17
--- /dev/null
+++ b/backend/src/services/adapters/minimax-video.ts
@@ -0,0 +1,171 @@
+/**
+ * MiniMax H3 视频生成 Adapter
+ * 端点: POST /v2/video_generation -> { task_id }
+ * 轮询: GET /v2/query/video_generation/{task_id} -> { task: { status, content.url, error } }
+ *
+ * 仅支持 MiniMax-H3 系列模型。content[] 多模态结构:
+ * - text 提示词(必填,≤7000 字符)
+ * - image_url role: first_frame / last_frame / reference_image(参考图 ≤9)
+ * - video_url role: reference_video(≤3)
+ * - audio_url 参考音频(≤3),混合总数 ≤12
+ *
+ * 注意:
+ * - 文生视频 ratio 必填且不能为 adaptive;有首帧图(图生视频)时恒为 adaptive,省略 ratio
+ * - H3 原生音画同步生成,官方文档无独立 generate_audio 开关,该字段忽略
+ */
+import type {
+ VideoProviderAdapter,
+ ProviderRequest,
+ AIConfig,
+ VideoGenerationRecord,
+ VideoGenResponse,
+ VideoPollResponse,
+} from './types'
+import { joinProviderUrl } from './url'
+
+/** 仅支持 MiniMax-H3 系列(前缀匹配,兼容未来 H3.x 变体) */
+const H3_MODEL_PREFIX = 'minimax-h3'
+const DEFAULT_MODEL = 'MiniMax-H3'
+
+/** 多模态参考素材上限:图片 9、视频 3、音频 3,混合总数 12 */
+const REF_LIMITS = { images: 9, videos: 3, audios: 3, total: 12 } as const
+
+const PROMPT_MAX_CHARS = 7000
+
+/** MiniMax 支持的宽高比 */
+const VALID_RATIOS = new Set(['16:9', '9:16', '1:1', '4:3', '3:4', '21:9'])
+
+function parseUrlArray(raw?: string | null): string[] {
+ if (!raw) return []
+ try {
+ const arr = JSON.parse(raw)
+ return Array.isArray(arr) ? arr.filter((u) => typeof u === 'string' && u.trim()) : []
+ } catch {
+ return []
+ }
+}
+
+export class MiniMaxVideoAdapter implements VideoProviderAdapter {
+ provider = 'minimax'
+
+ buildGenerateRequest(config: AIConfig, record: VideoGenerationRecord): ProviderRequest {
+ const model = record.model || config.model || DEFAULT_MODEL
+ if (!model.toLowerCase().startsWith(H3_MODEL_PREFIX)) {
+ throw new Error(`仅支持 MiniMax H3 系列模型(MiniMax-H3*),当前: ${model}`)
+ }
+
+ const prompt = (record.prompt || '').trim()
+ const refImages = parseUrlArray(record.referenceImageUrls)
+ const refVideos = parseUrlArray(record.referenceVideoUrls)
+ const refAudios = parseUrlArray(record.referenceAudioUrls)
+ const firstFrame = (record.firstFrameUrl || record.imageUrl || '').trim()
+ const lastFrame = (record.lastFrameUrl || '').trim()
+
+ if (!prompt) throw new Error('MiniMax H3 要求必须提供提示词(text content)')
+ if (prompt.length > PROMPT_MAX_CHARS) {
+ throw new Error(`提示词超长:MiniMax H3 上限 ${PROMPT_MAX_CHARS} 字符,当前 ${prompt.length}`)
+ }
+
+ const totalRefs = refImages.length + refVideos.length + refAudios.length + (firstFrame ? 1 : 0) + (lastFrame ? 1 : 0)
+ if (refImages.length > REF_LIMITS.images || refVideos.length > REF_LIMITS.videos || refAudios.length > REF_LIMITS.audios || totalRefs > REF_LIMITS.total) {
+ throw new Error(`参考素材超限:图片≤${REF_LIMITS.images}、视频≤${REF_LIMITS.videos}、音频≤${REF_LIMITS.audios}、总数≤${REF_LIMITS.total}`)
+ }
+
+ const content: any[] = [{ type: 'text', text: prompt }]
+ if (firstFrame) content.push({ type: 'image_url', image_url: { url: firstFrame }, role: 'first_frame' })
+ if (lastFrame) content.push({ type: 'image_url', image_url: { url: lastFrame }, role: 'last_frame' })
+ for (const url of refImages) {
+ content.push({ type: 'image_url', image_url: { url }, role: 'reference_image' })
+ }
+ for (const url of refVideos) {
+ content.push({ type: 'video_url', video_url: { url }, role: 'reference_video' })
+ }
+ for (const url of refAudios) {
+ content.push({ type: 'audio_url', audio_url: { url } })
+ }
+
+ const body: any = {
+ model,
+ content,
+ duration: this.normalizeDuration(record.duration),
+ resolution: this.normalizeResolution(record.resolution),
+ }
+
+ // 图生视频(有首帧)ratio 恒为 adaptive,省略;文生视频 ratio 必填
+ if (!firstFrame) {
+ const ratio = (record.aspectRatio || '').trim()
+ body.ratio = VALID_RATIOS.has(ratio) ? ratio : '16:9'
+ }
+
+ return {
+ url: joinProviderUrl(config.baseUrl, '/v2', '/video_generation'),
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${config.apiKey}`,
+ },
+ body,
+ }
+ }
+
+ parseGenerateResponse(result: any): VideoGenResponse {
+ if (result.task_id) {
+ return { isAsync: true, taskId: String(result.task_id) }
+ }
+ const videoUrl = result.task?.content?.url || result.content?.url || result.video_url
+ if (videoUrl) {
+ return { isAsync: false, videoUrl }
+ }
+ throw new Error('No task_id or video url in response')
+ }
+
+ buildPollRequest(config: AIConfig, taskId: string): ProviderRequest {
+ return {
+ url: joinProviderUrl(config.baseUrl, '/v2', `/query/video_generation/${taskId}`),
+ method: 'GET',
+ headers: {
+ 'Authorization': `Bearer ${config.apiKey}`,
+ },
+ body: undefined,
+ }
+ }
+
+ parsePollResponse(result: any): VideoPollResponse {
+ // 官方响应为 { task: { status, content: { url }, error } },兼容顶层平铺
+ const task = result.task && typeof result.task === 'object' ? result.task : result
+ const status = task.status
+
+ if (status === 'succeeded') {
+ return {
+ status: 'completed',
+ videoUrl: task.content?.url || task.video_url,
+ }
+ }
+ if (status === 'failed' || status === 'cancelled') {
+ const err = task.error
+ const msg = typeof err === 'string' ? err : (err?.message || JSON.stringify(err) || 'Video generation failed')
+ const code = err && typeof err === 'object' && err.code ? `[${err.code}] ` : ''
+ return { status: 'failed', error: `${code}${msg}` }
+ }
+ return { status: status || 'processing' }
+ }
+
+ extractVideoUrl(result: any): string | null {
+ const task = result.task && typeof result.task === 'object' ? result.task : result
+ return task.content?.url || task.video_url || null
+ }
+
+ private normalizeDuration(duration?: number | null): number {
+ const parsed = Math.round(Number(duration || 5))
+ if (!Number.isFinite(parsed)) return 5
+ // MiniMax H3 支持 4-15 秒
+ return Math.min(15, Math.max(4, parsed))
+ }
+
+ /** MiniMax 仅 768P / 2K 两档;项目侧 480p/720p 统一归到 768P,1080p/2K 归到 2K */
+ private normalizeResolution(resolution?: string | null): string {
+ const r = (resolution || '').toLowerCase()
+ if (r === '2k' || r === '1080p') return '2K'
+ return '768P'
+ }
+}
diff --git a/backend/src/services/adapters/registry.ts b/backend/src/services/adapters/registry.ts
index 62d0e5a..54c5722 100644
--- a/backend/src/services/adapters/registry.ts
+++ b/backend/src/services/adapters/registry.ts
@@ -6,6 +6,7 @@ import { OpenAIImageAdapter } from './openai-image'
import { GeminiImageAdapter } from './gemini-image'
import { VolcEngineImageAdapter } from './volcengine-image'
import { VolcEngineVideoAdapter } from './volcengine-video'
+import { MiniMaxVideoAdapter } from './minimax-video'
import type { ImageProviderAdapter, VideoProviderAdapter } from './types'
// 图片 Adapter 注册表
@@ -18,6 +19,7 @@ export const imageAdapters: Record<string, ImageProviderAdapter> = {
// 视频 Adapter 注册表
export const videoAdapters: Record<string, VideoProviderAdapter> = {
volcengine: new VolcEngineVideoAdapter(),
+ minimax: new MiniMaxVideoAdapter(),
}
/**
diff --git a/backend/src/services/ai.ts b/backend/src/services/ai.ts
index 6c01d4c..4030648 100644
--- a/backend/src/services/ai.ts
+++ b/backend/src/services/ai.ts
@@ -18,7 +18,7 @@ export interface AIConfig {
export const officialProviders: Record<ServiceType, readonly string[]> = {
text: ['openai', 'gemini', 'volcengine'],
image: ['openai', 'gemini', 'volcengine'],
- video: ['volcengine'],
+ video: ['volcengine', 'minimax'],
}
export function isOfficialProvider(serviceType?: string | null, provider?: string | null): boolean {
diff --git a/backend/src/services/generation.ts b/backend/src/services/generation.ts
index 999e19f..af3d78a 100644
--- a/backend/src/services/generation.ts
+++ b/backend/src/services/generation.ts
@@ -116,7 +116,8 @@ export async function generateVideo(params: GenerateVideoParams): Promise<number
generateAudio: params.generateAudio === false ? 0 : 1,
duration: params.duration || 5,
aspectRatio: params.aspectRatio || '16:9',
- resolution: params.resolution === '480p' ? '480p' : '720p',
+ // 保留高分辨率档位透传(MiniMax 768P/2K),火山等适配器内部自行归并
+ resolution: ['480p', '720p', '1080p', '2K'].includes(params.resolution || '') ? params.resolution : '720p',
})
logTaskStart('VideoTask', 'enqueue', {