|
| 1 | +# video-o1-reference-to-video |
| 2 | + |
| 3 | +{% columns %} |
| 4 | +{% column width="66.66666666666666%" %} |
| 5 | +{% hint style="info" %} |
| 6 | +This documentation is valid for the following list of our models: |
| 7 | + |
| 8 | +* `klingai/video-o1-reference-to-video` |
| 9 | +{% endhint %} |
| 10 | +{% endcolumn %} |
| 11 | + |
| 12 | +{% column width="33.33333333333334%" %} |
| 13 | +<a href="https://aimlapi.com/app/klingai/video-o1-reference-to-video" class="button primary">Try in Playground</a> |
| 14 | +{% endcolumn %} |
| 15 | +{% endcolumns %} |
| 16 | + |
| 17 | +A variant of Kling’s O1 omni-model that takes several reference images along with an instructional prompt as input. |
| 18 | + |
| 19 | +## Setup your API Key |
| 20 | + |
| 21 | +If you don’t have an API key for the AI/ML API yet, feel free to use our [Quickstart guide](https://docs.aimlapi.com/quickstart/setting-up). |
| 22 | + |
| 23 | +## API Schemas |
| 24 | + |
| 25 | +Generating a video using this model involves sequentially calling two endpoints: |
| 26 | + |
| 27 | +* The first one is for creating and sending a video generation task to the server (returns a generation ID). |
| 28 | +* The second one is for requesting the generated video from the server using the generation ID received from the first endpoint. |
| 29 | + |
| 30 | +Below, you can find two corresponding API schemas and an example with both endpoint calls. |
| 31 | + |
| 32 | +### Create a video generation task and send it to the server |
| 33 | + |
| 34 | +{% openapi-operation spec="video-o1-reference-to-video" path="/v2/video/generations" method="post" %} |
| 35 | +[OpenAPI video-o1-reference-to-video](https://raw.githubusercontent.com/aimlapi/api-docs/refs/heads/main/docs/api-references/video-models/Kling-AI/video-o1-reference-to-video.json) |
| 36 | +{% endopenapi-operation %} |
| 37 | + |
| 38 | +### Retrieve the generated video from the server |
| 39 | + |
| 40 | +After sending a request for video generation, this task is added to the queue. This endpoint lets you check the status of a video generation task using its `generation_id`, obtained from the endpoint described above.\ |
| 41 | +If the video generation task status is `complete`, the response will include the final result — with the generated video URL and additional metadata. |
| 42 | + |
| 43 | +{% openapi-operation spec="universal-video-endpoint-fetch" path="/v2/video/generations" method="get" %} |
| 44 | +[OpenAPI universal-video-endpoint-fetch](https://raw.githubusercontent.com/aimlapi/api-docs/refs/heads/main/docs/api-references/video-models/ByteDance/omnihuman-pair.json) |
| 45 | +{% endopenapi-operation %} |
| 46 | + |
| 47 | +## Code Example |
| 48 | + |
| 49 | +The code below creates a video generation task, then automatically polls the server every **15** seconds until it finally receives the video URL. |
| 50 | + |
| 51 | +{% tabs %} |
| 52 | +{% tab title="Python" %} |
| 53 | +{% code overflow="wrap" %} |
| 54 | +```python |
| 55 | +import requests |
| 56 | +import time |
| 57 | + |
| 58 | +# Insert your AIML API Key instead of <YOUR_AIMLAPI_KEY>: |
| 59 | +api_key = "<YOUR_AIMLAPI_KEY>" |
| 60 | +base_url = "https://api.aimlapi.com/v2" |
| 61 | + |
| 62 | +# Creating and sending a video generation task to the server |
| 63 | +def generate_video(): |
| 64 | + url = f"{base_url}/video/generations" |
| 65 | + headers = { |
| 66 | + "Authorization": f"Bearer {api_key}", |
| 67 | + } |
| 68 | + |
| 69 | + data = { |
| 70 | + "model": "klingai/video-o1-reference-to-video", |
| 71 | + "prompt": "A graceful ballerina dancing outside a circus tent on green grass, with colorful wildflowers swaying around her as she twirls and poses in the meadow.", |
| 72 | + "image_list": [ |
| 73 | + "https://storage.googleapis.com/falserverless/example_inputs/veo31-r2v-input-1.png", |
| 74 | + "https://storage.googleapis.com/falserverless/example_inputs/veo31-r2v-input-2.png", |
| 75 | + "https://storage.googleapis.com/falserverless/example_inputs/veo31-r2v-input-3.png" |
| 76 | + ], |
| 77 | + "duration": "5", |
| 78 | + } |
| 79 | + |
| 80 | + response = requests.post(url, json=data, headers=headers) |
| 81 | + |
| 82 | + if response.status_code >= 400: |
| 83 | + print(f"Error: {response.status_code} - {response.text}") |
| 84 | + else: |
| 85 | + response_data = response.json() |
| 86 | + return response_data |
| 87 | + |
| 88 | +# Requesting the result of the task from the server using the generation_id |
| 89 | +def get_video(gen_id): |
| 90 | + url = f"{base_url}/video/generations" |
| 91 | + params = { |
| 92 | + "generation_id": gen_id, |
| 93 | + } |
| 94 | + |
| 95 | + headers = { |
| 96 | + "Authorization": f"Bearer {api_key}", |
| 97 | + "Content-Type": "application/json" |
| 98 | + } |
| 99 | + |
| 100 | + response = requests.get(url, params=params, headers=headers) |
| 101 | + return response.json() |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + # Running video generation and getting a task id |
| 106 | + gen_response = generate_video() |
| 107 | + gen_id = gen_response.get("id") |
| 108 | + print("Generation ID: ", gen_id) |
| 109 | + |
| 110 | + # Try to retrieve the video from the server every 15 sec |
| 111 | + if gen_id: |
| 112 | + start_time = time.time() |
| 113 | + |
| 114 | + timeout = 1000 |
| 115 | + while time.time() - start_time < timeout: |
| 116 | + response_data = get_video(gen_id) |
| 117 | + |
| 118 | + if response_data is None: |
| 119 | + print("Error: No response from API") |
| 120 | + break |
| 121 | + |
| 122 | + status = response_data.get("status") |
| 123 | + |
| 124 | + if status in ["waiting", "queued", "generating"]: |
| 125 | + print(f"Status: {status}. Checking again in 15 seconds.") |
| 126 | + time.sleep(15) |
| 127 | + else: |
| 128 | + print("Processing complete:\n", response_data) |
| 129 | + return response_data |
| 130 | + |
| 131 | + print("Timeout reached. Stopping.") |
| 132 | + return None |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + main() |
| 137 | +``` |
| 138 | +{% endcode %} |
| 139 | +{% endtab %} |
| 140 | + |
| 141 | +{% tab title="JavaScript" %} |
| 142 | +{% code overflow="wrap" %} |
| 143 | +```javascript |
| 144 | +const https = require("https"); |
| 145 | +const { URL } = require("url"); |
| 146 | + |
| 147 | +// Replace <YOUR_AIMLAPI_KEY> with your actual AI/ML API key |
| 148 | +const apiKey = "<YOUR_AIMLAPI_KEY>"; |
| 149 | +const baseUrl = "https://api.aimlapi.com/v2"; |
| 150 | + |
| 151 | +// Creating and sending a video generation task to the server |
| 152 | +function generateVideo(callback) { |
| 153 | + const data = JSON.stringify({ |
| 154 | + model: "klingai/video-o1-reference-to-video", |
| 155 | + prompt: "A graceful ballerina dancing outside a circus tent on green grass, with colorful wildflowers swaying around her as she twirls and poses in the meadow.", |
| 156 | + image_list: [ |
| 157 | + "https://storage.googleapis.com/falserverless/example_inputs/veo31-r2v-input-1.png", |
| 158 | + "https://storage.googleapis.com/falserverless/example_inputs/veo31-r2v-input-2.png", |
| 159 | + "https://storage.googleapis.com/falserverless/example_inputs/veo31-r2v-input-3.png" |
| 160 | + ], |
| 161 | + duration: "5", |
| 162 | + |
| 163 | + }); |
| 164 | + |
| 165 | + const url = new URL(`${baseUrl}/video/generations`); |
| 166 | + const options = { |
| 167 | + method: "POST", |
| 168 | + headers: { |
| 169 | + "Authorization": `Bearer ${apiKey}`, |
| 170 | + "Content-Type": "application/json", |
| 171 | + "Content-Length": Buffer.byteLength(data), |
| 172 | + }, |
| 173 | + }; |
| 174 | + |
| 175 | + const req = https.request(url, options, (res) => { |
| 176 | + let body = ""; |
| 177 | + res.on("data", (chunk) => body += chunk); |
| 178 | + res.on("end", () => { |
| 179 | + if (res.statusCode >= 400) { |
| 180 | + console.error(`Error: ${res.statusCode} - ${body}`); |
| 181 | + callback(null); |
| 182 | + } else { |
| 183 | + const parsed = JSON.parse(body); |
| 184 | + callback(parsed); |
| 185 | + } |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + req.on("error", (err) => console.error("Request error:", err)); |
| 190 | + req.write(data); |
| 191 | + req.end(); |
| 192 | +} |
| 193 | + |
| 194 | +// Requesting the result of the task from the server using the generation_id |
| 195 | +function getVideo(genId, callback) { |
| 196 | + const url = new URL(`${baseUrl}/video/generations`); |
| 197 | + url.searchParams.append("generation_id", genId); |
| 198 | + |
| 199 | + const options = { |
| 200 | + method: "GET", |
| 201 | + headers: { |
| 202 | + "Authorization": `Bearer ${apiKey}`, |
| 203 | + "Content-Type": "application/json", |
| 204 | + }, |
| 205 | + }; |
| 206 | + |
| 207 | + const req = https.request(url, options, (res) => { |
| 208 | + let body = ""; |
| 209 | + res.on("data", (chunk) => body += chunk); |
| 210 | + res.on("end", () => { |
| 211 | + const parsed = JSON.parse(body); |
| 212 | + callback(parsed); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + req.on("error", (err) => console.error("Request error:", err)); |
| 217 | + req.end(); |
| 218 | +} |
| 219 | + |
| 220 | +// Initiates video generation and checks the status every 15 seconds until completion or timeout |
| 221 | +function main() { |
| 222 | + generateVideo((genResponse) => { |
| 223 | + if (!genResponse || !genResponse.id) { |
| 224 | + console.error("No generation ID received."); |
| 225 | + return; |
| 226 | + } |
| 227 | + |
| 228 | + const genId = genResponse.id; |
| 229 | + console.log("Generation ID:", genId); |
| 230 | + |
| 231 | + const timeout = 1000 * 1000; // 1000 sec |
| 232 | + const interval = 15 * 1000; // 15 sec |
| 233 | + const startTime = Date.now(); |
| 234 | + |
| 235 | + const checkStatus = () => { |
| 236 | + if (Date.now() - startTime >= timeout) { |
| 237 | + console.log("Timeout reached. Stopping."); |
| 238 | + return; |
| 239 | + } |
| 240 | + |
| 241 | + getVideo(genId, (responseData) => { |
| 242 | + if (!responseData) { |
| 243 | + console.error("Error: No response from API"); |
| 244 | + return; |
| 245 | + } |
| 246 | + |
| 247 | + const status = responseData.status; |
| 248 | + |
| 249 | + if (["waiting", "queued", "generating"].includes(status)) { |
| 250 | + console.log(`Status: ${status}. Checking again in 15 seconds.`); |
| 251 | + setTimeout(checkStatus, interval); |
| 252 | + } else { |
| 253 | + console.log("Processing complete:\n", responseData); |
| 254 | + } |
| 255 | + }); |
| 256 | + }; |
| 257 | + checkStatus(); |
| 258 | + }) |
| 259 | +} |
| 260 | + |
| 261 | +main(); |
| 262 | +``` |
| 263 | +{% endcode %} |
| 264 | +{% endtab %} |
| 265 | +{% endtabs %} |
| 266 | + |
| 267 | +<details> |
| 268 | + |
| 269 | +<summary>Response</summary> |
| 270 | + |
| 271 | +{% code overflow="wrap" %} |
| 272 | +```json5 |
| 273 | +Generation ID: a3f1e246-0831-4d6e-893a-990fb5c214ea:klingai/video-o1-reference-to-video |
| 274 | +Status: queued. Checking again in 15 seconds. |
| 275 | +Status: generating. Checking again in 15 seconds. |
| 276 | +Status: generating. Checking again in 15 seconds. |
| 277 | +Status: generating. Checking again in 15 seconds. |
| 278 | +Status: generating. Checking again in 15 seconds. |
| 279 | +Status: generating. Checking again in 15 seconds. |
| 280 | +Status: generating. Checking again in 15 seconds. |
| 281 | +Status: generating. Checking again in 15 seconds. |
| 282 | +Status: generating. Checking again in 15 seconds. |
| 283 | +Status: generating. Checking again in 15 seconds. |
| 284 | +Status: generating. Checking again in 15 seconds. |
| 285 | +Processing complete: |
| 286 | + {'id': 'a3f1e246-0831-4d6e-893a-990fb5c214ea:klingai/video-o1-reference-to-video', 'status': 'completed', 'video': {'url': 'https://cdn.aimlapi.com/flamingo/files/b/0a8787d0/ERB4UYY0-4b7THK5Uq49w_output.mp4'}} |
| 287 | +``` |
| 288 | +{% endcode %} |
| 289 | + |
| 290 | +</details> |
| 291 | + |
| 292 | +**Processing time**: \~ 2 min 6 sec. |
| 293 | + |
| 294 | +**Generated video** (1920x1080, without sound): |
| 295 | + |
| 296 | +{% embed url="https://drive.google.com/file/d/1kvuTVpZM9n6QI0JQeGUoMpFfsELKA2xR/view" %} |
0 commit comments