Skip to main content

cliprange

cliprange maps media time ranges to byte ranges and provides optional fetchers for partial media workflows.

The first version focuses on MP4/M4V/MOV planning and libtorrent-based piece fetching. It is intentionally independent from QQBot, NapCat, OneBot, ZIP packaging, and chat-bot task state.

Install

For planner-only usage:

pip install cliprange

Torrent support requires the optional torrent extra:

pip install "cliprange[torrent]"

Clip extraction requires ffmpeg and ffprobe to be available on PATH.

For local development from a checkout:

pip install -e ".[dev,torrent]"

Example

from cliprange import ClipRequest, plan_mp4_clip_ranges

plan = plan_mp4_clip_ranges(
    "video.mp4",
    ClipRequest(start_seconds=600, duration_seconds=120),
)

print(plan.ranges)
print(plan.estimated_bytes)

HTTP Range Example

from cliprange import ClipRequest, HttpRangeFetcher, plan_mp4_clip_ranges

plan = plan_mp4_clip_ranges(
    "video.mp4",
    ClipRequest(start_seconds=600, duration_seconds=120),
)

fetcher = HttpRangeFetcher("https://example.com/video.mp4")
result = await fetcher.fetch_ranges(plan.ranges, "partial-video.mp4")

print(result.bytes_written)

The HTTP fetcher requires the server to support Range requests and return 206 Partial Content. It will not fall back to downloading a full file when a server ignores the Range header.

CLI

Editable installs expose three commands:

cliprange-plan video.mp4 --start 00:10:00 --duration 00:02:00
cliprange-clip video.mp4 --start 00:10:00 --duration 00:02:00 --out clip.mp4
cliprange-torrent "magnet:?xt=..." --file-largest-video --middle 120 --out clip.mp4
cliprange-preview "magnet:?xt=..." --time 00:10:23 --out preview.jpg

cliprange-torrent requires the optional torrent extra and only downloads pieces needed for the selected clip. Users are responsible for accessing only media they are authorized to use.

cliprange-preview also requires the optional torrent extra. It currently supports MP4/M4V/MOV files and writes one JPEG frame for the requested timestamp.

Magnet Preview Example

from cliprange import FramePreviewRequest, TorrentConfig, preview_frame_from_magnet

result = await preview_frame_from_magnet(
    "magnet:?xt=...",
    request=FramePreviewRequest(timestamp_seconds=623.0),
    output_path="preview.jpg",
    config=TorrentConfig(download_dir="downloads"),
)

print(result.output_path)
print(result.piece_indexes)
print(result.cache_key)

To grab several frames from one video in a single torrent session, use preview_frames_from_magnet. Set exactly one of frame_count, interval_seconds, or timestamps_seconds:

from cliprange import MultiFramePreviewRequest, TorrentConfig, preview_frames_from_magnet

result = await preview_frames_from_magnet(
    "magnet:?xt=...",
    request=MultiFramePreviewRequest(frame_count=9, max_frames=16),
    output_dir="frames",
    config=TorrentConfig(download_dir="downloads"),
)

for frame in result.frames:
    print(frame.target_seconds, frame.output_path)

Real-World Smoke Test

The default test suite does not access external networks. To validate the library against a real public MP4 URL, run:

python scripts/smoke_real_world.py

The smoke script checks HTTP Range support, downloads a small public MP4, plans byte ranges, fetches those ranges, runs the CLI planner, extracts a short clip, and verifies the output duration.

To run the same check through pytest:

$env:CLIPRANGE_RUN_REAL_WORLD = "1"
pytest -m integration

Public API

The stable import surface for the current internal version is the package root:

from cliprange import (
    ByteRange,
    ClipPlan,
    ClipRangeError,
    ClipRequest,
    FfmpegError,
    FramePreviewError,
    FramePreviewPlan,
    FramePreviewRequest,
    FramePreviewResult,
    HttpRangeConfig,
    HttpRangeError,
    HttpRangeFetcher,
    HttpRangeFetchResult,
    MediaRangeError,
    Mp4IndexError,
    PieceDownloadProgress,
    PreviewDownloadTooLargeError,
    TorrentConfig,
    TorrentFile,
    TorrentMetadata,
    TorrentRangeError,
    TorrentRangeFetcher,
    UnsupportedPreviewFormatError,
    extract_clip_to_mp4,
    extract_frame_to_jpeg,
    is_magnet_uri,
    magnet_info_hash,
    piece_indexes_for_ranges,
    plan_mp4_clip_ranges,
    plan_mp4_frame_preview_ranges,
    preview_frame_from_magnet,
    preview_frame_from_torrent,
    probe_duration_seconds,
    probe_piece_indexes,
    ratio_buffer_piece_indexes,
)

Internal MP4 box parsing helpers are intentionally private and may change without notice.

Scope

  • MP4/M4V/MOV time range planning.
  • Byte ranges to torrent piece indexes.
  • HTTP Range fetching for planned byte ranges.
  • Optional libtorrent metadata and piece fetching.
  • Optional ffmpeg helpers for clip extraction.
  • Optional magnet/torrent JPEG preview frame extraction for MP4/M4V/MOV files.

Future versions may add object-storage fetchers.

Boundaries

This library does not choose application-specific defaults such as "middle two minutes", create ZIP files, upload files, store bot task state, or format chat messages. Applications should keep those decisions in their own code.

Roadmap

The plan for turning this internal prototype into a publishable library is tracked in docs/roadmap-to-1.0.md.

Documentation

Metadata

Release files for cliprange 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cliprange 0.7.0
File Size Uploaded Provenance
cliprange-0.7.0.tar.gz 42.8 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for cliprange 0.7.0
File Interpreter ABI Platform
cliprange-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 69.5 kB

Release files / cliprange-0.7.0.tar.gz

Download URL cliprange-0.7.0.tar.gz
Size 42.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b7f8ec04101f0c870c675fee88ed2c6389cbd43ecadb7b123f56fa30cbf82208
BLAKE2b-256 checksum
How to use checksums
b35122a8994d99a7ead0eba5bcb9943fb65267af7b8b4674e50c8331f80437c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / cliprange-0.7.0-py3-none-any.whl

Download URL cliprange-0.7.0-py3-none-any.whl
Size 26.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
397c9e275f393be8e68d37a7e31e527cebe37ae149c22de0646a2ee3625a2e27
BLAKE2b-256 checksum
How to use checksums
6f29fb84ef656ff68bb4980ebd70163ded29d09c8a5db155ece8aa1900aa042c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for cliprange 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for cliprange-0.7.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page