Skip to main content

Trim, compress, and highlight video clips — auto-managed ffmpeg, zero setup

Project description


       ██████╗██╗     ██╗██████╗ ███████╗██╗      ██████╗ ██╗    ██╗
      ██╔════╝██║     ██║██╔══██╗██╔════╝██║     ██╔═══██╗██║    ██║
      ██║     ██║     ██║██████╔╝█████╗  ██║     ██║   ██║██║ █╗ ██║
      ██║     ██║     ██║██╔═══╝ ██╔══╝  ██║     ██║   ██║██║███╗██║
      ╚██████╗███████╗██║██║     ██║     ███████╗╚██████╔╝╚███╔███╔╝
       ╚═════╝╚══════╝╚═╝╚═╝     ╚═╝     ╚══════╝ ╚═════╝  ╚══╝╚══╝

Trim · Compress · Highlight

Typed Python API and CLI for video clipping — auto-managed ffmpeg, zero setup. Zero runtime dependencies.


Tests PyPI version Python versions Coverage License: MIT Typed Homepage


CUTfLOW logo

Why clipflow?

Most Python video libraries convert frames to NumPy arrays — slow, memory-heavy, and unnecessary for trimming. clipflow builds and runs ffmpeg commands directly as subprocess calls:

  • Lossless stream-copy — trimming a 10 GB file takes seconds with zero quality loss
  • Frame-accurate re-encode when you need compression
  • Highlight routing — a first-class concept missing from every other package
  • Clean typed API — all inputs and outputs are typed dataclasses, no dicts
  • Zero runtime dependencies — stdlib + system ffmpeg, nothing else

Installation

pip install clipflow

Requires: Python ≥ 3.9

FFmpeg: Automatically downloaded and managed on first use — no manual installation required! 🎉

How FFmpeg Management Works

On first use, clipflow automatically:

  1. Detects your platform (Windows, macOS, or Linux)
  2. Downloads the appropriate FFmpeg binaries from trusted sources
  3. Caches them locally for future use
  4. Falls back to system PATH if you already have FFmpeg installed

You can also pre-download FFmpeg explicitly:

import clipflow

# Optional: Pre-download FFmpeg (happens automatically on first use anyway)
clipflow.setup_ffmpeg()

# Check where FFmpeg is located 
print(clipflow.get_ffmpeg_path())
print(clipflow.get_ffprobe_path())

Cache location:

  • Windows: %LOCALAPPDATA%\clipflow\ffmpeg\
  • macOS: ~/Library/Caches/clipflow/ffmpeg/
  • Linux: ~/.cache/clipflow/ffmpeg/

If you prefer to use your system FFmpeg, ensure it's on your PATH — clipflow will use it automatically.

Manual FFmpeg Installation (Optional)

If you want to install FFmpeg manually or use a specific version:

# Windows — via Chocolatey
choco install ffmpeg

# Windows — via winget
winget install Gyan.FFmpeg

# macOS — via Homebrew
brew install ffmpeg

# Ubuntu/Debian
sudo apt install ffmpeg

# Fedora
sudo dnf install ffmpeg

Quick start

import clipflow

results = clipflow.trim(
    "documentary.mp4",
    clipflow.ClipSpec(clipflow.parse_range("01:00", "02:30")),
)
print(results[0])
# ClipResult(✓ 'clip_01' → output/clip_01.mp4 [0.43s])
clipflow trim documentary.mp4 01:00-02:30

Python API

clipflow.trim()

import clipflow
from clipflow import ClipSpec, parse_range, COMPRESS_HIGH, AR_9_16

# Lossless stream-copy
results = clipflow.trim(
    "input.mp4",
    ClipSpec(parse_range("00:30", "02:15")),
    output_dir="clips",
)

# Multiple ranges
clips = [
    ClipSpec(parse_range("00:00", "01:00"), label="intro"),
    ClipSpec(parse_range("10:30", "12:00"), label="climax"),
    ClipSpec(parse_range("58:00", "60:00"), label="outro"),
]
results = clipflow.trim("lecture.mp4", clips, output_dir="out")

# Compress + aspect ratio + highlight
results = clipflow.trim(
    "raw_footage.mp4",
    ClipSpec(
        parse_range("05:00", "06:30"),
        highlight=True,          # copied to out/highlights/ automatically
        compress=COMPRESS_HIGH,  # CRF 18, slow preset
        aspect_ratio=AR_9_16,    # crop/pad to 9:16 for Reels/Shorts
        label="hero_moment",
    ),
    output_dir="out",
)
print(results[0].highlight_path)  # out/highlights/hero_moment.mp4

# Progress callback
def on_progress(idx, total, result):
    print(f"[{idx}/{total}] {'✓' if result.ok else '✗'} {result.spec.effective_label()}")

clipflow.trim("video.mp4", clips, output_dir="out", on_progress=on_progress)

ClipResult fields:

Field Type Description
ok bool True if clip was produced without error
output_path Path | None Absolute path to the output file
highlight_path Path | None Path to highlight copy, or None
duration_s float Wall-clock seconds the ffmpeg call took
error str | None Error message if ok is False

clipflow.inspect()

info = clipflow.inspect("documentary.mp4")

print(info.resolution)    # '1920×1080'
print(info.duration_fmt)  # '01:23:45'
print(info.fps)           # 29.97
print(info.video_codec)   # 'h264'
print(info.audio_codec)   # 'aac'
print(info.size_mb)       # 842.3

clipflow.batch()

from pathlib import Path
from clipflow import BatchSpec, ClipSpec, parse_range
import clipflow

