FFmpeg 没有正确裁剪视频

FFmpeg not cropping video correctly

我将 ffmpeg 用作:

ffmpeg -i "video.webm" -c:v libx264 -vf "crop=in_w:in_h:in_w/2:in_h" -ss 00:02:56.000 -to 00:03:02.750 cut.mp4 -y

使屏幕的 侧裁剪出右侧,但结果我得到的是未裁剪的视频(两边 都存在),当我更改命令以获取右侧(裁剪掉左侧)时:

ffmpeg -i "video.webm" -c:v libx264 -vf "crop=in_w/2:in_h:in_w:in_h" -ss 00:2:56.000 -to 00:03:02.750 cut.mp4 -y

一切正常(“cut.mp4”中只有 右侧 的原始视频出现)。

我做错了什么?

P.S.

ffmpeg version 4.4.1 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 11.2.1 (Gentoo 11.2.1_p20220115 p4) 20220115
  configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --docdir=/usr/share/doc/ffmpeg-4.4.1-r5/html --mandir=/usr/share/man --enable-shared --cc=x86_64-pc-linux-gnu-gcc --cxx=x86_64-pc-linux-gnu-g++ --ar=x86_64-pc-linux-gnu-ar --nm=x86_64-pc-linux-gnu-nm --strip=x86_64-pc-linux-gnu-strip --ranlib=x86_64-pc-linux-gnu-ranlib --pkg-config=x86_64-pc-linux-gnu-pkg-config --optflags='-march=native -O2 -pipe' --extra-libs= --disable-static --enable-avfilter --enable-avresample --disable-stripping --disable-optimizations --disable-libcelt --enable-nonfree --disable-indev=v4l2 --disable-outdev=v4l2 --disable-indev=alsa --disable-indev=oss --disable-indev=jack --disable-indev=sndio --disable-outdev=alsa --disable-outdev=oss --disable-outdev=sndio --enable-version3 --enable-bzlib --enable-runtime-cpudetect --disable-debug --disable-gcrypt --enable-gnutls --enable-gmp --enable-gpl --disable-hardcoded-tables --enable-iconv --disable-libxml2 --enable-lzma --enable-network --disable-opencl --enable-openssl --enable-postproc --disable-libsmbclient --disable-ffplay --disable-sdl2 --disable-vaapi --disable-vdpau --disable-vulkan --disable-xlib --disable-libxcb --disable-libxcb-shm --disable-libxcb-xfixes --enable-zlib --disable-libcdio --disable-libiec61883 --disable-libdc1394 --disable-libcaca --disable-openal --disable-opengl --disable-libv4l2 --disable-libpulse --disable-libdrm --disable-libjack --disable-libopencore-amrwb --disable-libopencore-amrnb --disable-libcodec2 --enable-libdav1d --disable-libfdk-aac --disable-libopenjpeg --disable-libbluray --disable-libgme --disable-libgsm --disable-libaribb24 --disable-mmal --disable-libmodplug --disable-libopus --disable-libilbc --disable-librtmp --disable-libssh --disable-libspeex --disable-libsrt --enable-librsvg --disable-ffnvcodec --disable-libvorbis --disable-libvpx --disable-libzvbi --disable-appkit --disable-libbs2b --disable-chromaprint --disable-cuda-llvm --disable-libflite --disable-frei0r --disable-libvmaf --disable-libfribidi --disable-fontconfig --disable-ladspa --disable-libass --disable-libtesseract --disable-lv2 --enable-libfreetype --disable-libvidstab --disable-librubberband --disable-libzmq --disable-libzimg --disable-libsoxr --enable-pthreads --disable-amf --disable-libvo-amrwbenc --disable-libkvazaar --disable-libaom --disable-libmp3lame --disable-libopenh264 --disable-librav1e --disable-libsnappy --disable-libsvtav1 --disable-libtheora --disable-libtwolame --enable-libwebp --enable-libx264 --disable-libx265 --disable-libxvid --disable-gnutls --disable-armv5te --disable-armv6 --disable-armv6t2 --disable-neon --disable-vfp --disable-vfpv3 --disable-armv8 --disable-mipsdsp --disable-mipsdspr2 --disable-mipsfpu --disable-altivec --disable-vsx --disable-power8 --disable-amd3dnow --disable-amd3dnowext --disable-fma3 --disable-fma4 --disable-xop --cpu=host --disable-doc --disable-htmlpages --enable-manpages
  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
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  9.100 /  5.  9.100
  libswresample   3.  9.100 /  3.  9.100
  libpostproc    55.  9.100 / 55.  9.100
