Search code examples
linuxffmpegvideo-processinghevclibx265

ffmpeg auto scaling problem - Picture width must be an integer multiple of the specified chroma subsampling


Apparently one of my input files have a wonky aspect ratio and it lands on a "non - integer" auto scaled width. which produces an error message about chroma multiples (naturally)

ffmpeg -i "1.mp4" -vf "scale=-1:1920" -c:v libx265 -c:a aac -preset slow -x265-params "crf=22:min-keyint=25:keyint=50" "2.mp4"


ffmpeg version 4.4-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 8 (Debian 8.3.0-6)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg
  libavutil      56. 70.100 / 56. 70.100
  libavcodec     58.134.100 / 58.134.100
  libavformat    58. 76.100 / 58. 76.100
  libavdevice    58. 13.100 / 58. 13.100
  libavfilter     7.110.100 /  7.110.100
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.65.101
  Duration: 00:46:25.43, start: 0.000000, bitrate: 29507 kb/s
  Stream #0:0(eng): Video: hevc (Main) (hev1 / 0x31766568), yuv420p(tv, progressive), 5120x2880 [SAR 1:1 DAR 16:9], 29238 kb/s, 59.94 fps, 59.94 tbr, 60k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : ?Mainconcept Video Media Handler
      vendor_id       : [0][0][0][0]
  Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 257 kb/s (default)
    Metadata:
      handler_name    : #Mainconcept MP4 Sound Media Handler
      vendor_id       : [0][0][0][0]
File '2.mp4' already exists. Overwrite? [y/N] y
Stream mapping:
  Stream #0:0 -> #0:0 (hevc (native) -> hevc (libx265))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
x265 [info]: HEVC encoder version 3.5+1-f0c1022b6
x265 [info]: build info [Linux][GCC 8.3.0][64 bit] 8bit+10bit+12bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x265 [error]: Picture width must be an integer multiple of the specified chroma subsampling
[libx265 @ 0x58180c0] Cannot open libx265 encoder.
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
[aac @ 0x58a7e00] Qavg: 12426.497
[aac @ 0x58a7e00] 2 frames left in the queue on closing

I think what's happening is ffmpeg internally converts "5120x2880" resolution to "3413.33x1920" hence the error message. Is there a way to tell ffmpeg to round up/down to nearest "chroma acceptable ratio" (for yuv420 this 2) These files are converted via a script and it does not really matter if the width is a few pixels longer or shorter.


Solution

  • @Gyan's answer on the comments is correct.

    Only after seeing this answer I've scrolled to the relevant section of the help page: http://trac.ffmpeg.org/wiki/Scaling#KeepingtheAspectRatio

    Some codecs require the size of width and height to be a multiple of n. You can achieve this by setting the width or height to -n:

    ffmpeg -i input.jpg -vf scale=320:-2 output_320.png

    The output will now be 320x206 pixels.