为什么使用 ffmpeg 从 png 文件创建的 mp4 文件的持续时间比预期的要短?

Why duration of the mp4 file created with ffmpeg from png files is smaller than expected?

我正在尝试使用 ffmpeg 将一堆 png 文件连接成一个 mp4 文件。我使用以下命令:

ffmpeg -f concat -i concat.txt -f mp4 out.mp4

“描述文件”(concat.txt)如下所示:

file screen_001.png
duration 0.14538311958312988
file screen_002.png
duration 0.11382007598876953
file screen_003.png
duration 2.543360710144043
...
file screen_036.png
duration 0.15303301811218262
file screen_037.png
duration 0.160630464553833
file screen_038.png
duration 3.2751874923706055

鉴于命令有效,我能够创建所需的 mp4 文件。问题是,输出文件的持续时间小于 concat.txtduration 行的总和。我希望 mp4 文件长 ~22.48s 但我得到 19.20s 长文件。

我做错了什么?也许我没有使用我应该使用的标志?甚至可以做我想做的事吗?我是一个新手 ffmpeg 和视频操作,所以任何帮助将不胜感激。

ffmpeg 的输出,也许会有用:

root@65181939e08e:/files/tmp# ffmpeg -f concat -i concat.txt -f mp4 out.mp4
ffmpeg version 4.1.6-1~deb10u1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 8 (Debian 8.3.0-6)
  configuration: --prefix=/usr --extra-version='1~deb10u1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-avresample --disable-filter=resample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librsvg --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-shared
  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, concat, from 'concat.txt':
  Duration: 00:00:22.48, start: 0.000000, bitrate: 0 kb/s
    Stream #0:0: Video: png, rgba(pc), 1366x768, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 0x5651f1a6e8c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x5651f1a6e8c0] profile High 4:4:4 Predictive, level 3.2, 4:4:4 8-bit
[libx264 @ 0x5651f1a6e8c0] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=4 threads=12 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 'out.mp4':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv444p, 1366x768, q=-1--1, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.35.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame=  483 fps=199 q=-1.0 Lsize=     258kB time=00:00:19.20 bitrate= 110.2kbits/s dup=445 drop=0 speed=7.89x    
video:252kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.564829%
[libx264 @ 0x5651f1a6e8c0] frame I:5     Avg QP: 7.86  size: 28761
[libx264 @ 0x5651f1a6e8c0] frame P:121   Avg QP:14.58  size:   729
[libx264 @ 0x5651f1a6e8c0] frame B:357   Avg QP:13.34  size:    70
[libx264 @ 0x5651f1a6e8c0] consecutive B-frames:  1.2%  0.0%  1.9% 96.9%
[libx264 @ 0x5651f1a6e8c0] mb I  I16..4: 88.2%  0.0% 11.8%
[libx264 @ 0x5651f1a6e8c0] mb P  I16..4:  1.3%  0.0%  0.3%  P16..4:  0.2%  0.0%  0.0%  0.0%  0.0%    skip:98.1%
[libx264 @ 0x5651f1a6e8c0] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  0.9%  0.0%  0.0%  direct: 0.0%  skip:99.1%  L0:59.4% L1:40.6% BI: 0.0%
[libx264 @ 0x5651f1a6e8c0] coded y,u,v intra: 7.9% 0.7% 1.3% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x5651f1a6e8c0] i16 v,h,dc,p: 81% 18%  1%  0%
[libx264 @ 0x5651f1a6e8c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 33% 28% 22%  2%  3%  3%  3%  3%  3%
[libx264 @ 0x5651f1a6e8c0] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x5651f1a6e8c0] ref P L0: 72.5% 11.9% 12.4%  3.1%
[libx264 @ 0x5651f1a6e8c0] ref B L0: 50.4% 48.1%  1.4%
[libx264 @ 0x5651f1a6e8c0] ref B L1: 98.9%  1.1%
[libx264 @ 0x5651f1a6e8c0] kb/s:106.45

感谢您的帮助!

像这样从每个 png 创建一个视频:

ffmpeg -f image2 -loop 1 -i 01.png -r 30 -t 00:00:05.000 -pix_fmt yuv420p 01.mp4

在哪里: "-r 30" 是帧率

"-t 00:00:05.000" 是这个 PNG 在视频中的时间,在这种情况下是 5 秒,看这个浮点数是 3 : (

无论如何,为每个视频制作这个并连接它,创建一个 txt:

concat.txt

file '01.mp4'
file '02.mp4'

并使用 ffmpeg 连接所有文件

ffmpeg -f concat -safe 0 -i concat.txt -c copy output.mp4

祝你好运!

文档中的编辑也以微秒为单位,可能浮点数是 6 而不是 3:

http://www.ffmpeg.org/ffmpeg-utils.html#time-duration-syntax