Communitygithub.com

Runway ML API(影片生成與編輯)

使用 Runway ML API 進行 AI 影片生成和編輯 — Gen-3 Alpha Turbo、圖片轉影片、影片轉影片。適用於從文字或圖片生成影片、套用 AI 影片特效,或自動化創意影片產出流程。

Runway ML API(影片生成與編輯) 是什麼?

此技能讓 AI 代理能透過 Runway ML API,以 Gen-3 Alpha Turbo 模型進行文字轉影片和圖片轉影片生成。採用非同步任務模式:建立任務、輪詢狀態、下載結果,支援多種解析度比例與持續時間。詳細的 Python 範例涵蓋完整流程,包括任務建立、輪詢與下載,並提供批次生成與錯誤處理指引。此技能適合整合至影片內容管線(如 Claude Code、Cursor),實現自動化影片製作。

相容平台✓Claude Code✓Codex CLI✓Cursor✓Gemini CLI
npx skills add https://github.com/TerminalSkills/skills/tree/HEAD/skills/runway-ml

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Runway ML API

概述

Runway 的 Gen-3 Alpha Turbo 模型可從文字提示或圖片生成高品質影片。REST API 採用非同步任務模式:建立任務、輪詢完成狀態、下載結果。可用於製作電影級短片、將圖片轉為動畫,或建置自動化影片內容管線。

設定

pip install requests python-dotenv
export RUNWAY_API_KEY="your_api_key_here"

Base URL: https://api.dev.runwayml.com/v1
API 文件:https://docs.runwayml.com

核心概念

  • Task(任務):一個非同步的影片生成工作。建立後立即回傳 task_id。
  • Gen-3 Alpha Turbo:最快的 Gen-3 模型 — 最適合生產管線。
  • 圖片轉影片(gen3a_turbo):將靜態圖片轉為動態影片。
  • 文字轉影片:完全根據文字提示生成影片。
  • 持續時間:5 或 10 秒。
  • 比例:1280:720(橫向)、720:1280(直向)、1104:832、832:1104、960:960(正方形)。

操作說明

步驟 1:設定客戶端

import os
import time
import requests

API_KEY = os.environ["RUNWAY_API_KEY"]
BASE_URL = "https://api.dev.runwayml.com/v1"
HEADERS = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json",
    "X-Runway-Version": "2024-11-06"
}

步驟 2:文字轉影片生成

def text_to_video(
    prompt_text: str,
    duration: int = 5,
    ratio: str = "1280:720",
    seed: int = None
) -> str:
    """提交文字轉影片任務,回傳 task_id。"""
    payload = {
        "model": "gen3a_turbo",
        "promptText": prompt_text,
        "duration": duration,
        "ratio": ratio
    }
    if seed is not None:
        payload["seed"] = seed

    r = requests.post(f"{BASE_URL}/image_to_video", json=payload, headers=HEADERS)
    r.raise_for_status()
    return r.json()["id"]

task_id = text_to_video(
    prompt_text="A drone shot flying over a misty mountain valley at golden hour, cinematic, slow motion",
    duration=5,
    ratio="1280:720"
)
print(f"Task submitted: {task_id}")

步驟 3:圖片轉影片生成

import base64
from pathlib import Path

def image_to_video(
    image_path: str,
    prompt_text: str = "",
    duration: int = 5,
    ratio: str = "1280:720",
    seed: int = None
) -> str:
    """將圖片轉為影片。image_path 可以是本地檔案路徑或 URL。"""

    if image_path.startswith("http"):
        prompt_image = image_path
    else:
        # 將本地檔案編碼為 data URI
        img_bytes = Path(image_path).read_bytes()
        ext = Path(image_path).suffix.lstrip(".").lower()
        mime = {"jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png", "webp": "image/webp"}.get(ext, "image/png")
        b64 = base64.b64encode(img_bytes).decode()
        prompt_image = f"data:{mime};base64,{b64}"

    payload = {
        "model": "gen3a_turbo",
        "promptImage": prompt_image,
        "promptText": prompt_text,
        "duration": duration,
        "ratio": ratio
    }
    if seed is not None:
        payload["seed"] = seed

    r = requests.post(f"{BASE_URL}/image_to_video", json=payload, headers=HEADERS)
    r.raise_for_status()
    return r.json()["id"]

task_id = image_to_video(
    image_path="product_shot.png",
    prompt_text="The product slowly rotates, sparkling particles float around it, luxury feel",
    duration=5,
    ratio="1280:720"
)
print(f"Task submitted: {task_id}")

