ffmpeg 命令以较小的文件大小以适当的比特率进行更快的编码
Posted
技术标签:
【中文标题】ffmpeg 命令以较小的文件大小以适当的比特率进行更快的编码【英文标题】:ffmpeg command for faster encoding at a decent bitrate with smaller file size 【发布时间】:2017-01-21 06:08:41 【问题描述】:我目前正在我的 android 应用程序上使用 ffmpeg 的实现。我允许用户在我的应用程序中拍摄短视频,然后当他们将它们上传到服务器时,我会使用 ffmpeg 将它们压缩以减小文件大小,这样他们就不会通过网络传递大量数据。
问题是,在 android 设备上对视频进行编码需要很长时间。这些视频通常不超过 45 秒,编码可能需要 20 分钟。我已经在 ffmpeg 命令行上使用不同的开关/参数进行了一些尝试,现在我可以在更舒适的时间使用它,但是文件大小要大得多。我只是不确定我应该使用什么编解码器(最快的编码但具有良好的输出质量),ffmpeg 如何在编码速度等方面处理变化的大小(纵横比)。
这是我一直在使用的两个命令。第一个输出我想要的文件大小/质量,但是编码时间太长了,更不用说它让我的设备在编码这么长时间时变得非常热:
ffmpeg -i input.mp4 -b:v 1024k -c:a copy -vf scale=960:540 output.mp4
我已经调整了一些比特率并将比例更改为更小的尺寸,但我不想让视频变得非常小(就比例而言)以实现更快的编码。第二条命令执行速度更快,但文件大小明显变大:
ffmpeg -i input.mp4 -vcodec libx264 -preset fast -c:a copy -s 960x540 output.mp4
我想找到一个快乐的媒体(文件更小,但编码更快),让视频比例尺接近原始尺寸。 ffmpeg 只是有太多不同的参数/开关,以至于我很难把我的头放在我应该做什么上。
编辑:添加 ffmpeg 输出。
ffmpeg -i input.mp4 -vcodec libx264 -crf 30 -preset veryfast -c:a copy -s 960x540 output.mp4
09-13 11:06:28.330 10881-10881/someapp D/home: ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
09-13 11:06:28.330 10881-10881/someapp D/home: built with gcc 4.8 (GCC)
09-13 11:06:28.331 10881-10881/someapp D/home: configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --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/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
09-13 11:06:28.331 10881-10881/someapp D/home: libavutil 55. 17.103 / 55. 17.103
09-13 11:06:28.331 10881-10881/someapp D/home: libavcodec 57. 24.102 / 57. 24.102
09-13 11:06:28.331 10881-10881/someapp D/home: libavformat 57. 25.100 / 57. 25.100
09-13 11:06:28.331 10881-10881/someapp D/home: libavdevice 57. 0.101 / 57. 0.101
09-13 11:06:28.331 10881-10881/someapp D/home: libavfilter 6. 31.100 / 6. 31.100
09-13 11:06:28.331 10881-10881/someapp D/home: libswscale 4. 0.100 / 4. 0.100
09-13 11:06:28.331 10881-10881/someapp D/home: libswresample 2. 0.101 / 2. 0.101
09-13 11:06:28.331 10881-10881/someapp D/home: libpostproc 54. 0.100 / 54. 0.100
09-13 11:06:28.430 10881-10881/someapp D/home: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/ExpeditionSpot/Videos/20160913110411.mp4':
09-13 11:06:28.430 10881-10881/someapp D/home: Metadata:
09-13 11:06:28.430 10881-10881/someapp D/home: major_brand : mp42
09-13 11:06:28.430 10881-10881/someapp D/home: minor_version : 0
09-13 11:06:28.430 10881-10881/someapp D/home: compatible_brands: isommp42
09-13 11:06:28.430 10881-10881/someapp D/home: creation_time : 2016-09-13 17:04:33
09-13 11:06:28.430 10881-10881/someapp D/home: com.android.version: 6.0.1
09-13 11:06:28.430 10881-10881/someapp D/home: Duration: 00:00:19.41, start: 0.000000, bitrate: 20222 kb/s
09-13 11:06:28.430 10881-10881/someapp D/home: Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p, 1920x1080, 19963 kb/s, SAR 1:1 DAR 16:9, 30.03 fps, 30 tbr, 90k tbn, 180k tbc (default)
09-13 11:06:28.430 10881-10881/someapp D/home: Metadata:
09-13 11:06:28.430 10881-10881/someapp D/home: creation_time : 2016-09-13 17:04:33
09-13 11:06:28.430 10881-10881/someapp D/home: handler_name : VideoHandle
09-13 11:06:28.431 10881-10881/someapp D/home: Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
09-13 11:06:28.431 10881-10881/someapp D/home: Metadata:
09-13 11:06:28.431 10881-10881/someapp D/home: creation_time : 2016-09-13 17:04:33
09-13 11:06:28.431 10881-10881/someapp D/home: handler_name : SoundHandle
09-13 11:06:28.448 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] using SAR=1/1
09-13 11:06:28.448 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] using cpu capabilities: none!
09-13 11:06:28.516 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] profile High, level 3.1
09-13 11:06:28.516 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 ref=1 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=2 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12 lookahead_threads=4 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=1 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=10 rc=crf mbtree=1 crf=27.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
09-13 11:06:28.532 10881-10881/someapp D/home: Output #0, mp4, to '/storage/emulated/0/ExpeditionSpot/.tmp/small-20160913110411.mp4':
09-13 11:06:28.532 10881-10881/someapp D/home: Metadata:
09-13 11:06:28.532 10881-10881/someapp D/home: major_brand : mp42
09-13 11:06:28.533 10881-10881/someapp D/home: minor_version : 0
09-13 11:06:28.533 10881-10881/someapp D/home: compatible_brands: isommp42
09-13 11:06:28.533 10881-10881/someapp D/home: com.android.version: 6.0.1
09-13 11:06:28.533 10881-10881/someapp D/home: encoder : Lavf57.25.100
09-13 11:06:28.533 10881-10881/someapp D/home: Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 960x540 [SAR 1:1 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default)
09-13 11:06:28.533 10881-10881/someapp D/home: Metadata:
09-13 11:06:28.533 10881-10881/someapp D/home: creation_time : 2016-09-13 17:04:33
09-13 11:06:28.533 10881-10881/someapp D/home: handler_name : VideoHandle
09-13 11:06:28.533 10881-10881/someapp D/home: encoder : Lavc57.24.102 libx264
09-13 11:06:28.533 10881-10881/someapp D/home: Side data:
09-13 11:06:28.533 10881-10881/someapp D/home: unknown side data type 10 (24 bytes)
09-13 11:06:28.533 10881-10881/someapp D/home: Stream #0:1(eng): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 96 kb/s (default)
09-13 11:06:28.533 10881-10881/someapp D/home: Metadata:
09-13 11:06:28.533 10881-10881/someapp D/home: creation_time : 2016-09-13 17:04:33
09-13 11:06:28.533 10881-10881/someapp D/home: handler_name : SoundHandle
09-13 11:06:28.533 10881-10881/someapp D/home: Stream mapping:
09-13 11:06:28.533 10881-10881/someapp D/home: Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
09-13 11:06:28.533 10881-10881/someapp D/home: Stream #0:1 -> #0:1 (copy)
09-13 11:06:28.533 10881-10881/someapp D/home: Press [q] to stop, [?] for help
09-13 11:06:29.102 10881-10881/someapp D/home: frame= 7 fps=0.0 q=0.0 size= 0kB time=00:00:01.04 bitrate= 0.4kbits/s speed=2.08x
09-13 11:06:29.699 10881-10881/someapp D/home: frame= 16 fps= 15 q=0.0 size= 0kB time=00:00:01.04 bitrate= 0.4kbits/s speed=0.998x
....
09-13 11:07:12.674 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] frame I:3 Avg QP:26.83 size: 21896
09-13 11:07:12.674 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] frame P:279 Avg QP:28.76 size: 5859
09-13 11:07:12.674 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] frame B:296 Avg QP:29.93 size: 863
09-13 11:07:12.674 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] consecutive B-frames: 18.3% 32.2% 23.9% 25.6%
09-13 11:07:12.674 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] mb I I16..4: 16.9% 54.2% 28.9%
09-13 11:07:12.674 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] mb P I16..4: 7.7% 9.1% 0.4% P16..4: 27.5% 11.1% 4.0% 0.0% 0.0% skip:40.1%
09-13 11:07:12.674 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] mb B I16..4: 0.8% 0.6% 0.0% B16..8: 10.9% 2.4% 0.1% direct: 1.8% skip:83.5% L0:35.8% L1:54.2% BI:10.0%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] 8x8 transform intra:52.0% inter:41.3%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] coded y,uvDC,uvAC intra: 28.9% 26.0% 2.1% inter: 6.4% 5.0% 0.0%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] i16 v,h,dc,p: 57% 20% 17% 6%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 33% 23% 34% 1% 2% 1% 3% 2% 2%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36% 21% 17% 2% 7% 4% 6% 4% 4%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] i8c dc,h,v,p: 59% 16% 23% 2%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] Weighted P-Frames: Y:16.8% UV:4.7%
09-13 11:07:12.675 10881-10881/someapp D/home: [libx264 @ 0xf71c4400] kb/s:812.09
【问题讨论】:
您无能为力。转码是一个 CPU 高度密集的过程。您正在同时有效地解码和编码视频。除了使用 GPU 卸载(不知道 ffmpeg 是否甚至可以使用 gpus)之外,当它执行大量必要的数学运算时,你会被烧毁 cpu。 好吧,显然我可以做一些事情,因为这两个命令会产生截然不同的结果(就编码时间而言)。我确信某些编解码器比其他编解码器更快等,只是不确定哪个或什么...... 试试ffmpeg -i input.mp4 -vcodec libx264 -crf 27 -preset veryfast -c:a copy -s 960x540 output.mp4
。但正如 Marc 所说,考虑到其他限制,加速的范围不大。
您正在执行的特定转换可能会影响时间,但您仍在对视频进行解码/重新编码。你无法避免。使用副本而不是缩放操作会节省一些时间,但是如果不知道您的具体输出要求是什么,就无法回答这个问题。将所有内容缩小到每视频一帧的 1x1 图像显然会花费最少的时间,因为它基本上是 1x1 jpg - 但并不完全有用。
@Mulvya 我稍后会试一试。你能解释一下 -crf 27 是什么意思吗?
【参考方案1】:
试试
ffmpeg -i input.mp4 -vcodec libx264 -crf 27 -preset veryfast -c:a copy -s 960x540 output.mp4
在 CRF 模式下,x264 对视频进行编码以始终保持一定的质量。较低的值会产生更高质量但更大的文件。结合veryfast
预设,它应该在速度和大小之间提供可接受的折衷。
【讨论】:
使用ultrafast
而不是veryfast
,以加快压缩速度
@reverie_ss veryfast
预设 - 4.9mb (9s), ultrafast
- 12.3mb (8s)。只有一秒的差异。以上是关于ffmpeg 命令以较小的文件大小以适当的比特率进行更快的编码的主要内容,如果未能解决你的问题,请参考以下文章
需要完整的图像作为背景Cover Scroll,当前Cover看起来已放大。我希望以较小的缩放而不是完整的缩放显示实际图像
当两个ID在Scala中具有相同的最高价格时,以较小的ID获得最高价格