DEV Community

Cover image for Generating Short Clips from Long Videos with Python
Frank Boucher ☁ for Reka

Posted on

Generating Short Clips from Long Videos with Python

This guide walks through clip_generator.py, a script in the clip-api-examples repo that uses the Clip API to extract short, captioned segments from a longer video. You provide a video source, either a YouTube URL or a local MP4 file, and the script handles the rest: downloading or uploading the video, analyzing it, and rendering a new clip based on your instructions. The example in this guide uses a YouTube URL to keep the setup simple.

The full source code is available at github.com/reka-ai/clip-api-examples.

Prerequisites

Clone the repository and install dependencies:

git clone https://github.com/reka-ai/clip-api-examples
cd clip-api-examples/python
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Then export your API key:

export REKA_API_KEY=your_key_here
Enter fullscreen mode Exit fullscreen mode

If you don't have a key yet, get one here. Free credits renew every month.

The README file showing installation instructions and environmental variable setup

Running the Script

From the python/ folder, run:

python clip_generator.py
Enter fullscreen mode Exit fullscreen mode

The script prompts you for a YouTube URL. Paste it in, and the job starts immediately.

The Job Lifecycle

Once submitted, the job moves through several statuses printed to your terminal: queued, processing, then completed. This works because the script uses stream=True in its HTTP request, which keeps the connection open and lets the script poll in a loop until the job finishes. If you prefer, you can remove stream=True and instead make one call to create the job, then a separate polling call to check its status.

Terminal output showing the job status moving through various stages of processing

Customizing the Payload

The heart of the script is the create_clip function, which builds a payload for the API:

payload = {
    "video_url": url,
    "prompt": "create an engaging video clip highlighting the best moments",
    "template": "moment",
    "num_generations": 1,
    "generation_config": {
        "aspect_ratio": "9:16",
        "subtitles": True,
        "min_duration_seconds": 10,
        "max_duration_seconds": 30
    }
}
Enter fullscreen mode Exit fullscreen mode

Each field is customizable:

  • prompt: Plain-language instruction. Examples: "the funniest moments", "the part where I did the demo". Treat it like a brief to an editor.
  • template: Controls the overall clip style. "moment" is one option; the repo includes others.
  • num_generations: Request multiple clip variations in one job, useful for comparing different cuts.
  • min_duration_seconds / max_duration_seconds: Keep these short during development for faster turnaround.
  • subtitles: Set to True to burn captions into the video, False to skip them.
  • aspect_ratio: "9:16" for TikTok/Reels, "1:1" for square, or keep the original.

A look at the Python code payload where aspect ratio and prompts are defined

The Output

When the job completes, the API returns a generated title, a description of the clip, and suggested hashtags. The video itself is already trimmed and has captions burned in. The demo in the video processed an 18-minute horizontal talk (no captions) into a 25-second vertical clip with orange subtitles.

Clone the repo, adjust the payload to fit your content, and happy clipping.

Top comments (0)