FFMPEG 在单个命令中缩放和裁剪

Posted

技术标签:

【中文标题】FFMPEG 在单个命令中缩放和裁剪【英文标题】:FFMPEG scale and crop in single command 【发布时间】:2016-03-25 14:32:34 【问题描述】:

我正在使用以下库在 android 上使用 FFMPEG:http://writingminds.github.io/ffmpeg-android-java/

这是我正在使用的命令:

"-i " + path + " -vf scale=480:360,crop=360:360:0:00 -strict -2  -preset ultrafast " + fileOutPath

我的目标是将图像缩小到 480x360,然后将其从 (0,0)(左上角)裁剪为 360x360。然而,最终发生的是它从垂直中心裁剪。因此,例如,如果您要在整个屏幕上拍摄三个块的视频,则裁剪最终将只显示中间块,而我想要顶部块。

关于我做错了什么有什么想法吗?

我的最终目标是获得一个 360x360 大小的方形视频。

输出日志:

03-26 12:10:16.816 17198-17198/com.noq D/FFMPEG: FFMPEG onStart
03-26 12:10:16.900 17198-17385/com.noq D/FFmpeg: Running publishing updates method
03-26 12:10:16.914 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: WARNING: linker: /data/user/0/com.noq/files/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
03-26 12:10:17.004 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
03-26 12:10:17.004 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   built on Oct  7 2014 15:08:46 with gcc 4.8 (GCC)
03-26 12:10:17.007 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   configuration: --target-os=linux --cross-prefix=/home/sb/Source-Code/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/sb/Source-Code/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/sb/Source-Code/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/sb/Source-Code/ffmpeg-android/build/armeabi-v7a-neon --extra-cflags='-I/home/sb/Source-Code/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all -mfpu=neon' --extra-ldflags='-L/home/sb/Source-Code/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
03-26 12:10:17.021 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libavutil      54.  7.100 / 54.  7.100
03-26 12:10:17.022 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libavcodec     56.  1.100 / 56.  1.100
03-26 12:10:17.022 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libavformat    56.  4.101 / 56.  4.101
03-26 12:10:17.022 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libavdevice    56.  0.100 / 56.  0.100
03-26 12:10:17.022 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libavfilter     5.  1.100 /  5.  1.100
03-26 12:10:17.022 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libswscale      3.  0.100 /  3.  0.100
03-26 12:10:17.022 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libswresample   1.  1.100 /  1.  1.100
03-26 12:10:17.022 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   libpostproc    53.  0.100 / 53.  0.100
03-26 12:10:17.201 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/Android/data/com.noq/files/Pictures/noq/1459008610063.mp4':
03-26 12:10:17.201 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   Metadata:
03-26 12:10:17.201 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     major_brand     : mp42
03-26 12:10:17.201 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     minor_version   : 0
03-26 12:10:17.201 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     compatible_brands: isommp42
03-26 12:10:17.201 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     creation_time   : 2016-03-26 16:10:16
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   Duration: 00:00:02.02, start: 0.000000, bitrate: 6131 kb/s
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 640x480, 5926 kb/s, SAR 65536:65536 DAR 4:3, 29.87 fps, 30.17 tbr, 90k tbn, 180k tbc (default)
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Metadata:
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       rotate          : 270
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       creation_time   : 2016-03-26 16:10:16
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       handler_name    : VideoHandle
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       encoder         : MOTO
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Side data:
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       displaymatrix: rotation of 90.00 degrees
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Metadata:
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       creation_time   : 2016-03-26 16:10:16
03-26 12:10:17.202 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       handler_name    : SoundHandle
03-26 12:10:17.263 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] using SAR=1/1
03-26 12:10:17.297 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] using cpu capabilities: none!
03-26 12:10:17.411 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] profile Constrained Baseline, level 2.1
03-26 12:10:17.417 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] 264 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
03-26 12:10:17.464 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: Output #0, mp4, to '/storage/emulated/0/Android/data/com.noq/files/Pictures/noq/14590086100631459008616802_square.mp4':
03-26 12:10:17.464 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   Metadata:
03-26 12:10:17.464 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     major_brand     : mp42
03-26 12:10:17.464 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     minor_version   : 0
03-26 12:10:17.464 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     compatible_brands: isommp42
03-26 12:10:17.464 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     encoder         : Lavf56.4.101
03-26 12:10:17.465 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 360x360 [SAR 1:1 DAR 1:1], q=-1--1, 30.17 fps, 11584 tbn, 30.17 tbc (default)
03-26 12:10:17.465 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Metadata:
03-26 12:10:17.465 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       rotate          : 270
03-26 12:10:17.465 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       creation_time   : 2016-03-26 16:10:16
03-26 12:10:17.465 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       handler_name    : VideoHandle
03-26 12:10:17.465 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       encoder         : Lavc56.1.100 libx264
03-26 12:10:17.465 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s (default)
03-26 12:10:17.466 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:     Metadata:
03-26 12:10:17.466 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       creation_time   : 2016-03-26 16:10:16
03-26 12:10:17.466 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       handler_name    : SoundHandle
03-26 12:10:17.467 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:       encoder         : Lavc56.1.100 aac
03-26 12:10:17.467 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: Stream mapping:
03-26 12:10:17.467 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
03-26 12:10:17.467 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress:   Stream #0:1 -> #0:1 (aac (native) -> aac (native))
03-26 12:10:17.468 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: Press [q] to stop, [?] for help
03-26 12:10:18.035 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: frame=    3 fps=0.0 q=0.0 size=       0kB time=00:00:01.00 bitrate=   0.4kbits/s dup=1 drop=0    
03-26 12:10:18.526 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: frame=   21 fps= 20 q=24.0 size=      48kB time=00:00:01.00 bitrate= 390.3kbits/s dup=1 drop=0    
03-26 12:10:19.008 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: frame=   26 fps= 17 q=25.0 size=      68kB time=00:00:01.85 bitrate= 301.1kbits/s dup=1 drop=0    
03-26 12:10:19.533 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: frame=   47 fps= 23 q=23.0 size=     165kB time=00:00:01.87 bitrate= 719.7kbits/s dup=1 drop=0    
03-26 12:10:19.956 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: frame=   62 fps= 25 q=-1.0 Lsize=     297kB time=00:00:02.05 bitrate=1184.9kbits/s dup=1 drop=0    
03-26 12:10:19.956 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: video:262kB audio:33kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.992648%
03-26 12:10:19.984 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] frame I:1     Avg QP:20.00  size: 18507
03-26 12:10:19.984 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] frame P:61    Avg QP:23.52  size:  4079
03-26 12:10:19.984 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] mb I  I16..4: 100.0%  0.0%  0.0%
03-26 12:10:19.984 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] mb P  I16..4:  4.4%  0.0%  0.0%  P16..4: 70.9%  0.0%  0.0%  0.0%  0.0%    skip:24.7%
03-26 12:10:19.985 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] coded y,uvDC,uvAC intra: 44.6% 40.1% 14.8% inter: 39.1% 32.7% 0.6%
03-26 12:10:19.985 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] i16 v,h,dc,p: 42% 24% 21% 13%
03-26 12:10:19.986 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] i8c dc,h,v,p: 44% 24% 25%  8%
03-26 12:10:19.987 17198-17198/com.noq D/FFMPEG: FFMPEG onProgress: [libx264 @ 0xb78cde30] kb/s:1040.60
03-26 12:10:20.002 17198-17198/com.noq D/FFMPEG: FFMPEG onSuccess: WARNING: linker: /data/user/0/com.noq/files/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
                                                 ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
                                                   built on Oct  7 2014 15:08:46 with gcc 4.8 (GCC)
                                                   configuration: --target-os=linux --cross-prefix=/home/sb/Source-Code/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/sb/Source-Code/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/sb/Source-Code/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/sb/Source-Code/ffmpeg-android/build/armeabi-v7a-neon --extra-cflags='-I/home/sb/Source-Code/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all -mfpu=neon' --extra-ldflags='-L/home/sb/Source-Code/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
                                                   libavutil      54.  7.100 / 54.  7.100
                                                   libavcodec     56.  1.100 / 56.  1.100
                                                   libavformat    56.  4.101 / 56.  4.101
                                                   libavdevice    56.  0.100 / 56.  0.100
                                                   libavfilter     5.  1.100 /  5.  1.100
                                                   libswscale      3.  0.100 /  3.  0.100
                                                   libswresample   1.  1.100 /  1.  1.100
                                                   libpostproc    53.  0.100 / 53.  0.100
                                                 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/Android/data/com.noq/files/Pictures/noq/1459008610063.mp4':
                                                   Metadata:
                                                     major_brand     : mp42
                                                     minor_version   : 0
                                                     compatible_brands: isommp42
                                                     creation_time   : 2016-03-26 16:10:16
                                                   Duration: 00:00:02.02, start: 0.000000, bitrate: 6131 kb/s
                                                     Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 640x480, 5926 kb/s, SAR 65536:65536 DAR 4:3, 29.87 fps, 30.17 tbr, 90k tbn, 180k tbc (default)
                                                     Metadata:
                                                       rotate          : 270
                                                       creation_time   : 2016-03-26 16:10:16
                                                       handler_name    : VideoHandle
                                                       encoder         : MOTO
                                                     Side data:
                                                       displaymatrix: rotation of 90.00 degrees
                                                     Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 130 kb/s (default)
                                                     Metadata:
                                                       creation_time   : 2016-03-26 16:10:16
                                                       handler_name    : SoundHandle
                                                 [libx264 @ 0xb78cde30] using SAR=1/1
                                                 [libx264 @ 0xb78cde30] using cpu capabilities: none!
                                                 [libx264 @ 0xb78cde30] profile Constrained Baseline, level 2.1
                                                 [libx264 @ 0xb78cde30] 264 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
                                                 Output #0, mp4, to '/storage/emulated/0/Android/data/com.noq/files/Pictures/noq/14590086100631459008616802_square.mp4':
                                                   Metadata:
                                                     major_brand     : mp42
                                                     minor_version   : 0
                                                     compatible_brands: isommp42
                                                     encoder         : Lavf56.4.101
                                                     Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 360x360 [SAR 1:1 DAR 1:1], q=-1--1, 30.17 fps, 11584 tbn, 30.17 tbc (default)
                                                     Metadata:
                                                       rotate          : 270
                                                       creation_time   : 2016-03-26 16:10:16
                                                       handler_name    : VideoHandle
                                                       encoder         : Lavc56.1.100 libx264
                                                     Stream #0:1(eng): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s (default)
                                                     Metadata:
                                                       creation_time   : 2016-03-26 16:10:16
                                                       handler_name    : SoundHandle
                                                       encoder         : La