Input #0, matroska,webm, from 'video.webm':
  Metadata:
    ENCODER         : Lavf58.76.100
  Duration: 00:36:29.58, start: -0.007000, bitrate: 1066 kb/s
  Stream #0:0(eng): Video: vp9 (Profile 0), yuv420p(tv, bt709), 1920x1080, SAR 1:1 DAR 16:9, 25 fps, 25 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      DURATION        : 00:36:29.560000000
  Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:36:29.581000000
Stream mapping:
  Stream #0:0 -> #0:0 (vp9 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (opus (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x564b4eda52c0] using SAR=1/1
[libx264 @ 0x564b4eda52c0] using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX
[libx264 @ 0x564b4eda52c0] profile Progressive High, level 4.0, 4:2:0, 8-bit
[libx264 @ 0x564b4eda52c0] 264 - core 157 - H.264/MPEG-4 AVC codec - Copyleft 2003-2019 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=15 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'cut.mp4':
  Metadata:
    encoder         : Lavf58.76.100
  Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 25 fps, 12800 tbn (default)
    Metadata:
      DURATION        : 00:36:29.560000000
      encoder         : Lavc58.134.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
  Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      DURATION        : 00:36:29.581000000
      encoder         : Lavc58.134.100 aac
frame=  169 fps= 28 q=-1.0 Lsize=     851kB time=00:00:06.74 bitrate=1033.9kbits/s speed=1.12x
video:738kB audio:106kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.764865%
[libx264 @ 0x564b4eda52c0] frame I:1     Avg QP:14.29  size: 31413
[libx264 @ 0x564b4eda52c0] frame P:43    Avg QP:16.65  size:  9476
[libx264 @ 0x564b4eda52c0] frame B:125   Avg QP:22.36  size:  2529
[libx264 @ 0x564b4eda52c0] consecutive B-frames:  1.2%  0.0%  1.8% 97.0%
[libx264 @ 0x564b4eda52c0] mb I  I16..4: 31.6% 63.8%  4.6%
[libx264 @ 0x564b4eda52c0] mb P  I16..4:  1.4%  5.4%  0.3%  P16..4: 13.8%  3.2%  1.0%  0.0%  0.0%    skip:75.0%
[libx264 @ 0x564b4eda52c0] mb B  I16..4:  0.1%  0.2%  0.0%  B16..8: 11.4%  0.7%  0.1%  direct: 0.4%  skip:87.0%  L0:48.2% L1:48.1% BI: 3.7%
[libx264 @ 0x564b4eda52c0] 8x8 transform intra:73.0% inter:90.2%
[libx264 @ 0x564b4eda52c0] coded y,uvDC,uvAC intra: 38.9% 41.7% 6.4% inter: 2.2% 2.7% 0.0%
[libx264 @ 0x564b4eda52c0] i16 v,h,dc,p: 45% 18%  5% 31%
[libx264 @ 0x564b4eda52c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 20% 22%  5%  6%  7%  5%  7%  5%
[libx264 @ 0x564b4eda52c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 22% 14%  6% 10%  8%  7%  5%  3%
[libx264 @ 0x564b4eda52c0] i8c dc,h,v,p: 55% 19% 19%  6%
[libx264 @ 0x564b4eda52c0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x564b4eda52c0] ref P L0: 67.6%  9.2% 16.0%  7.3%
[libx264 @ 0x564b4eda52c0] ref B L0: 88.6%  9.5%  1.9%
[libx264 @ 0x564b4eda52c0] ref B L1: 97.3%  2.7%
[libx264 @ 0x564b4eda52c0] kb/s:893.47
[aac @ 0x564b4edde600] Qavg: 208.505

我认为您对 the crop filter options 很困惑。以下是前 4 个选项的说明:

w, out_w The width of the output video. It defaults to iw. This expression is evaluated only once during the filter configuration, or when the ‘w’ or ‘out_w’ command is sent.

h, out_h The height of the output video. It defaults to ih. This expression is evaluated only once during the filter configuration, or when the ‘h’ or ‘out_h’ command is sent.

x The horizontal position, in the input video, of the left edge of the output video. It defaults to (in_w-out_w)/2. This expression is evaluated per-frame.

y The vertical position, in the input video, of the top edge of the output video. It defaults to (in_h-out_h)/2. This expression is evaluated per-frame.

  1. 如果要将宽度减半,那么第一个选项必须是 in_w/2,无论从哪一侧裁剪。
  2. 高度不变,所以一直用in_h
  3. 要从左侧裁剪,x 偏移必须与宽度匹配,因此 in_w/2。要从右侧裁剪,不会删除左侧边缘的任何像素,因此必须 0.
  4. 因为没有删除任何行,所以使用y = 0.

总结一下:

  • 裁剪左边缘:crop=in_w/2:in_h:in_w/2:0
  • 裁剪右边缘:crop=in_w/2:in_h:0:0