#!/usr/bin/env python3 # Listing reel from your photos: a property's photos become a vertical reel, # each with a slow push-in or pan, the address and price over the opening, a # logo in the corner, a music bed, and a closing card. A 30-second reel from # ten photos is 45 credits; the Preview is free. # The calls of the page https://api.oneupai.com/docs/recipes/listing-reel, in # its order, as one program. # # In the folder you run it from: the photos as photo-1.jpg, photo-2.jpg, ... (in # the order they should play; the page's Agent takes 4 to 40), your logo as # logo.png and the music as music.mp3. Change ADDRESS, DETAILS and LINES below. # # export ONEUP_API_KEY="oneup_live_..." # your Builder key # python3 run.py # Python 3.8 or newer # # ONEUP_BASE_URL changes the host (default https://api.oneupai.com). Every # answer is printed as it arrives. Standard library only. import glob, json, os, time, urllib.error, urllib.request, uuid API = os.environ.get("ONEUP_BASE_URL", "https://api.oneupai.com") KEY = os.environ["ONEUP_API_KEY"] # The default Python User-Agent is refused before a request reaches the API; # any name for your app works. AGENT = "my-app/1.0" ADDRESS = "14 Maple Lane, Oakridge" DETAILS = "$849,000 · 3 bd · 2.5 ba" # Script lines: (photo number, words shown over that photo). LINES = [(2, "Sunlit kitchen with an island"), (3, "Primary suite with a view")] def call(method, path, body=None, headers=None): """One API call: the answer is printed and returned; a refusal stops here.""" request = urllib.request.Request( API + path, method=method, data=None if body is None else json.dumps(body).encode(), headers={ "Authorization": f"Bearer {KEY}", "Content-Type": "application/json", "User-Agent": AGENT, **(headers or {}), }, ) try: with urllib.request.urlopen(request, timeout=60) as response: answer = json.load(response) except urllib.error.HTTPError as error: problem = json.load(error) print(json.dumps(problem)) raise SystemExit(f"{error.code} {problem['error']}: {problem['message']}") print(json.dumps(answer)) return answer def upload(name, content_type): with open(name, "rb") as file: data = file.read() target = call( "POST", "/v1/uploads/initiate", {"content_type": content_type, "file_name": name, "size_bytes": len(data)}, ) put = urllib.request.Request( target["upload_url"], method="PUT", data=data, headers={"Content-Type": content_type, "User-Agent": AGENT}, ) urllib.request.urlopen(put, timeout=300).close() return target["upload_id"] def render(project_id, version_id, kind): r = call( "POST", "/v1/renders", { "alias": "oneup/render-timeline", "project_id": project_id, "version_id": version_id, "kind": kind, }, {"Idempotency-Key": str(uuid.uuid4())}, ) render_id = r["render_id"] while r["status"] not in ("COMPLETED", "FAILED"): time.sleep(5) r = call("GET", f"/v1/renders/{render_id}") return r files = sorted(glob.glob("photo-*.jpg"), key=lambda f: int(f[6:-4])) if not files: raise SystemExit("no photo-*.jpg in this folder") photos = [upload(f, "image/jpeg") for f in files] logo = upload("logo.png", "image/png") music = upload("music.mp3", "audio/mpeg") project = call("POST", "/v1/projects", {"agent_slug": "oneup/render-timeline"}) project_id = project["project_id"] # Each photo is 3 s, joined by 0.4 s crossfades; the card is 4 s. The reel's # length is the durations added up, minus each crossfade. n = len(photos) total = n * 3000 + 4000 - n * 400 push_in = { "op": "kenburns", "from": {"x": 0.5, "y": 0.5, "zoom": 1}, "to": {"x": 0.5, "y": 0.5, "zoom": 1.18}, "ease": "ease_in_out", } pan = { "op": "kenburns", "from": {"x": 0.58, "y": 0.5, "zoom": 1.18}, "to": {"x": 0.42, "y": 0.5, "zoom": 1.18}, "ease": "ease_in_out", } segments = [ { "id": f"p{i + 1}", "source": {"$asset": photo}, "duration": 3000, "effects": [push_in if i % 2 == 0 else pan], "transition_out": {"op": "crossfade", "duration": 400}, } for i, photo in enumerate(photos) ] segments.append( { "id": "card", "source": {"$asset": photos[-1]}, "duration": 4000, "effects": [{"op": "fade", "from": 1, "to": 0, "duration": 600}], } ) overlays = [ { "id": "logo", "kind": "image", "source": {"$asset": logo}, "position": {"x": 0.05, "y": 0.028}, "scale": 0.12, "range": {"from": 0, "to": total}, }, { "id": "t1", "kind": "text", "content": ADDRESS, "style": "lower_third", "position": "middle", "range": {"from": 0, "to": 3000}, "enter": {"op": "fade", "duration": 250}, "exit": {"op": "fade", "duration": 200}, }, { "id": "t2", "kind": "text", "content": DETAILS, "style": "lower_third", "position": "bottom", "range": {"from": 0, "to": 3000}, }, ] for photo, words in LINES: if 2 <= photo <= n: start = (photo - 1) * 2600 overlays.append( { "id": f"line{photo}", "kind": "text", "content": words, "style": "lower_third", "position": "bottom", "range": {"from": start + 400, "to": start + 3000}, } ) timeline = { "v": "1.1", "output": {"aspect": "9:16", "resolution": 1080, "fps": 30, "container": "mp4"}, "segments": segments, "overlays": overlays, "audio": [ { "id": "bed", "role": "music", "source": {"$asset": music}, "range": {"from": 0, "to": total}, "loop": True, "gain": -14, "fade_in": 600, "fade_out": 600, } ], } version = call("PUT", f"/v1/projects/{project_id}/timeline", {"timeline": timeline}) render(project_id, version["version_id"], "preview") final = render(project_id, version["version_id"], "final") call("GET", f"/v1/queue/result/{final['ticket']}")