步驟 4:輪詢任務狀態

def get_task(task_id: str) -> dict:
    r = requests.get(f"{BASE_URL}/tasks/{task_id}", headers=HEADERS)
    r.raise_for_status()
    return r.json()

def wait_for_task(task_id: str, poll_interval: int = 5, timeout: int = 600) -> list[str]:
    """輪詢直到任務完成;回傳輸出影片 URL 列表。"""
    start = time.time()
    while True:
        task = get_task(task_id)
        status = task["status"]
        progress = task.get("progress", 0)
        print(f"[{int(time.time()-start)}s] 狀態: {status} ({int(progress*100)}%)")

        if status == "SUCCEEDED":
            return task["output"]  # 影片 URL 列表
        elif status in ("FAILED", "CANCELLED"):
            raise RuntimeError(f"任務 {status}: {task.get('failure', '')}")
        elif time.time() - start > timeout:
            raise TimeoutError(f"任務在 {timeout}s 後未完成")

        time.sleep(poll_interval)

output_urls = wait_for_task(task_id)
print(f"影片就緒: {output_urls}")

步驟 5:下載結果

def download_video(url: str, output_path: str = "output.mp4") -> str:
    r = requests.get(url, stream=True)
    r.raise_for_status()
    with open(output_path, "wb") as f:
        for chunk in r.iter_content(chunk_size=8192):
            f.write(chunk)
    size_mb = os.path.getsize(output_path) / 1024 / 1024
    print(f"已儲存: {output_path} ({size_mb:.1f} MB)")
    return output_path

download_video(output_urls[0], "mountain_valley.mp4")

完整管線範例

def generate_and_download(prompt: str, output_path: str = "output.mp4", **kwargs) -> str:
    """一鍵完成:從文字生成影片並下載。"""
    print(f"正在生成: {prompt[:80]}...")
    task_id = text_to_video(prompt, **kwargs)
    urls = wait_for_task(task_id)
    return download_video(urls[0], output_path)

# 生成產品廣告片段
generate_and_download(
    prompt="Close-up of a sleek smartphone on a white desk, screen lights up, smooth camera pull-back",
    output_path="product_ad.mp4",
    duration=5,
    ratio="1280:720"
)

參數參考

參數值說明
modelgen3a_turbo使用 Gen-3 Alpha Turbo(最快)
duration5, 10影片長度(秒)
ratio1280:720, 720:1280, 1104:832, 832:1104, 960:960解析度比例
seed整數可重現性的種子,用於產生確定性輸出
promptText字串描述目標影片的文字提示
promptImageURL 或 data URI圖片轉影片的起始圖片

指引

  • Runway 任務根據持續時間與負載,通常需要 30–120 秒完成。
  • 輸出 URL 有時間限制 — 請在生成後立即下載影片。
  • 提示詞應描述性強且富有電影感:包含鏡頭運動、光線、情緒。
  • 在迭代提示詞時使用 seed 來重現相同結果。
  • 批次生成時,請平行排入任務,但務必遵守速率限制(檢查 HTTP 429 狀態碼,並根據 Retry-After 標頭值重試)。
  • API 金鑰應儲存在環境變數中 — 切勿寫死在程式碼中。
  • 由於模型名稱與端點變化快速,請查閱 https://docs.runwayml.com 以取得最新資訊。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

TerminalSkills/skills

>- Run GitHub Actions locally with act. Use when a user asks to test GitHub Actions workflows locally, debug CI pipelines without pushing, or run workflows offline.

TerminalSkills/skills

>- You are an expert in AG2 (formerly AutoGen), the open-source multi-agent conversation framework. You help developers build systems where multiple AI agents collaborate through structured conversations — with tool use, human-in-the-loop, code execution, group chat orchestration, and nested conversations — for complex tasks like software development, research, and data analysis.

TerminalSkills/skills

>- Generate short-form videos with AI — script writing, text-to-speech narration, stock footage selection, subtitle generation, and video assembly. Use when: creating TikTok/YouTube Shorts/Reels content, automating video production, building content pipelines.

TerminalSkills/skills

>- Animate 3D objects and characters in Blender with Python. Use when the user wants to keyframe properties, create armatures and rigs, set up IK/FK chains, animate shape keys for facial animation, edit F-Curves, use the NLA editor to blend actions, add drivers for expression-based animation, or script any animation workflow in Blender.

TerminalSkills/skills