03-26 12:10:20.037 17198-17198/com.noq D/FFMPEG: FFMPEG onFinish

【问题讨论】:

应该按预期工作。 @Mulvya...应该,但不应该:) 只进行裁剪,看看行为是否相同。 @Mulvya 如果我只进行裁剪,它会从原始视频源中进行 360x360 裁剪,这不是我需要的。它必须按比例缩放。我基本上需要原始视频的整个宽度,而不是一部分。 是的,但是请检查它是从左上角还是中心。 【参考方案1】:

您的源视频已旋转 90 度!

与原始像素矩阵一起使用

-vf scale=480:360,crop=360:360:120:0

旋转重置,2015 年 5 月或更高版本的 ffmpeg 版本。

"-noautorotate -i " + path + \
" -vf scale=480:360,transpose=2,crop=360:360:0:0 
 -strict -2 -preset ultrafast -metadata:s:v rotate=0" + fileOutPath

对于旧版本,请跳过 noautorotate 标志。

【讨论】:

不,不是:/你为什么这么说? This: "displaymatrix: 旋转 90.00 度" 我希望你是对的 :( 这只是水平拉伸了我的视频 @Mulvya 我相信 2.4.2 对于自动轮换和-noautorotate 选项来说太旧了。 已更新。我的第一个命令假定旧版本不会重置轮换标签。

以上是关于FFMPEG 在单个命令中缩放和裁剪的主要内容,如果未能解决你的问题,请参考以下文章

ffmpeg调整缩放裁剪视频的基础

在 ffmpeg 中去除闪烁、裁剪和放大

如何使用ffmpeg进行音视频裁剪命令和音视频合成命令

流媒体开发11ffmpeg命令过滤器(裁剪水印画中画多宫格)

流媒体开发11ffmpeg命令过滤器(裁剪水印画中画多宫格)

Flutter音视频裁剪flutter_ffmpeg踩坑笔记