AI IntegrationsVideo Generation

Video Generation

ShipAny uses ai-sdk as its foundation library, combined with custom Providers to support mainstream video generation models available in the market.

Using Kling AI’s Video Model

First, you need to top up your Video Generation API credit on Kling AI and create an API Key.

  1. Set up environment variables
.env.development
KLING_ACCESS_KEY = "xxx"
KLING_SECRET_KEY = "xxx"
  1. Generate video

Note: Here we use ShipAny’s custom generateVideo method and custom Provider: import { kling } from "@/aisdk/kling";, which has a different import path from the providers under @ai-sdk.

For parameters supported by providerOptions.kling, refer to Kling Text to Video API documentation

app/api/demo/gen-video/route.ts
import { generateVideo } from "@/aisdk";
import { kling } from "@/aisdk/kling";
 
const prompt = "a beautiful girl running with 2 cats";
const model = "kling-v1-6";
 
const videoModel = kling.video(model);
const providerOptions = {
  kling: {
    mode: "std",
    duration: 5,
  },
};
 
const { videos, warnings } = await generateVideo({
  model: videoModel,
  prompt: prompt,
  n: 1,
  providerOptions,
});

Generating a 5s video with the kling-v1-6 model takes approximately 4 minutes. The kling-v1 model takes even longer.

The generateVideo method generates video synchronously by default, meaning you need to wait for the video generation to complete before disconnecting from the server.

  1. Preview

Saving Videos to Local Files

Videos generated through generateVideo are returned as an array of base64 encoded strings.

You can refer to the code below to save the video content to local files.

app/api/demo/gen-video/route.ts
const { videos, warnings } = await generateVideo({
  model: videoModel,
  prompt: prompt,
  n: 1,
  providerOptions,
});
 
if (warnings.length > 0) {
  console.log("gen videos warnings:", provider, warnings);
  return respErr("gen videos failed");
}
 
const batch = getUuid();
 
const processedImages = await Promise.all(
  videos.map(async (video, index) => {
    const fileName = `${provider}_video_${batch}_${index}.mp4`;
    const filePath = path.join(process.cwd(), ".tmp", fileName);
 
    const buffer = Buffer.from(video.base64, "base64");
    await writeFile(filePath, buffer);
 
    return {
      provider,
      fileName,
      filePath,
    };
  })
);

References