ffmpegmjpegh.265

ffmpeg mjpeg -> h.265 smeared color on output video file


I am converting some old mjpeg videos (stored in .avi container) to h.265 (.mp4 container) but am noticing the colors are smearing. Using the terminal command:

ffmpeg -y -i "input-file.avi" -c:v libx265 -vtag hvc1 "output-file.mp4"

I get the following image (notice how the red and blue are stretched donward). There is a lot of motion in the scene, but the motion is mostly horizontal:

enter image description here

Any idea what might cause this? The detail and resolution seem fine, just the colors are being interpreted weirdly.

Full output:

ffmpeg version N-105859-g37480b1b85-20220305 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 11.2.0 (crosstool-NG 1.24.0.533_681aaef)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librist --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20220305
  libavutil      57. 22.100 / 57. 22.100
  libavcodec     59. 21.103 / 59. 21.103
  libavformat    59. 17.102 / 59. 17.102
  libavdevice    59.  5.100 / 59.  5.100
  libavfilter     8. 27.100 /  8. 27.100
  libswscale      6.  5.100 /  6.  5.100
  libswresample   4.  4.100 /  4.  4.100
  libpostproc    56.  4.100 / 56.  4.100
Guessed Channel Layout for Input Stream #0.1 : mono
Input #0, avi, from 'E:\PATH\MVI_0168.AVI':
  Metadata:
    creation_time   : 2006-06-14 21:42:10
    software        : CanonMVI02
  Duration: 00:00:53.47, start: 0.000000, bitrate: 5203 kb/s
  Stream #0:0: Video: mjpeg (Baseline) (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 320x240, 5111 kb/s, 30 fps, 30 tbr, 30 tbn
  Stream #0:1: Audio: pcm_u8 ([1][0][0][0] / 0x0001), 11024 Hz, mono, u8, 88 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> hevc (libx265))
  Stream #0:1 -> #0:1 (pcm_u8 (native) -> aac (native))
Press [q] to stop, [?] for help
[aac @ 000001a7ad1aab00] Too many bits 6408.707483 > 6144 per frame requested, clamping to max
x265 [info]: HEVC encoder version 3.5+34-7a5709048
x265 [info]: build info [Windows][GCC 11.2.0][64 bit] 8bit+10bit+12bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x265 [info]: Main profile, Level-2 (Main tier)
x265 [info]: Thread pool created using 12 threads
x265 [info]: Slices                              : 1
x265 [info]: frame threads / pool features       : 3 / wpp(4 rows)
x265 [warning]: Source height < 720p; disabling lookahead-slices
x265 [info]: Coding QT: max CU size, min CU size : 64 / 8
x265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra
x265 [info]: ME / range / subpel / merge         : hex / 57 / 2 / 3
x265 [info]: Keyframe min / max / scenecut / bias  : 25 / 250 / 40 / 5.00
x265 [info]: Lookahead / bframes / badapt        : 20 / 4 / 2
x265 [info]: b-pyramid / weightp / weightb       : 1 / 1 / 0
x265 [info]: References / ref-limit  cu / depth  : 3 / off / on
x265 [info]: AQ: mode / str / qg-size / cu-tree  : 2 / 1.0 / 32 / 1
x265 [info]: Rate Control / qCompress            : CRF-28.0 / 0.60
x265 [info]: tools: rd=3 psy-rd=2.00 early-skip rskip mode=1 signhide tmvp
x265 [info]: tools: b-intra strong-intra-smoothing deblock sao
Output #0, mp4, to 'C:\PATH\test.mp4':
  Metadata:
    software        : CanonMVI02
    encoder         : Lavf59.17.102
  Stream #0:0: Video: hevc (hev1 / 0x31766568), yuvj422p(pc, bt470bg/unknown/unknown, progressive), 320x240, q=2-31, 30 fps, 1000k tbn
    Metadata:
      encoder         : Lavc59.21.103 libx265
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 11025 Hz, mono, fltp, 66 kb/s
    Metadata:
      encoder         : Lavc59.21.103 aac
frame= 1604 fps=187 q=35.9 Lsize=    1818kB time=00:00:53.49 bitrate= 278.4kbits/s speed=6.23x
video:1438kB audio:348kB subtitle:0kB other streams:0kB global headers:2kB muxing overhead: 1.811161%
x265 [info]: frame I:      9, Avg QP:28.81  kb/s: 1309.05
x265 [info]: frame P:    361, Avg QP:30.64  kb/s: 599.75
x265 [info]: frame B:   1234, Avg QP:35.12  kb/s: 100.04
x265 [info]: Weighted P-Frames: Y:2.2% UV:1.9%
x265 [info]: consecutive B-frames: 2.7% 1.6% 5.1% 40.5% 50.0%

encoded 1604 frames in 8.57s (187.25 fps), 219.29 kb/s, Avg QP:34.08
[aac @ 000001a7ad1aab00] Qavg: 64427.340

Solution

  • Your file seems to be missing some color information:

    Stream #0:0: Video: mjpeg (Baseline) (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 320x240, 5111 kb/s, 30 fps, 30 tbr, 30 tbn
                                                                                      ^ 
                                                                                      |
    

    You can set them manually with the following command:

    ffmpeg -i in.mkv -c:v libx265 -vtag hvc1 -filter:v "scale=in_color_matrix=auto:in_range=auto:out_color_matrix=bt709:out_range=tv" -pix_fmt:v "yuv420p" -colorspace:v "bt709" -color_primaries:v "bt709" -color_trc:v "bt709" -color_range:v "tv" -c:a copy out.mkv