import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# Access API key securely
api_key = os.getenv("OPENAI_API_KEY")
Sora API 的使用須遵守以下規定:
查看OpenAI 文檔以獲取最新信息,因為這些細節可能會隨著 API 從預覽版到普遍可用性而發展。
為了有效地與 Sora API 交互,您需要:
# Install required packages
pip install openai requests python-dotenv
# Basic imports for working with the API
import openai
import json
import time
使用 Sora API 需要了解其請求結構、參數和響應格式。
所有對 Sora API 的請求都需要使用您的 API 密鑰進行身份驗證:
# Configure OpenAI with your API key
openai.api_key = os.getenv("OPENAI_API_KEY")
# Basic request to generate a video
????????????????response = openai.Sora.create(
????????????????????prompt=prompt,
????????????????????duration_seconds=duration
????????????????)
????????????????results.append({
????????????????????"variation": i+1,
????????????????????"prompt": prompt,
????????????????????"success": True,
????????????????????"url": response.data[0].url
????????????????})
????????????except Exception as e:
????????????????results.append({
????????????????????"variation": i+1,
????????????????????"prompt": prompt,
????????????????????"success": False,
????????????????????"error": str(e)
????????????????})
????????????time.sleep(2)
# Prevent rate limiting# Analyze results to identify patterns
????successful = [r for r in results if r["success"]]
????failed = [r for r in results if not r["success"]]????if len(successful) > 0:
????????print("Successful variations found. Review them to understand what works.")
????????return successful
????else:
????????print("All variations failed. Consider more significant prompt restructuring.")
????????return failed
實施系統的評估方法有助于不斷改進您的 Sora API 實現。
有用的評估指標包括:
為了進行系統評估,請考慮實施評分系統:
def evaluate_generation(prompt, video_url, criteria=None):
????"""Basic evaluation framework for generations"""
????if criteria is None:
????????criteria = {
????????????"visual_quality": "Rate the overall visual quality from 1-10",
????????????"prompt_adherence": "Rate how well the video matches the prompt from 1-10",
????????????"consistency": "Rate the physical and temporal consistency from 1-10",
????????????"narrative": "Rate the narrative coherence from 1-10"
????????}
????print(f"Evaluating video generated from prompt: {prompt[:50]}...")
????print(f"Video URL: {video_url}")????results = {}
????for criterion, description in criteria.items():
????????score = input(f"{description}: ")
????????results[criterion] = int(score)# Calculate overall score
????overall = sum(results.values()) / len(results)
????results["overall"] = overall????print(f"Overall score: {overall:.1f}/10")
????return results
有效的反饋方法包括:
在您的應用程序中實現一個簡單的反饋系統:
def collect_user_feedback(video_id, user_id):
????"""Collect and store user feedback on generated videos"""
????questions = [
????????{"id": "quality", "text": "How would you rate the visual quality?", "type": "scale", "range": [1, 5]},
????????{"id": "realism", "text": "How realistic did the video appear?", "type": "scale", "range": [1, 5]},
????????{"id": "usefulness", "text": "How useful was this video for your needs?", "type": "scale", "range": [1, 5]},
????????{"id": "improvements", "text": "What could be improved about this video?", "type": "text"}
????]
# In a real application, this would render a form and collect responses# For this example, we'll simulate responses????responses = {
????????"video_id": video_id,
????????"user_id": user_id,
????????"timestamp": time.time(),
????????"ratings": {
????????????"quality": 4,
????????????"realism": 3,
????????????"usefulness": 4
????????},
????????"comments": "The lighting was great but motion could be smoother."
????}# In a real application, store this in a database
????store_feedback(responses)# Analyze feedback trends
????analyze_feedback_trends(video_id)????return responses
為了不斷提高您的成果:
實施持續改進流程:
????best_result = max(results, key=lambda x: x["score"])
????print(f"Best result was iteration {best_result[‘iteration’]} with score {best_result[‘score’]}/10")
????return best_result
????for i in range(iterations):
????????print(f"Iteration {i+1} with prompt: {current_prompt[:50]}...")
# Generate video with current prompt
????????response = openai.Sora.create(
????????????prompt=current_prompt,
????????????duration_seconds=10
????????)
# Collect evaluation (in a real system, this could be user feedback)
????????evaluation = evaluate_generation(current_prompt, response.data[0].url)
????????results.append({
????????????"iteration": i+1,
????????????"prompt": current_prompt,
????????????"score": evaluation["overall"],
????????????"url": response.data[0].url
????????})
# If score is high enough, stop iterations
????????if evaluation["overall"] >= 8:
????????????print("Reached satisfactory quality. Stopping iterations.")
????????????break
# Use feedback to improve the prompt
????????if evaluation["prompt_adherence"] < 7:
????????????current_prompt = add_specificity(current_prompt)
????????if evaluation["consistency"] < 7:
????????????current_prompt = enhance_physical_descriptions(current_prompt)
????????if evaluation["narrative"] < 7:
????????????current_prompt = improve_narrative_flow(current_prompt)
????????print(f"Revised prompt: {current_prompt[:50]}...")
????????time.sleep(2)
# Prevent rate limiting
# Return the best result
????best_result = max(results, key=lambda x: x["score"])
????print(f"Best result was iteration {best_result['iteration']} with score {best_result['score']}/10")
????return best_result
隨著 Sora API 的發展,適應性設計將確保您的實施保持有效。
構建彈性實施方案:
版本感知的實現方法:
????????return params
????def _detect_api_version(self):
????????"""Detect the current [Sora API](http://www.dlbhg.com/blog/ua-how-to-call-soras-api) version"""
????????try:
# Make a minimal API call to check version
????????????metadata = openai.Sora.get_info()
????????????return metadata.version
????????except:
# Fall back to default version if detection fails
????????????return "v1"
????def generate_video(self, prompt, duration, **kwargs):
????????"""Version-aware video generation"""
????????if self._supports_feature("high_resolution") and kwargs.get("high_res"):
????????????resolution = "1080p"
????????else:
????????????resolution = "720p"
????????if self._supports_feature("extended_duration") and duration > 60:
# Handle with segmentation for older API versions
????????????return self._generate_segmented(prompt, duration, **kwargs)
# Standard generation with version-appropriate parameters
????????params = self._prepare_parameters(prompt, duration, **kwargs)
????????return openai.Sora.create(**params)
????def _supports_feature(self, feature_name):
????????"""Check if current API version supports a specific feature"""
????????feature_map = {
????????????"high_resolution": ["v1.2", "v2.0"],
????????????"extended_duration": ["v2.0"],
????????????"style_transfer": ["v1.5", "v2.0"]
????????}
????????if feature_name in feature_map:
????????????return self.api_version in feature_map[feature_name]
????????return False
????def _prepare_parameters(self, prompt, duration, **kwargs):
????????"""Prepare version-appropriate parameters"""
# Base parameters supported across versions
????????params = {
????????????"prompt": prompt,
????????????"duration_seconds": min(duration, 60)
# Enforce limits for older versions
????????}
# Add version-specific parameters
????????if self.api_version >= "v1.5" and "style" in kwargs:
????????????params["style_preset"] = kwargs["style"]
# Add other parameters based on version capability
????????return params
對于預期需求增加的應用程序:
可擴展隊列實現:
????return openai.Sora.create(
????????prompt=prompt,
????????duration_seconds=duration
????)
async def send_callback(url, data):
????"""Send callback to notify of completion"""
????async with aiohttp.ClientSession() as session:
????????await session.post(url, json=data)
# Limit concurrent processing
@app.post("/generate")
async def enqueue_generation(request: VideoRequest, background_tasks: BackgroundTasks):
# Add to queue
????await request_queue.put(request)
# Start processing in background if not already running
????background_tasks.add_task(process_queue)
????return {"status": "queued", "queue_position": request_queue.qsize()}
async def process_queue():
????while not request_queue.empty():
????????async with processing_semaphore:
????????????request = await request_queue.get()
????????????try:
# Generate video
????????????????response = await generate_video_async(request.prompt, request.duration)
# Notify via callback
????????????????await send_callback(request.callback_url, {
????????????????????"user_id": request.user_id,
????????????????????"status": "completed",
????????????????????"video_url": response.data[0].url
????????????????})
????????????except Exception as e:
# Handle failures
????????????????await send_callback(request.callback_url, {
????????????????????"user_id": request.user_id,
????????????????????"status": "failed",
????????????????????"error": str(e)
????????????????})
????????????finally:
????????????????request_queue.task_done()
async def generate_video_async(prompt, duration):
????"""Asynchronous video generation"""
# In a real implementation, use the OpenAI async client
????return openai.Sora.create(
????????prompt=prompt,
????????duration_seconds=duration
????)
async def send_callback(url, data):
????"""Send callback to notify of completion"""
????async with aiohttp.ClientSession() as session:
????????await session.post(url, json=data)
無論您是將 Sora 集成到應用程序的開發人員、希望擴展工具包的內容創建者,還是尋求轉變視覺內容制作的組織,本指南中涵蓋的原則和技術都為成功實施和優化 OpenAI Sora API 提供了路線圖。
prompt="A calm lake reflecting the sunrise, with mountains in the background and birds flying across the sky.",
duration_seconds=10
)
video_url = response.data[0].url
### Essential Parameters Explained
The Sora API accepts several key parameters that control the generation process:
- **prompt** (required): The text description of the video you want to generate. This is the most important parameter and should be detailed and specific.
- **duration_seconds**: Specifies the desired length of the video (typically 1-60 seconds).
- **output_format**: The file format for the generated video (e.g., "mp4", "webm").
- **resolution**: The dimensions of the output video (e.g., "1080p", "720p").
- **style_preset**: Optional parameter to influence the visual style (e.g., "cinematic", "animation", "documentary").
- **negative_prompt**: Descriptions of what you want to avoid in the generated video.
### Understanding Response Formats
The API returns a structured response containing:
```json
{
??"id": "gen-2xJ7LjGi8M5UgRq2XCTg8Zp2",
??"created": 1709548934,
??"status": "completed",
??"data": [
????{
??????"url": "https://cdn.openai.sora.generation/videos/gen-2xJ7LjGi8M5UgRq2XCTg8Zp2.mp4",
??????"metadata": {
????????"duration_ms": 10000,
????????"resolution": "1080p",
????????"format": "mp4"
??????}
????}
??]
}```
關鍵要素包括:
- id:生成請求的唯一標識符
- 狀態:生成的當前狀態(“處理中”、“完成”、“失敗”)
- data.url:下載生成視頻的URL
- 元數據:有關生成視頻的技術細節
### 6.2 錯誤處理最佳實踐
使用 Sora API 時,強大的錯誤處理至關重要:
try:
????response = openai.Sora.create(
????????prompt="A serene mountain landscape with flowing rivers and dense forests.",
????????duration_seconds=15
????)
????video_url = response.data[0].url
except openai.error.RateLimitError:
????print("Rate limit exceeded. Implementing exponential backoff…")
????time.sleep(30)
except openai.error.InvalidRequestError as e:
????print(f"Invalid request: {str(e)}")
except Exception as e:
????print(f"An error occurred: {str(e)}")
建議采用指數退避實現智能重試邏輯來處理速率限制和瞬態錯誤。
## 7、OpenAI Sora API 提示詞編寫技巧
提示符的質量會顯著影響 Sora 的輸出。學習如何編寫有效的提示符或許是使用 API 最重要的技能。
### 7.1 視頻生成的提示工程原理
有效的 Sora 提示通常遵循以下原則:
1. __具體而詳細__:包括有關設置、主題、動作、燈光、攝像機移動和風格的信息。
2. __時間結構__:按時間順序描述事件的順序,幫助 Sora 理解敘述流程。
3. __包括視覺和感官細節__:提及顏色、紋理、聲音(即使視頻是無聲的)和氛圍。
4. __指定技術方面__:相關時,包括攝像機角度、鏡頭、過渡和動作。
5. __平衡約束和創作自由__:提供足夠的指導,但不要過度限制[人工智能](http://www.dlbhg.com/wiki/what-is-artificial-intelligence/)的解釋。
### 7.2 有效提示示例
__基本提示:__
一只紅狐貍在雪林中奔跑。
__改進的提示:__
一只尾巴濃密的紅狐貍在茂密的冬日森林中奔跑。白雪皚皚的松樹環繞著小路。清晨的陽光透過枝葉,在雪地上留下斑駁的光芒。狐貍快速地從左到右移動,偶爾回頭望向鏡頭。隨著狐貍的經過,廣角鏡頭逐漸過渡到特寫。
改進的提示提供了更多關于場景、燈光、運動方向和攝影工作的背景信息,從而產生更具體、更可控的輸出。
### 7.3 描述運動和過渡
對于動態視頻,有效地傳達動作至關重要:
- __明確方向__:“從左向右移動”而不是僅僅“移動”
- __指定速度__:“緩慢平移”與“快速掃描”
- __描述過渡__:“從白天過渡到夜晚”或“切換到俯視圖”
- __詳細鏡頭運動__:“隨著拍攝對象接近而進行推拉變焦”或“跟隨角色進行慢速跟蹤拍攝”
### 7.4 掌控風格、情緒和美學
風格指導有助于設定視覺基調:
東京夜晚熙熙攘攘的街道,以霓虹黑色電影風格拍攝。濃重的陰影與鮮艷的霓虹燈形成鮮明對比,倒映在雨水濕滑的街道上。慢動作鏡頭捕捉撐傘行人穿過十字路口的場景。變形鏡頭拍攝的過往車輛前燈產生的眩光。
這個提示不僅描述了內容,還具體引用了電影風格并提供了有關視覺處理的細節。
### 7.5 需要避免的常見陷阱
- __矛盾的描述__:避免使用相互矛盾的元素,例如“陽光明媚,繁星點點的夜空”
- __過于復雜的序列__:將復雜的場景分成不同的幾代通常效果更好
- __模糊的術語__:“好看”或“漂亮”是主觀的,不如具體的視覺描述有用
- __主題或動作過多__:提示過多可能會使模型混亂
- __忽略物理一致性__:記住,物體需要在整個場景中遵循基本物理
## 8、OpenAI Sora API 高級技術
一旦您熟悉了基本的視頻生成,您就可以探索更復雜的方法來擴展 Sora 的功能。
### 8.1 鏈接多代
對于較長的敘述或復雜的序列,您可以將多個代鏈接在一起:
def generate_story_sequence(scene_descriptions, durations):
????video_urls = []
????for i, (description, duration) in enumerate(zip(scene_descriptions, durations)):
????????print(f"Generating scene {i+1}: {description[:50]}…")
????????response = openai.Sora.create(
????????????prompt=description,
????????????duration_seconds=duration
????????)
????????video_urls.append(response.data[0].url)
????????time.sleep(2)
????return video_urls
scene_descriptions = [
????"A seed sprouting from soil, close-up timelapse with morning light.",
????"The sprout growing into a small plant, developing its first leaves.",
????"The plant maturing and developing flower buds, still in timelapse.",
????"The flower blooming in vibrant colors, attracting a hummingbird."
]
durations = [8, 12, 10, 15]
video_sequence = generate_story_sequence(scene_descriptions, durations)
然后可以使用 MoviePy 或 ffmpeg 等視頻編輯庫連接這些視頻。
### 8.2 場景延續和擴展視頻
為了保持場景間的一致性:
initial_response = openai.Sora.create(
????prompt="A young woman in a red dress walks along a beach at sunset, seen from behind.",
????duration_seconds=10
)
continuation_response = openai.Sora.create(
????prompt="The same woman in the red dress now turns to face the ocean, the golden sunset light illuminating her face as she smiles.",
????duration_seconds=12
)
### 8.3 風格轉換應用
您可以嘗試將特定的視覺樣式應用到您的世代中:
styles = [
????"in the style of a watercolor painting",
????"filmed as classic film noir with high contrast black and white",
????"rendered as a vibrant anime scene",
????"captured as a vintage 8mm home movie"
]
base_prompt = "A sailboat on a calm lake with mountains in the background"
for style in styles:
????styled_prompt = f"{base_prompt}, {style}"
????print(f"Generating: {styled_prompt}")
????response = openai.Sora.create(
????????prompt=styled_prompt,
????????duration_seconds=8
????)
### 8.4 與其他 OpenAI API 結合
對于更復雜的工作流程,請將 Sora 與其他 [OpenAI ](http://www.dlbhg.com/provider/uid202405280134182e2ed4)服務結合使用:
from openai import OpenAI
client = OpenAI()
basic_idea = "Dog in a park"
gpt_response = client.chat.completions.create(
????model="gpt-4",
????messages=[
????????{"role": "system", "content": "You are a video description expert. Expand the basic video idea into a detailed, visually rich prompt for a video generation AI."},
????????{"role": "user", "content": f"Basic idea: {basic_idea}"}
????]
)
enhanced_prompt = gpt_response.choices[0].message.content
sora_response = openai.Sora.create(
????prompt=enhanced_prompt,
????duration_seconds=15
)
## 結論
[OpenAI Sora API](http://www.dlbhg.com/blog/ua-openai-sora-api-application-guide/) 代表了 [AI 生成視頻](http://www.dlbhg.com/blog/ua-streamingt2v-agent-development-leading-ai-long-video-generation-new-era)領域的重大進步,它提供了前所未有的能力,可以將[文本描述](http://www.dlbhg.com/blog/dalle3-api)轉化為高質量、連貫的視覺內容。正如我們在本指南中所探討的,要有效地實現 Sora,需要理解其技術層面以及成功生成視頻的創意原則。
對于希望利用 Sora 的開發人員和內容創建者來說,關鍵要點包括:
1. __提示設計至關重要__:提示的質量和精準度對生成結果有顯著影響。投入時間開發和完善你的提示設計技巧。
2. __了解技術基礎__:有效使用 API 需要了解其參數、響應格式和錯誤處理方法。
3. __負責任地構建__:與任何強大的人工智能技術一樣,考慮實施的道德影響并建立適當的保障措施。
4. __優化效率__:戰略緩存、批處理和資源管理有助于控制成本并提高性能。
5. __迭代和改進__:實施系統的反饋收集和評估,以不斷提高您的結果。
隨著技術的不斷發展,保持適應性將是最大限度發揮其潛力的關鍵。通過兼顧技術卓越性和創意品質,您可以充分利用這一突破性工具的全部功能,創作出引人入勝的視覺內容,而這在幾年前還是不可能實現或成本高昂的。
未來幾年,AI 視頻生成能力將迎來顯著提升,分辨率將更高、時長將更長、控制將更精準,創意可能性也將進一步拓展。現在就打下堅實的知識[基礎和最佳實踐](http://www.dlbhg.com/blog/wx-api-design-from-basics-to-best-practices),您將能夠充分利用這些新興技術。
__文章轉載自:__[How to Use OpenAI's Sora API: A Comprehensive Guide](https://qodex.ai/blog/openai-sora-api)