Skip to content

Commit 27ba994

Browse files
committed
update qs to python
1 parent 39305af commit 27ba994

File tree

1 file changed

+69
-42
lines changed

1 file changed

+69
-42
lines changed

articles/ai-services/openai/video-generation-quickstart.md

Lines changed: 69 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.date: 05/22/2025
1212

1313
# Quickstart: Generate a video with Sora (preview)
1414

15-
In this Quickstart, you generate video clips using the Azure OpenAI service. The example uses the Sora model, which is a video generation model that creates realistic and imaginative video scenes from text instructions. This guide shows how to create a video generation job, poll for its status, and retrieve the generated video.
15+
In this Quickstart, you generate video clips using the Azure OpenAI service. The example uses the Sora model, which is a video generation model that creates realistic and imaginative video scenes from text instructions. This guide shows you how to create a video generation job, poll for its status, and retrieve the generated video.
1616

1717

1818
## Prerequisites
@@ -21,6 +21,7 @@ In this Quickstart, you generate video clips using the Azure OpenAI service. The
2121
- <a href="https://www.python.org/" target="_blank">Python 3.8 or later version</a>.
2222
- An Azure OpenAI resource created in a supported region. See [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability).
2323
- Then, you need to deploy a `sora` model with your Azure resource. For more information, see [Create a resource and deploy a model with Azure OpenAI](./how-to/create-resource.md).
24+
- [Python 3.8 or later version](https://www.python.org/).
2425

2526

2627
## Setup
@@ -38,49 +39,75 @@ Go to your resource in the Azure portal. On the navigation pane, select **Keys a
3839

3940
:::image type="content" source="./media/quickstarts/endpoint.png" alt-text="Screenshot that shows the Keys and Endpoint page for an Azure OpenAI resource in the Azure portal." lightbox="../media/quickstarts/endpoint.png":::
4041

41-
[!INCLUDE [environment-variables](./includes/environment-variables.md)]
42-
43-
44-
45-
46-
## Create a video generation job
47-
48-
Send a POST request to create a new video generation job.
49-
50-
```bash
51-
curl -X POST "{endpoint}/openai/v1/video/generations/jobs?api-version=preview" ^
52-
-H "Content-Type: application/json" ^
53-
-H "Authorization: Bearer {Azure_OpenAI_Auth_Token}" ^
54-
-H "api-key: {Your-API-Key}" ^
55-
-d "{
56-
\"prompt\": \"A cat playing piano in a jazz bar.\",
57-
\"model\": \"sora\"
58-
}"
59-
```
6042

6143

44+
[!INCLUDE [environment-variables](./includes/environment-variables.md)]
6245

63-
## Poll for job status
64-
65-
Send a GET request with the `job-id` from the previous step to check the job status.
66-
67-
```bash
68-
curl -X GET "{endpoint}/openai/v1/video/generations/jobs/{job-id}?api-version=preview" ^
69-
-H "Authorization: Bearer {Azure_OpenAI_Auth_Token}" ^
70-
-H "api-key: {Your-API-Key}"
71-
```
72-
73-
Repeat this step until the status is `succeeded`. Then you can retrieve the generated video ID from the `"generations"` field.
74-
75-
## Retrieve the generated video
76-
77-
Once the job status is `succeeded`, use the generation ID from the job result to get the generated video.
78-
79-
```bash
80-
curl -X GET "{endpoint}/openai/v1/video/generations/{generation-id}?api-version=preview" ^
81-
-H "Authorization: Bearer {Azure_OpenAI_Auth_Token}" ^
82-
-H "api-key: {Your-API-Key}"
83-
```
8446

85-
The response contains the download URL for your generated video.
8647

48+
## Create a new Python application
49+
50+
Create a new Python file named `quickstart.py`. Open the new file in your preferred editor or IDE.
51+
1. Replace the contents of `quickstart.py` with the following code. Change the value of `prompt` to your preferred text.
52+
53+
```python
54+
import os
55+
import time
56+
import requests
57+
58+
# Set these variables with your values
59+
endpoint = os.environ["AZURE_OPENAI_ENDPOINT"] # e.g., "https://docs-test-001.openai.azure.com"
60+
api_key = os.environ["AZURE_OPENAI_KEY"]
61+
access_token = os.environ.get("AZURE_OPENAI_TOKEN") # Optional: if using Azure AD auth
62+
63+
headers = {
64+
"Content-Type": "application/json",
65+
"api-key": api_key,
66+
}
67+
if access_token:
68+
headers["Authorization"] = f"Bearer {access_token}"
69+
70+
# 1. Create a video generation job
71+
create_url = f"{endpoint}/openai/v1/video/generations/jobs?api-version=preview"
72+
payload = {
73+
"prompt": "A cat playing piano in a jazz bar.",
74+
"model": "sora"
75+
}
76+
response = requests.post(create_url, headers=headers, json=payload)
77+
response.raise_for_status()
78+
job_id = response.json()["body"]["id"]
79+
print(f"Job created: {job_id}")
80+
81+
# 2. Poll for job status
82+
status_url = f"{endpoint}/openai/v1/video/generations/jobs/{job_id}?api-version=preview"
83+
while True:
84+
status_response = requests.get(status_url, headers=headers)
85+
status_response.raise_for_status()
86+
status = status_response.json()["body"]["status"]
87+
print(f"Job status: {status}")
88+
if status == "succeeded":
89+
generations = status_response.json()["body"].get("generations", [])
90+
if not generations:
91+
raise Exception("No generations found in job result.")
92+
generation_id = generations[0]["id"]
93+
break
94+
elif status in ("failed", "cancelled"):
95+
raise Exception(f"Job did not succeed. Status: {status}")
96+
time.sleep(5) # Wait before polling again
97+
98+
# 3. Retrieve the generated video
99+
get_video_url = f"{endpoint}/openai/v1/video/generations/{generation_id}?api-version=preview"
100+
video_response = requests.get(get_video_url, headers=headers)
101+
video_response.raise_for_status()
102+
download_url = video_response.json()["body"]["generations"]
103+
print(f"Download your video at: {download_url}")
104+
```
105+
1. Run the application with the `python` command:
106+
107+
```console
108+
python quickstart.py
109+
```
110+
111+
Wait a few moments to get the response.
112+
113+
---

0 commit comments

Comments
 (0)