- Add Litterbox temporary image hosting for image URLs - Update backend to accept image_base64 parameter - Update TypeScript client and API route - Subject button now enabled for Meta AI (for image-to-video) - Button changes from 'Video' to 'Animate' when subject is set - Pink/purple gradient for image-to-video, blue/cyan for text-to-video
72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { MetaCrawlClient } from '@/lib/providers/meta-crawl-client';
|
|
|
|
/**
|
|
* POST /api/meta/video
|
|
*
|
|
* Generate a video from a text prompt (and optionally an image) using Meta AI.
|
|
* - Text-to-Video: Just provide prompt and cookies
|
|
* - Image-to-Video: Also provide imageBase64
|
|
* Video generation takes 30-60+ seconds, so this endpoint may take a while.
|
|
*/
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const { prompt, cookies: clientCookies, imageBase64 } = await req.json();
|
|
|
|
if (!prompt) {
|
|
return NextResponse.json({ error: "Prompt is required" }, { status: 400 });
|
|
}
|
|
|
|
// Get cookies from request body or cookie header
|
|
let cookieString = clientCookies || req.cookies.get('meta_cookies')?.value;
|
|
|
|
if (!cookieString) {
|
|
return NextResponse.json(
|
|
{ error: "Meta AI cookies not found. Please configure settings." },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
const mode = imageBase64 ? 'image-to-video' : 'text-to-video';
|
|
console.log(`[Meta Video API] Starting ${mode} for prompt: "${prompt.substring(0, 50)}..."`);
|
|
|
|
const client = new MetaCrawlClient();
|
|
|
|
// Check if crawl4ai service is available
|
|
const isHealthy = await client.healthCheck();
|
|
if (!isHealthy) {
|
|
return NextResponse.json(
|
|
{ error: "Meta AI video service is not available. Make sure crawl4ai is running." },
|
|
{ status: 503 }
|
|
);
|
|
}
|
|
|
|
// Generate video - this can take 30-60+ seconds
|
|
const result = await client.generateVideo(prompt, cookieString, imageBase64);
|
|
|
|
if (!result.success || result.videos.length === 0) {
|
|
throw new Error(result.error || "No videos generated");
|
|
}
|
|
|
|
console.log(`[Meta Video API] Successfully generated ${result.videos.length} video(s)`);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
videos: result.videos,
|
|
conversation_id: result.conversation_id
|
|
});
|
|
|
|
} catch (error: unknown) {
|
|
const err = error as Error;
|
|
console.error("[Meta Video API] Error:", err.message);
|
|
|
|
const msg = err.message || "";
|
|
const isAuthError = msg.includes("401") || msg.includes("403") ||
|
|
msg.includes("auth") || msg.includes("cookies") || msg.includes("expired");
|
|
|
|
return NextResponse.json(
|
|
{ error: err.message || "Video generation failed" },
|
|
{ status: isAuthError ? 401 : 500 }
|
|
);
|
|
}
|
|
}
|