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
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) |
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).
- Requirements
- pip:
pip install -r requirements.txt
- If you use Conda:
conda install --file requirements.txt
- pip:
- If you'd like to use the PyAV backend
- FFMPEG (if not installed):
bash util/install_ffmpeg_av1_x265.sh $HOME/ffmpeg
- 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 runsource $HOME/.bashrc
)export LD_LIBRARY_PATH="$HOME/ffmpeg/build/lib:$LD_LIBRARY_PATH"
export PKG_CONFIG_PATH="$HOME/ffmpeg/build/lib/pkgconfig:$PKG_CONFIG_PATH"
- Install PyAV:
pip install av --no-binary av --install-option="--ffmpeg-dir=$HOME/ffmpeg/build/"
- FFMPEG (if not installed):
- Install PyCodecs
- Clone:
git clone https://github.com/kloppjp/pycodecs.git
- Setup:
cd pycodecs; pip install .
(use-e
to install in developer mode)
- Clone:
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
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 ornumpy.ndarray
of dimensionHxWxC
orCxHxW
typednumpy.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 bytesrestored
restored image (RGB24numpy.ndarray
in same dimensionality asoriginal
), only ifdecoded
is not supplied. If not provided, a temporary file is used.
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")