-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_to_video.py
More file actions
130 lines (111 loc) · 4.14 KB
/
Copy pathtext_to_video.py
File metadata and controls
130 lines (111 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
Seedance 2.0 Text-to-Video – Basic Example
==========================================
Generate video from a text prompt using the Seedance 2.0 API on Atlas Cloud.
Get your API key: https://www.atlascloud.ai/seedance-2?utm_source=github&utm_campaign=free-seedance-2-api
API docs: https://www.atlascloud.ai/models/bytedance/seedance-2.0/text-to-video?tab=api&utm_source=github&utm_campaign=free-seedance-2-api
"""
import os
import time
import requests
API_KEY = os.environ.get("ATLASCLOUD_API_KEY", "your_api_key_here")
BASE_URL = "https://api.atlascloud.ai"
def generate_video(
prompt: str,
model: str = "bytedance/seedance-2.0/text-to-video",
width: int = 1280,
height: int = 720,
duration: int = 5,
fps: int = 24,
negative_prompt: str = "",
seed: int | None = None,
) -> str:
"""
Generate a video from a text prompt.
Args:
prompt: Description of the video to generate
model: Model ID. Use 'text-to-video-fast' for cheaper, faster generation
width: Output width in pixels
height: Output height in pixels
duration: Video duration in seconds (5–15)
fps: Frames per second (24 or 30)
negative_prompt: Things to avoid in the output
seed: Random seed for reproducibility
Returns:
URL to the generated MP4 video file
"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}",
}
input_params = {
"prompt": prompt,
"width": width,
"height": height,
"duration": duration,
"fps": fps,
}
if negative_prompt:
input_params["negative_prompt"] = negative_prompt
if seed is not None:
input_params["seed"] = seed
# Submit generation request
response = requests.post(
f"{BASE_URL}/api/v1/model/generateVideo",
headers=headers,
json={"model": model, "input": input_params},
timeout=30,
)
response.raise_for_status()
prediction_id = response.json()["data"]["id"]
print(f"Generation started | ID: {prediction_id}")
# Poll for completion
poll_headers = {"Authorization": f"Bearer {API_KEY}"}
poll_url = f"{BASE_URL}/api/v1/model/prediction/{prediction_id}"
interval = 2
while True:
result = requests.get(poll_url, headers=poll_headers, timeout=15).json()["data"]
status = result["status"]
print(f"Status: {status}")
if status in ("completed", "succeeded"):
video_url = result["outputs"][0]
predict_time = result.get("metrics", {}).get("predict_time", "?")
print(f"Done in {predict_time}s → {video_url}")
return video_url
if status == "failed":
raise RuntimeError(f"Generation failed: {result.get('error', 'unknown error')}")
time.sleep(interval)
interval = min(interval * 1.5, 10) # Exponential backoff, max 10s
def download_video(url: str, output_path: str = "output.mp4") -> str:
"""Download a generated video to local disk."""
response = requests.get(url, timeout=60, stream=True)
response.raise_for_status()
with open(output_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Saved → {output_path}")
return output_path
if __name__ == "__main__":
# Example 1: Cinematic landscape
video_url = generate_video(
prompt=(
"A cinematic drone shot gliding over snow-capped mountain peaks at golden hour, "
"volumetric fog in the valleys, ultra HD, photorealistic"
),
width=1920,
height=1080,
duration=10,
)
download_video(video_url, "mountain_landscape.mp4")
# Example 2: Vertical format for social media
video_url = generate_video(
prompt=(
"A barista expertly pouring latte art in slow motion, "
"warm café lighting, bokeh background, commercial style"
),
width=1080,
height=1920, # 9:16 vertical
duration=5,
model="bytedance/seedance-2.0/text-to-video-fast", # Faster + cheaper
)
download_video(video_url, "latte_art.mp4")