Skip to main content

HlsKit provides strong HLS video conversion features backed by ffmpeg. Prepare your mp4 files for streaming

Project description

HlsKit-Py

PyPI version License: MIT Lint: Ruff

HlsKit-Py is the Python version of hlskit-rs, an asyncio-based video processing library for converting video bytes into HLS (HTTP Live Streaming) output. Built on top of pydantic and asyncio, it provides a fast, type-safe, and scalable interface for video transcoding tasks.


๐Ÿ“ฆ Installation

pip install hlskit-py

๐Ÿงฌ Dependencies

HlsKit-Py uses the following core dependencies:

  • pydantic โ€” for data validation and settings modeling.
  • ruff โ€” for code linting and formatting.

๐Ÿคช Example

import asyncio
from hlskit_py import HlsVideoProcessingSettings, HlsVideoProcessor

async def process_videos():
    input_path = "example/sample.mp4"
    with open(input_path, "rb") as video_binary:
        input_bytes = video_binary.read()

    results = await HlsVideoProcessor.process_video(
        input_bytes=input_bytes,
        output_profiles=[
            HlsVideoProcessingSettings(resolution=(1920, 1080), constant_rate_factor=28),
            HlsVideoProcessingSettings(resolution=(1280, 720), constant_rate_factor=28),
            HlsVideoProcessingSettings(resolution=(854, 480), constant_rate_factor=28),
        ],
    )

    # Do something with `results.output_profiles`
    print(results)

if __name__ == "__main__":
    asyncio.run(process_videos())

๐ŸŒ Web API + S3 Integration

import boto3
import uuid
from fastapi import FastAPI, UploadFile, File
from hlskit_py import HlsVideoProcessor, HlsVideoProcessingSettings

app = FastAPI()
s3 = boto3.client("s3")
BUCKET_NAME = "your-s3-bucket"

@app.post("/upload")
async def upload_and_process_video(file: UploadFile = File(...)):
    input_bytes = await file.read()

    results = await HlsVideoProcessor.process_video(
        input_bytes=input_bytes,
        output_profiles=[
            HlsVideoProcessingSettings(resolution=(1920, 1080), constant_rate_factor=28),
            HlsVideoProcessingSettings(resolution=(1280, 720), constant_rate_factor=28),
            HlsVideoProcessingSettings(resolution=(854, 480), constant_rate_factor=28),
        ],
    )

    uploaded_files = []
    for profile in results.output_profiles:
        for relative_path, content in profile.output_files.items():
            key = f"{uuid.uuid4()}/{relative_path}"
            s3.upload_fileobj(content, BUCKET_NAME, key)
            uploaded_files.append(f"s3://{BUCKET_NAME}/{key}")

    return {"message": "Upload successful", "files": uploaded_files}

๐Ÿง  Processing Flow (Mermaid Diagram)

flowchart TD
    A[Input MP4 Bytes] --> B{For Each Resolution}
    B -->|1080p| C1[Spawn ffmpeg Task]
    B -->|720p| C2[Spawn ffmpeg Task]
    B -->|480p| C3[Spawn ffmpeg Task]
    C1 & C2 & C3 --> D[Generate .ts Segments & .m3u8 Playlists]
    D --> E[Generate Master Playlist - master.m3u8 file]
    E --> F[Return HlsVideo Struct]

๐Ÿ“ Project Structure

hlskit-py/
โ”œโ”€โ”€ src/hlskit_py/
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”œโ”€โ”€ hls_video_processing_settings.py
โ”‚   โ”‚   โ””โ”€โ”€ hls_video.py
โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ””โ”€โ”€ hls_video_processor.py
โ”‚   โ””โ”€โ”€ tools/
โ”‚       โ”œโ”€โ”€ ffmpeg_command_builder.py
โ”‚       โ””โ”€โ”€ m3u8_tools.py
โ”œโ”€โ”€ example/
โ”‚   โ””โ”€โ”€ example.py
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ CONTRIBUTING.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ pyproject.toml

๐Ÿ“Œ Future Goals

  • Implement GStreamer backend.
  • Add GPU encoding (NVIDIA/AMD) support.

๐Ÿค Contributing

See CONTRIBUTING.md for contribution workflow, testing practices, coding style, and PR conventions.


๐Ÿชช License

HlsKit is licensed under LGPLv3. By modifying or distributing it (e.g., via forks or extensions), you agree to the HlsKit Contributor License Agreement (CLA), which ensures our ecosystem thrives.

The 'HlsKit' name and logo are trademarks of Engels Tercero. Use in forks or derivatives requires written permission.

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

hlskit_py-0.1.3.tar.gz (61.7 MB view details)

Uploaded Source

Built Distribution

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

hlskit_py-0.1.3-py3-none-any.whl (34.3 kB view details)

Uploaded Python 3

File details

Details for the file hlskit_py-0.1.3.tar.gz.

File metadata

  • Download URL: hlskit_py-0.1.3.tar.gz
  • Upload date:
  • Size: 61.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for hlskit_py-0.1.3.tar.gz
Algorithm Hash digest
SHA256 af8b56249b4880503ace6f831205750b86f1d9a9ca185c46ced33a729f962b75
MD5 07ef8364c4d720406615f96655f4999d
BLAKE2b-256 97918b4511547cd28770e04dca29d9e65cfd660af1ee4e8eae5f207c0ffc65ad

See more details on using hashes here.

File details

Details for the file hlskit_py-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: hlskit_py-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 34.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.13

File hashes

Hashes for hlskit_py-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b51b5970253912449d7e616170bf36bed61137fb1ae1aff502bc45be5b7e0b64
MD5 c8f6ceccf8033038ea495d3bd047bc8e
BLAKE2b-256 a08870206edb94135d8e5eafe26e3670b78ce1f31a6b9c4f5f2fb626aa1409b1

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