#hey ai, give me ai video code
1 messages · Page 1 of 1 (latest)
Certainly! Here’s a basic example of how to generate an AI video using OpenAI’s Sora API in Python. This code will create a video from a text prompt using the sora-2 model. You’ll need your OpenAI API key and the openai Python package installed.
from openai import OpenAI
openai = OpenAI()
video = openai.videos.create(
model="sora-2",
prompt="A video of a cool cat on a motorcycle in the night",
)
print("Video generation started:", video)
progress = getattr(video, "progress", 0)
bar_length = 30
while video.status in ("in_progress", "queued"):
# Refresh status
video = openai.videos.retrieve(video.id)
progress = getattr(video, "progress", 0)
filled_length = int((progress / 100) * bar_length)
bar = "=" * filled_length + "-" * (bar_length - filled_length)
status_text = "Queued" if video.status == "queued" else "Processing"
print(f"{status_text}: [{bar}] {progress:.1f}%")
import time; time.sleep(2)
if video.status == "failed":
message = getattr(
getattr(video, "error", None), "message", "Video generation failed"
)
print(message)
else:
print("Video generation completed:", video)
print("Downloading video content...")
content = openai.videos.download_content(video.id, variant="video")
content.write_to_file("video.mp4")
print("Wrote video.mp4")
This script:
- Starts a video generation job with a prompt.
- Polls the job status until it’s complete.
- Downloads the resulting MP4 file when finished.
You can adjust the prompt and model as needed. For more advanced options (like setting resolution or duration), see the OpenAI video generation documentation.
Note: You must have access to the Sora API and a valid API key for this to work.
Source: OpenAI Video Generation Guide
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@crystal imp Kapa.ai is still learning and improving, please let me know how I did by reacting below.