Creating/downloading videos from Azure OpenAI Sora 2 by PowerShell/API


Short blogpost today with an updated PowerShell script to generate Sora 2 videos with the Azure AI Foundry API. You need to apply for access here to get access to Sora 2. When you have access you can follow the guide here I wrote for deploying a Sora 1 resource, its the same for Sora 2.

$ResourceName = "resourcename"
$apikey = "enter api key"
$headers = @{
"api-key" = "$apikey"
"Content-Type" = "application/json"
}
# 1. Create a video generation job
# Seconds: 4, 8 and 12
# Videoresolutions to choose from: 720x1280 and 1280x720
$create_url = "https://$ResourceName.openai.azure.com/openai/v1/videos"
$body = @"
{
"model":"sora-2",
"prompt":"A car driving through a futuristic cityscape at sunset",
"seconds":"12",
"size":"1280x720"}
"@
$response = Invoke-RestMethod -Uri $create_url -Method Post -Headers $headers -Body $body -ContentType "application/json"
# 2. Poll for job status
$status_url = "https://$ResourceName.openai.azure.com/openai/v1/videos/$($response.id)"
$status_reponse = Invoke-RestMethod -Uri $status_url -Method Get -Headers $headers -ContentType "application/json"
while ($status_reponse.status -ne "completed" -and $status_reponse.status -ne "failed" -and $status_reponse.status -ne "cancelled") {
Start-Sleep -Seconds 5 # Wait before polling again
$status_reponse = Invoke-RestMethod -Uri $status_url -Method Get -Headers $headers -ContentType "application/json"
Write-Host "Job status: $($status_reponse.status)"
}
# 3. Download the generated video
$video_url = "
https://$ResourceName.openai.azure.com/openai/v1/videos/$($response.id)/content?variant=video&api-key=$apikey"
Invoke-RestMethod -Uri $video_url -Method Get -Headers $headers -OutFile "output.mp4"
view raw sora2.ps1 hosted with ❤ by GitHub

Leave a comment