ffmpeg调整分辨率height not divisible by 2问题解决

Author Avatar
呃哦 10月 12, 2019

问题

调整视频分辨率时候,以下命令

ffmpeg -i a.mp4 -y -c:v libx264 -movflags +faststart -vf scale=640:-1 -c:a copy b.mp4
  • -i 表示输入视频
  • -y 不询问,默认执行
  • -c:v libx264 指定编解码器为 x264
  • -movflags +faststart 输出为 fragment MP4

  • -vf 使用视频滤镜

  • scale=640:-1 缩放视频,宽度为640,高度按原宽高比例自适应

  • -c:a copy 复制音频

执行后报错:

ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.4_1 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'a.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.20.4
  Duration: 00:03:19.12, start: 0.000000, bitrate: 2612 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x654 [SAR 1:1 DAR 640:327], 2417 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 188 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libx264 @ 0x7fc73d804600] height not divisible by 2 (640x327)
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
Conversion failed!

解决

问题在于 height not divisible by 2

原视频的分辨率为 1280x654

自适应缩放到640后为 640x327

查资料发现 x264 编解码器要求视频的分辨率都是偶数。

因此,ffmpeg的命令修改为

ffmpeg -i a.mp4 -y -c:v libx264 -movflags +faststart -vf scale=640:-2 -c:a copy b.mp4

主要为 scale 修改为 640:-2

根据官网文档介绍

If one and only one of the values is -n with n >= 1, the scale filter will use a value that maintains the aspect ratio of the input image, calculated from the other specified dimension. After that it will, however, make sure that the calculated dimension is divisible by n and adjust the value if necessary.

如果宽高值设置为负数,则表示对应的值取按比例缩放后的自适应值,且能被n整除。

因此,取 640:-2 表示宽度为640,高度自适应后能被2整除的值。则为 640x328