>- Automate motion capture and tracking workflows in Blender with Python. Use when the user wants to import BVH or FBX mocap data, retarget motion to armatures, track camera or object motion from video, solve camera motion, clean up motion capture data, or script any tracking pipeline in Blender.

TerminalSkills/skills

>- You are an expert in Bolt.new by StackBlitz, the AI-powered full-stack development environment that runs entirely in the browser. You help developers go from idea to deployed app in minutes using natural language prompts — Bolt generates complete applications with frontend, backend, database, and deployment, all running in a WebContainer without local setup.

TerminalSkills/skills

>- Assists with using Bun as an all-in-one JavaScript/TypeScript runtime, package manager, bundler, and test runner. Use when building HTTP servers, managing packages, running tests, or migrating from Node.js. Trigger words: bun, bun serve, bun install, bun test, bun build, javascript runtime, bun runtime.

TerminalSkills/skills

>- Self-host customer support with Chatwoot. Use when a user asks to set up open-source customer support, add live chat without SaaS costs, build a multi-channel inbox, or deploy a free Intercom alternative.

TerminalSkills/skills

>- You are an expert in Chi, the lightweight, idiomatic Go HTTP router built on `net/http`. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters, sub-routers, and context-based request scoping — providing Express-like ergonomics while staying 100% compatible with Go's standard library.

TerminalSkills/skills

When the user wants to edit, review, or improve existing marketing copy. Also use when the user mentions 'edit this copy,' 'review my copy,' 'copy feedback,' 'proofread,' 'polish this,' 'make this better,' or 'copy sweep.' This skill provides a systematic approach to editing marketing copy through multiple focused passes.

TerminalSkills/skills

>- Add live chat and customer support with Crisp. Use when a user asks to add a chat widget, implement live customer support, set up a help desk, create a knowledge base, or add a chatbot to a website.

TerminalSkills/skills

>- Assists with building custom interactive data visualizations using D3.js. Use when creating charts, graphs, maps, force layouts, or hierarchical diagrams that require fine-grained control beyond what charting libraries provide. Trigger words: d3, data visualization, chart, svg, scales, force graph, treemap, choropleth.

TerminalSkills/skills

dbt (data build tool) transforms data in your warehouse using SQL SELECT statements. Learn project setup, models, tests, documentation, incremental materializations, and integration with data warehouses like PostgreSQL, BigQuery, and Snowflake.

TerminalSkills/skills

>- You are an expert in dlt, the open-source Python library for building data pipelines. You help developers load data from any API, file, or database into warehouses and lakes using simple Python decorators — with automatic schema inference, incremental loading, and built-in data contracts. dlt is the "requests library for data pipelines.

TerminalSkills/skills

Data Version Control for ML projects. Track large datasets and models alongside Git, build reproducible ML pipelines, and run experiments with metric comparison. Works with any storage backend including S3, GCS, Azure, and local filesystems.

TerminalSkills/skills

>- You are an expert in E2B, the cloud platform for running AI-generated code in secure sandboxes. You help developers give AI agents the ability to execute code, install packages, read/write files, and run long processes in isolated cloud environments — each sandbox is a lightweight VM that boots in ~150ms with full Linux, filesystem, and networking.

TerminalSkills/skills

>- Transcode, convert, edit, and process audio and video with FFmpeg. Use when a user asks to convert video formats (webm to mp4, mkv to mp4), extract audio from video, compress video files, trim or cut clips, concatenate videos, add subtitles, create thumbnails, apply filters, change resolution or bitrate, re-encode media, create GIFs from video, add watermarks, normalize audio, stream media, or build automated media processing pipelines.

TerminalSkills/skills

>- Create advanced animations with GSAP. Use when building complex timelines, scroll-driven animations, SVG morphing, text animations, or high-performance web animations beyond CSS capabilities.

TerminalSkills/skills

>- Generate deterministic MP4 videos from HTML, CSS, media, and seekable animations using HeyGen's HyperFrames framework. Use when someone asks to "render HTML to video", "make a video with HyperFrames", "create a programmatic video", "turn an animation into MP4", "build a launch/product video from HTML", "render GSAP/Lottie/Three.js to video", or set up an agent-driven video pipeline. Covers init/preview/render/add/lint/inspect commands, data-* timing attributes, animation adapters, and the component catalog.

TerminalSkills/skills

When the user wants to perform load testing, stress testing, or performance testing of APIs and websites using k6. Also use when the user mentions "k6," "load test," "performance test," "stress test," "spike test," "soak test," "thresholds," "virtual users," or "VUs." For browser-based testing, see selenium.

相關技能