Skip to main content

uppercut-rawvideo-encoder

Pipes RGB frames from a Pillow (or numpy) render into ffmpeg, which writes H.264 in yuv420p. It checks ffmpeg's exit code and raises with the end of ffmpeg's error output, so a failed encode is an exception instead of the path of a file that was never written. It also carries the two helpers render scripts keep copying: nearest() for whole-number pixel upscales and a cached font() loader.

Install

pip install uppercut-rawvideo-encoder

Requires Python 3.10 or newer and Pillow 10 or newer. ffmpeg with libx264 must be on your PATH.

Example

from PIL import Image, ImageDraw
from uppercut_rawvideo_encoder import RawVideoEncoder, font, nearest

W, H = 1920, 1080
label = font("C:/Windows/Fonts/consolab.ttf", 48)  # loaded once, shared after that

with RawVideoEncoder("out.mp4", (W, H), 60, crf=16, preset="medium") as enc:
    for i in range(120):
        sprite = nearest(Image.open("sprite.png").convert("RGB"), 4)
        frame = Image.new("RGB", (W, H))
        frame.paste(sprite, (40 + i * 4, 200))
        ImageDraw.Draw(frame).text((40, 40), f"frame {i}", font=label, fill="white")
        enc.write(frame)

Leaving the with block normally finishes the file and raises EncodeError if ffmpeg exits with a nonzero code, or if no frame was written. If your own code raises inside the block, ffmpeg is stopped, the partial file is deleted and your exception goes through unchanged.

fps, crf and preset have no defaults. Each caller says what it wants, so changing to this package never changes a render's quality, and a video never gets a frame rate nobody chose.

RawVideoEncoder

RawVideoEncoder(path, size, fps, *, crf, preset, extra_inputs=(), output_args=(), ffmpeg="ffmpeg", tail_bytes=4096)

Argument Meaning
path The output file. Its folder must exist.
size (width, height) of every frame.
fps Frames per second as an int, float, Fraction or "num/den" such as "24000/1001". Required.
crf, preset The x264 settings. Required.
extra_inputs ffmpeg arguments for more inputs, placed after the frame input.
output_args More ffmpeg output arguments, placed after the x264 settings and before the file name.
ffmpeg The executable, or a command prefix as a list.
tail_bytes How much of ffmpeg's error output goes into EncodeError.
Member What it does
write(frame) Adds a frame: a Pillow image of the video's size (converted to RGB when it is not), or exactly width x height x 3 bytes, as bytes or any object with tobytes() such as a numpy array. A wrong size raises ValueError.
close() Finishes the file. Raises EncodeError on a nonzero exit or when nothing was written.
abort() Stops ffmpeg and deletes the partial file.
frames How many frames have been written.
command The ffmpeg argument list that was run.

The command always carries -nostdin, so ffmpeg never reads keystrokes from the terminal, and -y, so an existing file is replaced. It reads rgb24 from stdin at the rate you gave and writes libx264 with -pix_fmt yuv420p.

EncodeError has returncode, stderr_tail (the end of ffmpeg's error output) and frames. If ffmpeg dies while you are still writing, the next write raises it, instead of a bare BrokenPipeError. A failed encode deletes its output file.

ffmpeg_command(path, size, fps, *, crf, preset, extra_inputs=(), output_args=(), ffmpeg="ffmpeg") returns the argument list without running it. format_fps(fps) is the text that goes after -r.

A soundtrack

with RawVideoEncoder(
    "cut.mp4", (1920, 1080), 24, crf=14, preset="slow",
    extra_inputs=["-ss", "12.5", "-t", "30", "-i", "song.wav"],
    output_args=["-map", "0:v", "-map", "1:a", "-c:a", "aac", "-b:a", "256k", "-shortest"],
) as enc:
    ...

Pixel helpers

Function What it does
nearest(img, n) Scales an image up by the whole number n with no smoothing.
font(path, size, *, index=0) ImageFont.truetype, cached by file, size and index. font.cache_clear() empties the cache. index picks a face inside a .ttc collection.

License

MIT

Metadata

Release files for uppercut-rawvideo-encoder 0.1.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 uppercut-rawvideo-encoder 0.1.0
File Size Uploaded Provenance Actions
uppercut_rawvideo_encoder-0.1.0.tar.gz 8.8 kB Without provenance Details for uppercut_rawvideo_encoder-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for uppercut-rawvideo-encoder 0.1.0
File Interpreter ABI Platform Actions
uppercut_rawvideo_encoder-0.1.0-py3-none-any.whl Python 3 none any Details for uppercut_rawvideo_encoder-0.1.0-py3-none-any.whl

Total release size: 16.6 kB

Release files / uppercut_rawvideo_encoder-0.1.0.tar.gz

Download URL uppercut_rawvideo_encoder-0.1.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cfc557e1eeca997f2edb77053dd170ea0bd1f9d506fdb288663ff74c348e7646
BLAKE2b-256 checksum
How to use checksums
01691d27c6640eb8b87370978a8031e10ddc6b937261d876565567c73e3de7f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / uppercut_rawvideo_encoder-0.1.0-py3-none-any.whl

Download URL uppercut_rawvideo_encoder-0.1.0-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5458cc4b1ee78f52c5d3a9a9d17c0a0270dfa5533f0bb098c4aab625a0b9dbc8
BLAKE2b-256 checksum
How to use checksums
792b4bf9b0b89195ec9a0a2e2c10cde15ee1782946f4fc3b28fd342c30907cbf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for uppercut-rawvideo-encoder 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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