|
| 1 | +# video-o1-video-to-video-edit |
| 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-video-to-video-edit` |
| 9 | +{% endhint %} |
| 10 | +{% endcolumn %} |
| 11 | + |
| 12 | +{% column width="33.33333333333334%" %} |
| 13 | +<a href="https://aimlapi.com/app/?model=klingai/video-o1-video-to-video-edit" class="button primary">Try in Playground</a> |
| 14 | +{% endcolumn %} |
| 15 | +{% endcolumns %} |
| 16 | + |
| 17 | +The model transforms an input video according to a natural-language text prompt, altering style, visual attributes, or the overall look of the scene while preserving the original motion and structural layout of the footage. |
| 18 | + |
| 19 | +## How to Make a Call |
| 20 | + |
| 21 | +<details> |
| 22 | + |
| 23 | +<summary>Step-by-Step Instructions</summary> |
| 24 | + |
| 25 | +:digit\_one: **Setup You Can’t Skip** |
| 26 | + |
| 27 | +:black\_small\_square: [**Create an Account**](https://aimlapi.com/app/sign-up): Visit the AI/ML API website and create an account (if you don’t have one yet). \ |
| 28 | +:black\_small\_square: [**Generate an API Key**](https://aimlapi.com/app/keys): After logging in, navigate to your account dashboard and generate your API key. Ensure the key is enabled on the UI. |
| 29 | + |
| 30 | +:digit\_two: **Copy the code example** |
| 31 | + |
| 32 | +At the bottom of this page, you'll find a code example that shows how to structure the request. Choose the code snippet in your preferred programming language and copy it into your development environment. |
| 33 | + |
| 34 | +:digit\_three: **Modify the code example** |
| 35 | + |
| 36 | +:black\_small\_square: Replace `<YOUR_AIMLAPI_KEY>` with your actual AI/ML API key. \ |
| 37 | +:black\_small\_square: Adjust the input field used by this model (for example, prompt, input text, instructions, media source, or other model-specific input) to match your request. |
| 38 | + |
| 39 | +:digit\_four: <sup><sub><mark style="background-color:yellow;">**(Optional)**<mark style="background-color:yellow;"><sub></sup> **Adjust other optional parameters if needed** |
| 40 | + |
| 41 | +Only the required parameters shown in the example are needed to run the request, but you can include optional parameters to fine-tune behavior. Below, you can find the corresponding **API schema**, which lists all available parameters and usage notes. |
| 42 | + |
| 43 | +:digit\_five: **Run your modified code** |
| 44 | + |
| 45 | +Run your modified code inside your development environment. Response time depends on many factors, but for simple requests it rarely exceeds a few seconds. |
| 46 | + |
| 47 | +{% hint style="success" %} |
| 48 | +If you need a more detailed walkthrough for setting up your development environment and making a request step-by-step, feel free to use our [**Quickstart guide.**](https://docs.aimlapi.com/quickstart/setting-up) |
| 49 | +{% endhint %} |
| 50 | + |
| 51 | +</details> |
| 52 | + |
| 53 | +## API Schemas |
| 54 | + |
| 55 | +Generating a video using this model involves sequentially calling two endpoints: |
| 56 | + |
| 57 | +* The first one is for creating and sending a video generation task to the server (returns a generation ID). |
| 58 | +* The second one is for requesting the generated video from the server using the generation ID received from the first endpoint. |
| 59 | + |
| 60 | +Below, you can find two corresponding API schemas and an example with both endpoint calls. |
| 61 | + |
| 62 | +### Create a video generation task and send it to the server |
| 63 | + |
| 64 | +{% openapi-operation spec="kling-video-o1-video-to-video-edit" path="/v2/video/generations" method="post" %} |
| 65 | +[OpenAPI kling-video-o1-video-to-video-edit](https://raw.githubusercontent.com/aimlapi/api-docs/refs/heads/main/docs/api-references/video-models/Kling-AI/video-o1-video-to-video-edit.json) |
| 66 | +{% endopenapi-operation %} |
| 67 | + |
| 68 | +### Retrieve the generated video from the server |
| 69 | + |
| 70 | +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.\ |
| 71 | +If the video generation task status is `complete`, the response will include the final result — with the generated video URL and additional metadata. |
| 72 | + |
| 73 | +{% openapi-operation spec="universal-video-endpoint-fetch" path="/v2/video/generations" method="get" %} |
| 74 | +[OpenAPI universal-video-endpoint-fetch](https://raw.githubusercontent.com/aimlapi/api-docs/refs/heads/main/docs/api-references/video-models/ByteDance/omnihuman-pair.json) |
| 75 | +{% endopenapi-operation %} |
| 76 | + |
| 77 | +## Code Example |
| 78 | + |
| 79 | +The code below creates a video generation task, then automatically polls the server every **15** seconds until it finally receives the video URL. |
| 80 | + |
| 81 | +{% tabs %} |
| 82 | +{% tab title="Python" %} |
| 83 | +{% code overflow="wrap" %} |
| 84 | +```python |
| 85 | +import requests |
| 86 | +import time |
| 87 | + |
| 88 | +# Insert your AIML API Key instead of <YOUR_AIMLAPI_KEY>: |
| 89 | +api_key = "<YOUR_AIMLAPI_KEY>" |
| 90 | +base_url = "https://api.aimlapi.com/v2" |
| 91 | + |
| 92 | +# Creating and sending a video generation task to the server |
| 93 | +def generate_video(): |
| 94 | + url = f"{base_url}/video/generations" |
| 95 | + headers = { |
| 96 | + "Authorization": f"Bearer {api_key}", |
| 97 | + } |
| 98 | + |
| 99 | + data = { |
| 100 | + "model": "klingai/video-o1-video-to-video-edit", |
| 101 | + "prompt":''' |
| 102 | + Add a small fairy as a rider on the raccoon’s back. She must have a black-and-golden face and a cloak in the colors of a dark emerald tropical butterfly with bright blue shimmering spots. |
| 103 | + ''', |
| 104 | + "video_url": "https://raw.githubusercontent.com/aimlapi/api-docs/main/reference-files/racoon-in-the-forest.mp4" |
| 105 | + } |
| 106 | + |
| 107 | + response = requests.post(url, json=data, headers=headers) |
| 108 | + |
| 109 | + if response.status_code >= 400: |
| 110 | + print(f"Error: {response.status_code} - {response.text}") |
| 111 | + else: |
| 112 | + response_data = response.json() |
| 113 | + return response_data |
| 114 | + |
| 115 | +# Requesting the result of the task from the server using the generation_id |
| 116 | +def get_video(gen_id): |
| 117 | + url = f"{base_url}/video/generations" |
| 118 | + params = { |
| 119 | + "generation_id": gen_id, |
| 120 | + } |
| 121 | + |
| 122 | + headers = { |
| 123 | + "Authorization": f"Bearer {api_key}", |
| 124 | + "Content-Type": "application/json" |
| 125 | + } |
| 126 | + |
| 127 | + response = requests.get(url, params=params, headers=headers) |
| 128 | + return response.json() |
| 129 | + |
| 130 | + |
| 131 | +def main(): |
| 132 | + # Running video generation and getting a task id |
| 133 | + gen_response = generate_video() |
| 134 | + gen_id = gen_response.get("id") |
| 135 | + print("Generation ID: ", gen_id) |
| 136 | + |
| 137 | + # Try to retrieve the video from the server every 15 sec |
| 138 | + if gen_id: |
| 139 | + start_time = time.time() |
| 140 | + |
| 141 | + timeout = 1000 |
| 142 | + while time.time() - start_time < timeout: |
| 143 | + response_data = get_video(gen_id) |
| 144 | + |
| 145 | + if response_data is None: |
| 146 | + print("Error: No response from API") |
| 147 | + break |
| 148 | + |
| 149 | + status = response_data.get("status") |
| 150 | + |
| 151 | + if status in ["waiting", "queued", "generating"]: |
| 152 | + print(f"Status: {status}. Checking again in 15 seconds.") |
| 153 | + time.sleep(15) |
| 154 | + else: |
| 155 | + print("Processing complete:\n", response_data) |
| 156 | + return response_data |
| 157 | + |
| 158 | + print("Timeout reached. Stopping.") |
| 159 | + return None |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + main() |
| 164 | +``` |
| 165 | +{% endcode %} |
| 166 | +{% endtab %} |
| 167 | + |
| 168 | +{% tab title="JavaScript" %} |
| 169 | +{% code overflow="wrap" %} |
| 170 | +```javascript |
| 171 | +const https = require("https"); |
| 172 | +const { URL } = require("url"); |
| 173 | + |
| 174 | +// Replace <YOUR_AIMLAPI_KEY> with your actual AI/ML API key |
| 175 | +const apiKey = "<YOUR_AIMLAPI_KEY>"; |
| 176 | +const baseUrl = "https://api.aimlapi.com/v2"; |
| 177 | + |
| 178 | +// Creating and sending a video generation task to the server |
| 179 | +function generateVideo(callback) { |
| 180 | + const data = JSON.stringify({ |
| 181 | + model: "klingai/video-o1-video-to-video-edit", |
| 182 | + prompt: ` |
| 183 | + Add a small fairy as a rider on the raccoon’s back. She must have a black-and-golden face and a cloak in the colors of a dark emerald tropical butterfly with bright blue shimmering spots.`, |
| 184 | + video_url: "https://raw.githubusercontent.com/aimlapi/api-docs/main/reference-files/racoon-in-the-forest.mp4", |
| 185 | + }); |
| 186 | + |
| 187 | + const url = new URL(`${baseUrl}/video/generations`); |
| 188 | + const options = { |
| 189 | + method: "POST", |
| 190 | + headers: { |
| 191 | + "Authorization": `Bearer ${apiKey}`, |
| 192 | + "Content-Type": "application/json", |
| 193 | + "Content-Length": Buffer.byteLength(data), |
| 194 | + }, |
| 195 | + }; |
| 196 | + |
| 197 | + const req = https.request(url, options, (res) => { |
| 198 | + let body = ""; |
| 199 | + res.on("data", (chunk) => body += chunk); |
| 200 | + res.on("end", () => { |
| 201 | + if (res.statusCode >= 400) { |
| 202 | + console.error(`Error: ${res.statusCode} - ${body}`); |
| 203 | + callback(null); |
| 204 | + } else { |
| 205 | + const parsed = JSON.parse(body); |
| 206 | + callback(parsed); |
| 207 | + } |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + req.on("error", (err) => console.error("Request error:", err)); |
| 212 | + req.write(data); |
| 213 | + req.end(); |
| 214 | +} |
| 215 | + |
| 216 | +// Requesting the result of the task from the server using the generation_id |
| 217 | +function getVideo(genId, callback) { |
| 218 | + const url = new URL(`${baseUrl}/video/generations`); |
| 219 | + url.searchParams.append("generation_id", genId); |
| 220 | + |
| 221 | + const options = { |
| 222 | + method: "GET", |
| 223 | + headers: { |
| 224 | + "Authorization": `Bearer ${apiKey}`, |
| 225 | + "Content-Type": "application/json", |
| 226 | + }, |
| 227 | + }; |
| 228 | + |
| 229 | + const req = https.request(url, options, (res) => { |
| 230 | + let body = ""; |
| 231 | + res.on("data", (chunk) => body += chunk); |
| 232 | + res.on("end", () => { |
| 233 | + const parsed = JSON.parse(body); |
| 234 | + callback(parsed); |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + req.on("error", (err) => console.error("Request error:", err)); |
| 239 | + req.end(); |
| 240 | +} |
| 241 | + |
| 242 | +// Initiates video generation and checks the status every 15 seconds until completion or timeout |
| 243 | +function main() { |
| 244 | + generateVideo((genResponse) => { |
| 245 | + if (!genResponse || !genResponse.id) { |
| 246 | + console.error("No generation ID received."); |
| 247 | + return; |
| 248 | + } |
| 249 | + |
| 250 | + const genId = genResponse.id; |
| 251 | + console.log("Generation ID:", genId); |
| 252 | + |
| 253 | + const timeout = 1000 * 1000; // 1000 sec |
| 254 | + const interval = 15 * 1000; // 15 sec |
| 255 | + const startTime = Date.now(); |
| 256 | + |
| 257 | + const checkStatus = () => { |
| 258 | + if (Date.now() - startTime >= timeout) { |
| 259 | + console.log("Timeout reached. Stopping."); |
| 260 | + return; |
| 261 | + } |
| 262 | + |
| 263 | + getVideo(genId, (responseData) => { |
| 264 | + if (!responseData) { |
| 265 | + console.error("Error: No response from API"); |
| 266 | + return; |
| 267 | + } |
| 268 | + |
| 269 | + const status = responseData.status; |
| 270 | + |
| 271 | + if (["waiting", "queued", "generating"].includes(status)) { |
| 272 | + console.log(`Status: ${status}. Checking again in 15 seconds.`); |
| 273 | + setTimeout(checkStatus, interval); |
| 274 | + } else { |
| 275 | + console.log("Processing complete:\n", responseData); |
| 276 | + } |
| 277 | + }); |
| 278 | + }; |
| 279 | + checkStatus(); |
| 280 | + }) |
| 281 | +} |
| 282 | + |
| 283 | +main(); |
| 284 | +``` |
| 285 | +{% endcode %} |
| 286 | +{% endtab %} |
| 287 | +{% endtabs %} |
| 288 | + |
| 289 | +<details> |
| 290 | + |
| 291 | +<summary>Response</summary> |
| 292 | + |
| 293 | +{% code overflow="wrap" %} |
| 294 | +```json5 |
| 295 | +Generation ID: 9177d714-9f53-4ce7-829f-b81e2223c48b:klingai/video-o1-video-to-video-edit |
| 296 | +Status: generating. Checking again in 15 seconds. |
| 297 | +Status: generating. Checking again in 15 seconds. |
| 298 | +Status: generating. Checking again in 15 seconds. |
| 299 | +Status: generating. Checking again in 15 seconds. |
| 300 | +Status: generating. Checking again in 15 seconds. |
| 301 | +Status: generating. Checking again in 15 seconds. |
| 302 | +Status: generating. Checking again in 15 seconds. |
| 303 | +Status: generating. Checking again in 15 seconds. |
| 304 | +Status: generating. Checking again in 15 seconds. |
| 305 | +Status: generating. Checking again in 15 seconds. |
| 306 | +Status: generating. Checking again in 15 seconds. |
| 307 | +Status: generating. Checking again in 15 seconds. |
| 308 | +Status: generating. Checking again in 15 seconds. |
| 309 | +Status: generating. Checking again in 15 seconds. |
| 310 | +Status: generating. Checking again in 15 seconds. |
| 311 | +Processing complete: |
| 312 | + {'id': '9177d714-9f53-4ce7-829f-b81e2223c48b:klingai/video-o1-video-to-video-edit', 'status': 'completed', 'video': {'url': 'https://cdn.aimlapi.com/flamingo/files/b/0a875051/Uce7GGCPuWbicbRrdaI4U_output.mp4'}} |
| 313 | +``` |
| 314 | +{% endcode %} |
| 315 | + |
| 316 | +</details> |
| 317 | + |
| 318 | +**Processing time**: \~ 3 min 55 sec. |
| 319 | + |
| 320 | +**Generated video** (1940x1068, without sound): |
| 321 | + |
| 322 | +{% embed url="https://drive.google.com/file/d/19HfgD3-h-qtKIHNfjHPrn8jxn-8Yl8CY/view" %} |
0 commit comments