@@ -92,9 +92,50 @@ You can generate a video with the Sora model by creating a video generation job,
92
92
credential = DefaultAzureCredential()
93
93
token = credential.get_token("https://cognitiveservices.azure.com/.default")
94
94
95
- api_version = ' 2025-01-01-preview'
96
- url = f"{endpoint}/openai/deployments/gpt-4o-mini-audio-preview/chat/completions?api-version={api_version}"
95
+ api_version = ' preview'
97
96
headers= { "Authorization": f"Bearer {token.token}", "Content-Type": "application/json" }
97
+
98
+ # 1. Create a video generation job
99
+ create_url = f"{endpoint}/openai/v1/video/generations/jobs?api-version={api_version}"
100
+ body = {
101
+ "prompt": "A cat playing piano in a jazz bar.",
102
+ "width": 480,
103
+ "height": 480,
104
+ "n_seconds": 5,
105
+ "model": "sora"
106
+ }
107
+ response = requests.post(create_url, headers=headers, json=body)
108
+ response.raise_for_status()
109
+ print("Full response JSON:", response.json())
110
+ job_id = response.json()["id"]
111
+ print(f"Job created: {job_id}")
112
+
113
+ # 2. Poll for job status
114
+ status_url = f"{endpoint}/openai/v1/video/generations/jobs/{job_id}?api-version={api_version}"
115
+ status=None
116
+ while status not in ("succeeded", "failed", "cancelled"):
117
+ time.sleep(5) # Wait before polling again
118
+ status_response = requests.get(status_url, headers=headers).json()
119
+ status = status_response.get("status")
120
+ print(f"Job status: {status}")
121
+
122
+ # 3. Retrieve generated video
123
+ if status == "succeeded":
124
+ generations = status_response.get("generations", [])
125
+ if generations:
126
+ print(f"✅ Video generation succeeded.")
127
+ generation_id = generations[0].get("id")
128
+ video_url = f"{endpoint}/openai/v1/video/generations/{generation_id}/content/video?api-version={api_version}"
129
+ video_response = requests.get(video_url, headers=headers)
130
+ if video_response.ok:
131
+ output_filename = "output.mp4"
132
+ with open(output_filename, "wb") as file:
133
+ file.write(video_response.content)
134
+ print(f' Generated video saved as " {output_filename}" ' )
135
+ else:
136
+ raise Exception("No generations found in job result.")
137
+ else:
138
+ raise Exception(f"Job didn' t succeed. Status: {status}" )
98
139
` ` `
99
140
100
141
1. Run the Python file.
@@ -116,9 +157,50 @@ You can generate a video with the Sora model by creating a video generation job,
116
157
endpoint = os.environ['AZURE_OPENAI_ENDPOINT']
117
158
api_key = os.environ['AZURE_OPENAI_API_KEY']
118
159
119
- api_version = ' 2025-01-01-preview'
120
- url = f"{endpoint}/openai/deployments/gpt-4o-mini-audio-preview/chat/completions?api-version={api_version}"
160
+ api_version = 'preview'
121
161
headers= { " api-key" : api_key, " Content-Type" : " application/json" }
162
+
163
+ # 1. Create a video generation job
164
+ create_url = f" {endpoint}/openai/v1/video/generations/jobs? api-version={api_version}"
165
+ body = {
166
+ " prompt" : " A cat playing piano in a jazz bar." ,
167
+ " width" : 480,
168
+ " height" : 480,
169
+ " n_seconds" : 5,
170
+ " model" : " sora"
171
+ }
172
+ response = requests.post(create_url, headers=headers, json=body)
173
+ response.raise_for_status()
174
+ print(" Full response JSON:" , response.json())
175
+ job_id = response.json()[" id" ]
176
+ print(f" Job created: {job_id}" )
177
+
178
+ # 2. Poll for job status
179
+ status_url = f" {endpoint}/openai/v1/video/generations/jobs/{job_id}? api-version={api_version}"
180
+ status=None
181
+ while status not in (" succeeded" , " failed" , " cancelled" ):
182
+ time.sleep(5) # Wait before polling again
183
+ status_response = requests.get(status_url, headers=headers).json()
184
+ status = status_response.get(" status" )
185
+ print(f" Job status: {status}" )
186
+
187
+ # 3. Retrieve generated video
188
+ if status == " succeeded" :
189
+ generations = status_response.get(" generations" , [])
190
+ if generations:
191
+ print(f" ✅ Video generation succeeded." )
192
+ generation_id = generations[0].get(" id" )
193
+ video_url = f" {endpoint}/openai/v1/video/generations/{generation_id}/content/video? api-version={api_version}"
194
+ video_response = requests.get(video_url, headers=headers)
195
+ if video_response.ok:
196
+ output_filename = " output.mp4"
197
+ with open(output_filename, " wb" ) as file:
198
+ file.write(video_response.content)
199
+ print(f'Generated video saved as " {output_filename}" ')
200
+ else:
201
+ raise Exception(" No generations found in job result." )
202
+ else:
203
+ raise Exception(f" Job didn' t succeed. Status: {status}")
122
204
```
123
205
124
206
1. Run the Python file.
@@ -133,4 +215,37 @@ Wait a few moments to get the response.
133
215
134
216
### Output
135
217
218
+ The output will show the full response JSON from the video generation job creation request, including the job ID and status.
219
+
220
+ ```json
221
+ ```json
222
+ {
223
+ "object": "video.generation.job",
224
+ "id": "task_01jwcet0eje35tc5jy54yjax5q",
225
+ "status": "queued",
226
+ "created_at": 1748469875,
227
+ "finished_at": null,
228
+ "expires_at": null,
229
+ "generations": [],
230
+ "prompt": "A cat playing piano in a jazz bar.",
231
+ "model": "sora",
232
+ "n_variants": 1,
233
+ "n_seconds": 5,
234
+ "height": 480,
235
+ "width": 480,
236
+ "failure_reason": null
237
+ }
238
+ ```
239
+
240
+ The generated video will be saved as `output.mp4` in the current directory.
241
+
242
+ ```text
243
+ Job created: task_01jwcet0eje35tc5jy54yjax5q
244
+ Job status: preprocessing
245
+ Job status: running
246
+ Job status: processing
247
+ Job status: succeeded
248
+ ✅ Video generation succeeded.
249
+ Generated video saved as "output.mp4"
250
+ ```
136
251
0 commit comments