specs = [
    BatchSpec(
        input_path=Path("ep01.mp4"),
        output_dir=Path("ep01_clips"),
        clips=[
            ClipSpec(parse_range("00:30", "01:30"), label="cold_open"),
            ClipSpec(parse_range("20:00", "21:00"), label="twist", highlight=True),
        ],
    ),
]
all_results = clipflow.batch(specs)

CLI

# Lossless trim
clipflow trim lecture.mp4 01:00-02:30

# Multiple ranges
clipflow trim lecture.mp4 00:00-01:00 10:30-12:00 --output clips/

# Compress + crop + highlight
clipflow trim concert.mp4 05:00-06:30 --compress high --aspect 9:16 --highlight

# Custom CRF + H.265
clipflow trim raw.mp4 00:00-30:00 --crf 20 --codec libx265

# Inspect metadata
clipflow inspect documentary.mp4
clipflow inspect documentary.mp4 --json | jq .fps

# Batch from JSON spec
clipflow batch spec.json

spec.json format:

[
  {
    "input": "lecture.mp4",
    "output_dir": "clips",
    "clips": [
      { "start": "00:30", "end": "02:15", "label": "intro", "compress": "medium" },
      { "start": "10:00", "end": "11:30", "highlight": true, "aspect": "16:9" }
    ]
  }
]

Time formats

All time inputs accept:

Format Example Seconds
"MM:SS" "01:30" 90.0
"HH:MM:SS" "01:02:03" 3723.0
"SS" "90" 90.0
int / float 90 / 90.5 90.0 / 90.5

Compression presets

Constant CRF Speed Use case
COMPRESS_LOW 28 fast Smallest file
COMPRESS_MEDIUM 23 medium Balanced (ffmpeg default)
COMPRESS_HIGH 18 slow Best quality

Custom:

from clipflow import CompressOptions

custom = CompressOptions(crf=20, preset="slower", codec="libx265", audio_bitrate="192k")

Aspect ratio shortcuts

Constant Ratio Use case
AR_16_9 16:9 YouTube, landscape
AR_9_16 9:16 Reels, Shorts, TikTok
AR_1_1 1:1 Instagram square
AR_4_3 4:3 Classic TV
from clipflow import AspectRatio
ultra_wide = AspectRatio(21, 9)

How it works

clipflow builds ffmpeg commands as list[str] and runs them with subprocess.run.

FFmpeg Management: On first use, clipflow automatically downloads and caches FFmpeg binaries for your platform. The binaries are stored in a local cache directory and reused on subsequent runs. If you have FFmpeg on your PATH, clipflow will use your system installation instead.

Stream-copy (default — lossless, fast):

ffmpeg -y -ss <start> -t <duration> -i input.mp4 -c copy output.mp4

-ss before -i = keyframe seek. Bytes are copied directly, no re-encoding.

Re-encode (with compress=):

ffmpeg -y -i input.mp4 -ss <start> -t <duration> -c:v libx264 -crf 23 -preset medium -c:a copy output.mp4

-ss after -i = frame-accurate cut. Required when the encoder needs full frame data.

Highlights: shutil.copy2 of the finished clip to output/highlights/. No second ffmpeg call.

The entire subprocess layer is isolated in clipflow/_ffmpeg.py — no other module touches subprocess.


Development

git clone https://github.com/ronaldgosso/clipflow.git
cd clipflow
pip install -e ".[dev]"

pytest                                           # 82 tests
pytest --cov=clipflow --cov-report=term          # coverage (>90%)
ruff check clipflow/ tests/ && black clipflow/   # lint + format
mypy clipflow/ --ignore-missing-imports          # strict types

Note: FFmpeg binaries are automatically managed. Tests mock the FFmpeg manager to avoid actual downloads.

Release:

# Bump version in pyproject.toml + clipflow/__init__.py, then:
git tag v0.2.0 && git push origin v0.2.0
# publish.yml triggers → PyPI via OIDC trusted publishing

See CONTRIBUTING.md for detailed guidelines.


License

MIT © Ronald Isack Gosso

Built with Python · auto-managed ffmpeg · subprocess · zero magic

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clipflow-0.2.0.tar.gz (44.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clipflow-0.2.0-py3-none-any.whl (37.6 kB view details)

Uploaded Python 3

File details

Details for the file clipflow-0.2.0.tar.gz.

File metadata

  • Download URL: clipflow-0.2.0.tar.gz
  • Upload date:
  • Size: 44.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clipflow-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9157244f532678ac35b87e82ccd812e367e64bc62b1d53fe240c9f5c1c3efcd0
MD5 c3fafc0c62fc04a8c570f1be86457e3b
BLAKE2b-256 2a1a33bd1ef6681060ba973203f89668ccdccf222de575f58e6fb3202f5f753c

See more details on using hashes here.

Provenance

The following attestation bundles were made for clipflow-0.2.0.tar.gz:

Publisher: publish.yml on ronaldgosso/clipflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file clipflow-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: clipflow-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 37.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for clipflow-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e306b4a4772a356176384704aad60dfbdb72f8e351bfaaf17071d0de704808b
MD5 04e327fd1f781b16bbab471f8d8d4327
BLAKE2b-256 c9aeedd7ad88e48c8ecae4b9146b3d3401fafa7b9fa035b3af5eac075c4c970b

See more details on using hashes here.

Provenance

The following attestation bundles were made for clipflow-0.2.0-py3-none-any.whl:

Publisher: publish.yml on ronaldgosso/clipflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page