Skip to main content

Plan media time ranges into byte ranges and fetch partial media data.

Project description

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-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.

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,
    HttpRangeConfig,
    HttpRangeError,
    HttpRangeFetcher,
    HttpRangeFetchResult,
    MediaRangeError,
    Mp4IndexError,
    PieceDownloadProgress,
    TorrentConfig,
    TorrentFile,
    TorrentMetadata,
    TorrentRangeError,
    TorrentRangeFetcher,
    extract_clip_to_mp4,
    is_magnet_uri,
    magnet_info_hash,
    piece_indexes_for_ranges,
    plan_mp4_clip_ranges,
    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.

Future versions may add object-storage fetchers.

Boundaries

This library does not select the "largest video", choose a default clip position, 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

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

cliprange-0.5.0.tar.gz (31.2 kB view details)

Uploaded Source

Built Distribution

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

cliprange-0.5.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file cliprange-0.5.0.tar.gz.

File metadata

  • Download URL: cliprange-0.5.0.tar.gz
  • Upload date:
  • Size: 31.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cliprange-0.5.0.tar.gz
Algorithm Hash digest
SHA256 9d4ada8a50e9e645ca886845455bd6ce5a789a739f1988626f3a9f1815be9b10
MD5 64cc92d3c37f2b280ef2ce0df9ecd401
BLAKE2b-256 3c9cfdd4c9ba28db3ad9b11536fe75be25a1c61ec54be7c9fc1108214bfbf876

See more details on using hashes here.

Provenance

The following attestation bundles were made for cliprange-0.5.0.tar.gz:

Publisher: publish.yml on buzhidaoqusm/cliprange

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

File details

Details for the file cliprange-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: cliprange-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cliprange-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57dd96427aedac9d4990833aba7322199fd6d76bd1758d8603b37f811ee62fa1
MD5 e6c4049ab0ec00da939f4f2fdcc4b41b
BLAKE2b-256 ba1898359b0a50c56a35fdcfc2495b2424b0ef008c1f6740ea1cb384f7bedf09

See more details on using hashes here.

Provenance

The following attestation bundles were made for cliprange-0.5.0-py3-none-any.whl:

Publisher: publish.yml on buzhidaoqusm/cliprange

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