-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_usage.py
More file actions
92 lines (74 loc) · 2.95 KB
/
example_usage.py
File metadata and controls
92 lines (74 loc) · 2.95 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
#!/usr/bin/env python3
"""Example: programmatic usage of the PostAnimate library.
This script demonstrates how to use the postanimate package as a library
in your own project, rather than through the CLI.
"""
from pathlib import Path
from postanimate.animator import PosterAnimator
from postanimate.config import Config
from postanimate.image_prep import prepare_poster_image
from postanimate.loop_processor import create_seamless_loop
def example_full_pipeline():
"""Run the full pipeline: prep -> upload -> generate -> loop."""
config = Config.from_env()
config.output_dir = Path("output")
animator = PosterAnimator(config)
result = animator.animate(
poster_path="input/my_poster.jpg",
prompt="subtle ambient particle effects, gentle light flickers, cinematic atmosphere",
negative_prompt="blurry, distorted, low quality, jittery, text changes",
loop_mode="crossfade",
crossfade_duration=1.0,
mode="pro", # higher quality
duration=5, # 5-second clip
)
print(f"Task ID: {result['task_id']}")
print(f"Raw video: {result['raw_video']}")
print(f"Looped video: {result['looped_video']}")
def example_image_prep_only():
"""Prepare a poster image without generating video."""
prepared = prepare_poster_image(
"input/my_poster.png",
output_path="output/my_poster_27x40.jpg",
long_side=1920,
fit_mode="cover", # crop to fill the 27:40 ratio
)
print(f"Prepared image: {prepared}")
def example_loop_existing_video():
"""Apply seamless looping to a video you already have."""
looped = create_seamless_loop(
"output/existing_animation.mp4",
output_video="output/existing_animation_loop.mp4",
crossfade_duration=1.5,
num_loops=3, # 3 repetitions in the output file
)
print(f"Looped video: {looped}")
def example_batch_processing():
"""Animate multiple posters in sequence."""
config = Config.from_env()
config.output_dir = Path("output/batch")
animator = PosterAnimator(config)
posters = [
("input/action_movie.jpg", "dynamic energy waves, flickering flames"),
("input/horror_film.jpg", "creeping fog, subtle shadow movement"),
("input/romance.jpg", "floating petals, soft bokeh light shifts"),
]
results = []
for poster_path, prompt in posters:
try:
result = animator.animate(
poster_path=poster_path,
prompt=prompt,
output_name=Path(poster_path).stem,
)
results.append(result)
print(f"Done: {poster_path} -> {result['looped_video']}")
except Exception as e:
print(f"Failed: {poster_path} - {e}")
return results
if __name__ == "__main__":
# Uncomment the example you want to run:
example_full_pipeline()
# example_image_prep_only()
# example_loop_existing_video()
# example_batch_processing()