2

I have an animation rendered as a series of png images, and I want to convert it losslessly into an H.264 mp4 video. The images are in the RGB color space.

I tried these options:

ffmpeg -framerate 24 -i "animation.%04d.png" -c:v libx264 -preset veryslow -tune animation -crf 0 "test video.mp4"

But the output file had messed up colors, possibly because of an improper RGB to YUV conversion.

Then I tried these options:

ffmpeg -framerate 24 -i "animation.%04d.png" -c:v libx264rgb -preset veryslow -tune animation -crf 0 "test video.mp4"

The quality of the output video file was the same as that of the original png images, that is the encoding was indeed lossless, but the output file was encoded in the RGB color space, and thus it is not compatible with video editing software (such as Avidemux, I did not test other programs, but I suppose it will be the same).

How do I resolve this?

Is there a way to improve the RGB to YUV conversion, or even to make it lossless?

Or is there a way to improve the compatibility of RGB encoded videos?

1 Answers1

0

I've been banging my head against the wall with this for hours today. A friend found a workaround.

ffmpeg -r 24 -i "animation.%04d.png" -c:v libaom-av1 -aom-params lossless=1 "video.mp4"

Unfortunately, this is not h264 but av1, but it does still create an MP4 file, is actually lossless, and, at least for me, the file can be played on all the devices I care about.

Other candidates, in case it's helpful for other people:

  • -c:v libx264 -crf 0 (line 1 in original question): This automatically selects pix_fmt yuv444p and, while I've heard that it should be lossless, it actually isn't. Rgb 255,0,0 is mangled to 254,0,0. But it doesn't produce glaring compression artifacts, so I'm calling this "almost lossless".
  • -c:v libx264 -crf 0 -pix_fmt rgb24: Almost lossless
  • -c:v libx264rgb -crf 0 and -c:v libx264rgb -crf 0 -pix_fmt rgb24: Completely lossless, but most devices can't play it.
  • -c:v libvpx-vp9 -lossless 1 and -c:v libvpx-vp9 -lossless 1 -qmin 0 -qmax 0: Very bad. Also see https://trac.ffmpeg.org/ticket/4246
  • -c:v ffv1: Completely lossless, but most devices can't play it.
  • -filter_complex "[0:v:0]palettegen[p];[0:v:0][p]paletteuse" (creating a .gif file): If you have less than 256 distinct colors, this is actually lossless, so I'm mentioning it here.

The losslessness can be confirmed by extracting the frames with ffmpeg -i "video.mp4" -q:v 1 -f image2 -y "roundtrip.%04d.png" and I used Beyond Compare 4 to diff the pixels.

Also note that, for some reason, some media players show lots of compression artifacts even when the actual file is lossless. PotPlayer does this for me. Don't let that confuse you.

Niko O
  • 225