Video Generation
RavenSaaS uses ai-sdk as the base library, combined with custom Providers, to support mainstream video generation models on the market.
Using Kling AI Video Models
You need to first top up Video Generation API at Kling AI and create an API Key.
1. Set Environment Variables
1KLING_ACCESS_KEY = "xxx"
2KLING_SECRET_KEY = "xxx"
2. Generate Video
Note
This uses RavenSaaS custom generateVideo method and custom Provider: import '{ kling }' from "@/aisdk/kling";, which has a different import path from @ai-sdk Providers.
Parameters supported by providerOptions.kling, refer to Kling Text to Video API documentation
1import { generateVideo } from "@/aisdk";
2import { kling } from "@/aisdk/kling";
3
4const prompt = "a beautiful girl running with 2 cats";
5const model = "kling-v1-6";
6
7const videoModel = kling.video(model);
8const providerOptions = {
9 kling: {
10 mode: "std",
11 duration: 5,
12 },
13};
14
15const { videos, warnings } = await generateVideo({
16 model: videoModel,
17 prompt: prompt,
18 n: 1,
19 providerOptions,
20});
Generation Time Information
The kling-v1-6 model takes about 4 minutes to generate a 5-second video. kling-v1 takes even longer.
The generateVideo method generates videos synchronously by default. You need to wait for video generation to complete before disconnecting from the server.
3. Preview Effect
Kling AI video generation effect example
Save Videos to Local Files
Videos generated through generateVideo are base64 encoded string arrays.
You can refer to the code below to save video content to local files.
1const { videos, warnings } = await generateVideo({
2 model: videoModel,
3 prompt: prompt,
4 n: 1,
5 providerOptions,
6});
7
8if (warnings.length > 0) {
9 console.log("gen videos warnings:", provider, warnings);
10 return respErr("gen videos failed");
11}
12
13const batch = getUuid();
14
15const processedImages = await Promise.all(
16 videos.map(async (video, index) => {
17 const fileName = `${provider}_video_${batch}_${index}.mp4`;
18 const filePath = path.join(process.cwd(), ".tmp", fileName);
19
20 const buffer = Buffer.from(video.base64, "base64");
21 await writeFile(filePath, buffer);
22
23 return {
24 provider,
25 fileName,
26 filePath,
27 };
28 })
29);