1

Currently, I am applying h.264 compression to a single image.
As you can see in the picture below, I want to compress that image based on h.264 with 40 constant quantization.

enter image description here However, there are no relevant functionality anywhere, such as Python-based libraries (opencv, ffmpeg).

Also, there is no github for applying to single image and well-structured h.264 algorithm.

So, is there any github implemetation or library to do it?

Thanks in advance.

JaeJu
  • 85
  • 1
  • 11
  • 2
    Why? h.264 "is a block-oriented motion-compensation-based video compression standard". Your image doesn't have motion (frames before/after) which is required for h.264 compression to function... – sorifiend Apr 28 '21 at 08:40
  • @sorifiend. I see. h.264 codec is for video compression not for single image. – JaeJu Apr 28 '21 at 08:52
  • For what it's worth the so-called [webp still image format](https://developers.google.com/speed/webp) is basically a single-frame VP8 video. – O. Jones May 03 '21 at 14:54

1 Answers1

3

There are cases (like academic purposes) that is does make sense to encode a single frame video.

There is an existing image format named HEIF that based on HEVC (H.265) codec, but for H.264, you must encode a video file.

You may use FFmpeg command line tool for creating a single frame video file:

ffmpeg -i input.png -vcodec libx264 -qp 40 -pix_fmt yuv420p single_frame.mp4

(Assuming your input image is input.png).

The -qp 40 argument applies constant quantization 40.

qp (qp)
Set constant quantization rate control method parameter

See: libx264 documentation

Note:
Since it's a single frame, the frame type is going to be an I-Frame.


In case you prefer elementary H.264 stream (without MP4 container), you may use:

ffmpeg -i input.png -vcodec libx264 -qp 40 -pix_fmt yuv420p single_frame.264

There are Python bindings for FFmpeg, like ffmpeg-python.
You may use it, for encoding a NumPy array that represents a raw video frame.
You may also use a pipe to FFmpeg sub-process as the following sample.


You can find source code for H.264 encoder in GitHub (here for example).
As far a I know, the sources are implemented in C.

Without looking at the sources, I don't think you are going to consider them as "well-structured".

Rotem
  • 30,366
  • 4
  • 32
  • 65
  • Thanks expert. I'm sorry if my words sound little harsh :)) – JaeJu Apr 29 '21 at 07:57
  • FWIW: lower values of `qp` generate more encoded data (larger compressed files). Higher values generate pictures with more artifacts. – O. Jones May 03 '21 at 14:58