Coder Social home page Coder Social logo

pycodecs's Introduction

PyCodecs

PyCodecs: A simple (image) codec interface. In early alpha stage.

New in version 0.2.3: PyAV can be used as backend to reduce latency when making calls to ffmpeg

Capabilities

The (for now) supported codecs are listed below. Only image coding is supported. Some codecs can be supplied with data via IPC (pipe) or direct call (via PyAV), so you can encode and decode directly from and to memory, i.e. a numpy.ndarray doesn't have to be saved to disk first.

Codec Backend Info
WebP syscall https://developers.google.com/speed/webp
BPG/H265 syscall https://bellard.org/bpg/
X265 ffmpeg (pipe) / pyav (direct) http://x265.org/
X264 ffmpeg (pipe) / pyav (direct) http://x264.org/
AV1 ffmpeg (pipe) / pyav (direct) https://aomedia.org/av1-features/get-started/
MJPEG ffmpeg (pipe)
JPEG2000 ffmpeg (pipe)
JPEG ImageIO/PIP (direct)

Caveats

While there are certainly some bugs hidden and the API design isn't final, the size estimates for the AV1 code are too high, because the bitstream is wrapped in FFMPEG's NUT format. This is to be fixed soon. X265 writes raw hevc format on the contrary (you can enforce NUT for comparison, though).

Install

  1. Requirements
    1. pip: pip install -r requirements.txt
    2. If you use Conda: conda install --file requirements.txt
  2. If you'd like to use the PyAV backend
    1. FFMPEG (if not installed): bash util/install_ffmpeg_av1_x265.sh $HOME/ffmpeg
    2. Add paths to libraries to your environment (so that PyAV can find them), you can add these lines to $HOME/.bashrc (don't forget to run source $HOME/.bashrc)
      1. export LD_LIBRARY_PATH="$HOME/ffmpeg/build/lib:$LD_LIBRARY_PATH"
      2. export PKG_CONFIG_PATH="$HOME/ffmpeg/build/lib/pkgconfig:$PKG_CONFIG_PATH"
    3. Install PyAV: pip install av --no-binary av --install-option="--ffmpeg-dir=$HOME/ffmpeg/build/"
  3. Install PyCodecs
    1. Clone: git clone https://github.com/kloppjp/pycodecs.git
    2. Setup: cd pycodecs; pip install . (use -e to install in developer mode)

Note that you can also use PyAV build with FFMPEG by installing pip install av, but I noticed that the crf rate control didn't work for AV1, so I use my own ffmpeg build.

If you want to use BPG, you need to install it:

bash util/install_bpg.sh

Use

Basic usage is simple, if you want to apply a codec, use the n_bytes, restored = codec.apply(original, encoded, decodec, quality) method.

  • original path to image file or numpy.ndarray of dimension HxWxC or CxHxW typed numpy.uint8 in RGB24 format
  • (optional) encoded path where the encoded file should be stored. If not provided, a temporary file is used.
  • (optional) decoded path where the decoded file should be stored. If not provided, a temporary file is used.
  • (optional) quality quality index (int) to use. Otherwise the codec's setting is used.
  • n_bytes size of the encoded bit stream in bytes
  • restored restored image (RGB24 numpy.ndarray in same dimensionality as original), only if decoded is not supplied. If not provided, a temporary file is used.

Examples

Take a look at examples/example.py or just run it with

python examples/example.py

Optionally, you can specify the ffmpeg path via --ffmpeg_path, the backend with --ffmpeg_backend (either ffmpeg or pyav) and the image path with the --image option.

Alternatively, as simple as this:

import pycodecs
x265 = pycodecs.X265(backend='pyav')
if x265.available():
    x265.apply(original="example.png", quality=37, encoded=f"example_37.{x265.file_extension}",
    decoded="example_decoded_37.png")

pycodecs's People

Contributors

kloppjp avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.