Skip to main content

A fast, efficient video trimming and manipulation toolkit

Project description

videotrim

A fast, efficient video trimming and manipulation toolkit built on Python.

Features

  • Fast Video I/O: Efficient video reading and writing with imageio and PyAV backends
  • Frame-Accurate Trimming: Precise frame-level control with re-encoding support
  • Lossless Copy Mode: Ultra-fast trimming using ffmpeg's copy mode (when frame accuracy isn't critical)
  • Frame Extraction: Export individual frames as images
  • Video Concatenation: Merge multiple videos into one
  • Flexible CLI: Powerful command-line interface with timestamp and frame-based operations
  • Python API: Full-featured library for programmatic video manipulation

Quick Start

Run without installation (using uv)

# Install uv if you haven't already
curl -LsSf https://astral.sh/uv/install.sh | sh

# Run videotrim directly
uvx --from videotrim videotrim --help

# Trim a video (frames 100-500)
uvx --from videotrim videotrim trim input.mp4 output.mp4 --start 100 --end 500

# Trim by timestamps
uvx --from videotrim videotrim trim input.mp4 output.mp4 --start-time 00:10 --end-time 00:30

Install as a tool

# Install videotrim as a tool
uv tool install videotrim

# Run directly
videotrim --help
videotrim info input.mp4
videotrim trim input.mp4 output.mp4 -s 100 -e 500

# Update to latest version
uv tool upgrade videotrim

Installation

Install from PyPI (when published)

# Using pip
pip install videotrim

# Or using uv
uv pip install videotrim

Install from Source

# Clone the repository
git clone https://github.com/talmolab/videotrim.git
cd videotrim

# Install with uv
uv pip install -e .

# Or with dev dependencies
uv pip install -e ".[dev]"

Command Line Usage

Get video information

videotrim info input.mp4

Output:

File: input.mp4
Size: 1,234,567 bytes
Duration: 00:01:23.456
FPS: 30.00
Frames: 2,504
Resolution: 1920x1080
Codec: h264

Trim videos

# Trim by frame range (frame-accurate with re-encoding)
videotrim trim input.mp4 output.mp4 --start 100 --end 500

# Trim by timestamps (skip first 30 seconds, save next 5 seconds)
videotrim trim input.mp4 output.mp4 --start-time 00:30 --end-time 00:35 --mode encode

# Trim by timestamps with shorthand
videotrim trim input.mp4 output.mp4 --start-time 00:10 --end-time 00:30

# Fast copy mode (no re-encoding, may not be frame-accurate)
videotrim trim input.mp4 output.mp4 -s 100 -e 500 --mode copy

# High quality encode
videotrim trim input.mp4 output.mp4 -s 100 -e 500 --mode encode --quality 10

# Auto mode (automatically choose copy or encode)
videotrim trim input.mp4 output.mp4 -s 100 -e 500 --mode auto

Extract frames

# Extract all frames as PNG
videotrim extract input.mp4 frames/

# Extract specific range
videotrim extract input.mp4 frames/ --start 100 --end 500

# Extract every 10th frame
videotrim extract input.mp4 frames/ --step 10

# Extract as JPEG with custom prefix
videotrim extract input.mp4 frames/ --format jpg --prefix frame_

Concatenate videos

# Fast concatenation (copy mode)
videotrim concat output.mp4 part1.mp4 part2.mp4 part3.mp4

# With re-encoding for compatibility
videotrim concat output.mp4 part1.mp4 part2.mp4 --mode encode

Python API

Basic trimming

from videotrim import trim_video, TrimMode

# Trim with frame-accurate encoding
trim_video(
    "input.mp4",
    "output.mp4",
    start_frame=100,
    end_frame=500,
    mode=TrimMode.ENCODE
)

# Fast copy mode
trim_video(
    "input.mp4",
    "output.mp4",
    start_frame=100,
    end_frame=500,
    mode=TrimMode.COPY
)

Video I/O

from videotrim import VideoReader, VideoWriter

