|
| 1 | +--- |
| 2 | +title: 'Quickstart: Generate video with Sora' |
| 3 | +titleSuffix: Azure OpenAI |
| 4 | +description: Learn how to get started generating video clips with Azure OpenAI. |
| 5 | +manager: nitinme |
| 6 | +ms.service: azure-ai-openai |
| 7 | +ms.topic: quickstart |
| 8 | +author: PatrickFarley |
| 9 | +ms.author: pafarley |
| 10 | +ms.date: 05/22/2025 |
| 11 | +--- |
| 12 | + |
| 13 | +# Quickstart: Generate a video with Sora (preview) |
| 14 | + |
| 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. |
| 16 | + |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An Azure subscription. <a href="https://azure.microsoft.com/free/ai-services" target="_blank">Create one for free</a>. |
| 21 | +- <a href="https://www.python.org/" target="_blank">Python 3.8 or later version</a>. |
| 22 | +- An Azure OpenAI resource created in a supported region. See [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability). |
| 23 | +- 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/). |
| 25 | + |
| 26 | + |
| 27 | +## Setup |
| 28 | + |
| 29 | +### Retrieve key and endpoint |
| 30 | + |
| 31 | +To successfully call the Azure OpenAI APIs, you need the following information about your Azure OpenAI resource: |
| 32 | + |
| 33 | +| Variable | Name | Value | |
| 34 | +|---|---|---| |
| 35 | +| **Endpoint** | `api_base` | The endpoint value is located under **Keys and Endpoint** for your resource in the Azure portal. You can also find the endpoint via the **Deployments** page in Azure AI Foundry portal. An example endpoint is: `https://docs-test-001.openai.azure.com/`. | |
| 36 | +| **Key** | `api_key` | The key value is also located under **Keys and Endpoint** for your resource in the Azure portal. Azure generates two keys for your resource. You can use either value. | |
| 37 | + |
| 38 | +Go to your resource in the Azure portal. On the navigation pane, select **Keys and Endpoint** under **Resource Management**. Copy the **Endpoint** value and an access key value. You can use either the **KEY 1** or **KEY 2** value. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption. |
| 39 | + |
| 40 | +:::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"::: |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +[!INCLUDE [environment-variables](./includes/environment-variables.md)] |
| 45 | + |
| 46 | + |
| 47 | + |
| 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