# Read video
with VideoReader("input.mp4") as reader:
    print(f"FPS: {reader.fps}")
    print(f"Frames: {reader.frame_count}")
    print(f"Resolution: {reader.width}x{reader.height}")

    # Read specific frame
    frame = reader.read_frame(42)

    # Read frame range
    frames = reader.read_frames(100, 200)

    # Iterate through all frames
    for frame in reader:
        process_frame(frame)

# Write video
with VideoWriter("output.mp4", fps=30.0, quality=8) as writer:
    for frame in frames:
        writer.write_frame(frame)

Frame extraction

from videotrim import extract_frames

# Extract all frames
num_frames = extract_frames("input.mp4", "frames/")

# Extract every 10th frame
num_frames = extract_frames(
    "input.mp4",
    "frames/",
    step=10,
    format="png"
)

Video concatenation

from videotrim import concatenate_videos, TrimMode

# Concatenate multiple videos
concatenate_videos(
    ["part1.mp4", "part2.mp4", "part3.mp4"],
    "full.mp4",
    mode=TrimMode.COPY
)

Utility functions

from videotrim.utils import (
    get_video_info,
    frame_to_timestamp,
    timestamp_to_frame,
    parse_time_string,
    format_timestamp
)

# Get video metadata
info = get_video_info("input.mp4")
print(info)

# Convert between frames and timestamps
timestamp = frame_to_timestamp(150, fps=30.0)  # 5.0 seconds
frame = timestamp_to_frame(5.0, fps=30.0)  # 150

# Parse time strings
seconds = parse_time_string("01:23:45.5")  # 5025.5

# Format timestamps
time_str = format_timestamp(5025.5)  # "01:23:45.500"

Development

Running from Source

# Clone and enter directory
git clone https://github.com/talmolab/videotrim.git
cd videotrim

# Install in development mode with dev dependencies
uv pip install -e ".[dev]"

# Run the CLI
python -m videotrim --help
videotrim --help  # After installation

Running Tests

# Install dev dependencies if not already installed
uv pip install -e ".[dev]"

# Run tests
pytest

# Run with coverage
pytest --cov=videotrim --cov-report=html

# Run specific test file
pytest tests/test_io.py

# Run with verbose output
pytest -v

Code Quality

# Format and lint with ruff
ruff check src/ tests/
ruff format src/ tests/

# Auto-fix issues
ruff check --fix src/ tests/

Requirements

  • Python e 3.12
  • numpy
  • imageio
  • imageio-ffmpeg
  • av (PyAV)
  • click

Optional:

  • ffmpeg (for fast copy mode trimming and concatenation)

Architecture

videotrim is built with a modular architecture:

  • videotrim.io: Core video I/O with VideoReader and VideoWriter classes
  • videotrim.trim: Trimming operations with multiple modes (copy/encode/auto)
  • videotrim.utils: Utility functions for time/frame conversions and validation
  • videotrim.cli: Command-line interface built with Click

The library uses imageio with PyAV backend for frame-accurate video operations, and optionally uses ffmpeg directly for ultra-fast copy mode operations.

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

videotrim-0.1.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

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

videotrim-0.1.0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file videotrim-0.1.0.tar.gz.

File metadata

  • Download URL: videotrim-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.14

File hashes

Hashes for videotrim-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1948946c861a269208df07e345c9efaf152dc5c19d65c995cb88aba01316b591
MD5 46ff1ed43be0c3afc74a7832c9e119a4
BLAKE2b-256 3c3a93ef35bc143fe7c047745fab25a89b8c3cb001f46e38bb11c01bc5fd6f1c

See more details on using hashes here.

File details

Details for the file videotrim-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: videotrim-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.14

File hashes

Hashes for videotrim-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0eefc8597da2a75ad61c2ad7e6ec34a41e02d5abd9c6ee2a12ec13c3042d9e75
MD5 b4b4016cc9bcd846f9445e24e2e78813
BLAKE2b-256 f1374960f1356bd6b6dbe81b0255e7a08b783b9af8b99943d96c1aae1bfbd44e

See more details on using hashes here.

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