使用FFMPEG concat将帧添加到具有相同FPS的视频会降低输出FPS


2

我正在开发一个C#图像采集软件,我正在将图像编译成视频。

目前我这样做是通过一组图像创建一个图像,一旦图像计数是所需输出FPS的图像,为了这个问题我们将使用5 FPS。它将等到它有5个图像,因此它可以为基本视频渲染1秒的素材,新帧将被添加到其中。要生成此视频,我使用以下命令:

-framerate 5 -start_number 1 -i“C:\ path \ to \ image \ Image_%05d.jpg”-c:v libx264 -pix_fmt yuv420p -r 5“C:\ path \ to \ output \ output.mp4”

探针输出:

ffmpeg version N-86994-g92da230 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 7.1.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
  libavutil      55. 74.100 / 55. 74.100
  libavcodec     57.102.100 / 57.102.100
  libavformat    57. 76.100 / 57. 76.100
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 99.100 /  6. 99.100
  libswscale      4.  7.102 /  4.  7.102
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
Input #0, image2, from 'B00375_EOL_%05d.jpg':
  Duration: 00:00:02.40, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1200x1200 [SAR 144:144 DAR 1:1], 5 fps, 5 tbr, 5 tbn, 5 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[swscaler @ 0553cfc0] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 05491120] using SAR=1/1
[libx264 @ 05491120] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 05491120] profile High, level 4.0
[libx264 @ 05491120] 264 - core 152 r2851 ba24899 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - 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=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=5 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 'output.mp4':
  Metadata:
    encoder         : Lavf57.76.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1200x1200 [SAR 1:1 DAR 1:1], q=-1--1, 5 fps, 10240 tbn, 5 tbc
    Metadata:
      encoder         : Lavc57.102.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame=   12 fps=0.0 q=-1.0 Lsize=     187kB time=00:00:01.80 bitrate= 852.8kbits/s speed=2.76x
video:186kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.490686%
[libx264 @ 05491120] frame I:2     Avg QP:14.80  size: 23424
[libx264 @ 05491120] frame P:9     Avg QP:15.59  size: 14619
[libx264 @ 05491120] frame B:1     Avg QP:15.13  size: 11846
[libx264 @ 05491120] consecutive B-frames: 83.3% 16.7%  0.0%  0.0%
[libx264 @ 05491120] mb I  I16..4: 27.4% 71.2%  1.4%
[libx264 @ 05491120] mb P  I16..4: 15.6% 72.7%  0.2%  P16..4:  2.8%  0.8%  0.7%  0.0%  0.0%    skip: 7.2%
[libx264 @ 05491120] mb B  I16..4:  5.3% 31.8%  0.0%  B16..8: 13.6%  8.8%  0.7%  direct: 7.9%  skip:31.8%  L0:93.9% L1: 5.1% BI: 1.0%
[libx264 @ 05491120] 8x8 transform intra:80.1% inter:91.7%
[libx264 @ 05491120] coded y,uvDC,uvAC intra: 22.2% 25.1% 4.7% inter: 8.5% 14.4% 0.8%
[libx264 @ 05491120] i16 v,h,dc,p: 38% 31%  7% 24%
[libx264 @ 05491120] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 52% 21% 23%  1%  1%  0%  1%  1%  1%
[libx264 @ 05491120] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 30% 34% 14%  3%  3%  4%  3%  4%  3%
[libx264 @ 05491120] i8c dc,h,v,p: 65% 18% 14%  3%
[libx264 @ 05491120] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 05491120] ref P L0: 67.1%  9.6% 20.2%  3.1%
[libx264 @ 05491120] kb/s:634.23

从这里我然后生成短的单帧视频附加到视频的末尾以添加新帧,这是通过此命令完成的:

-framerate 5 -loop 1 -i“C:\ path \ to \ image \ new_image.jpg”-c:v libx264 -pix_fmt yuv420p -t 0.2 -r 5“C:\ path \ to \ output \ new_frame.mp4”

探针输出:

ffmpeg version N-86994-g92da230 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 7.1.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
  libavutil      55. 74.100 / 55. 74.100
  libavcodec     57.102.100 / 57.102.100
  libavformat    57. 76.100 / 57. 76.100
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 99.100 /  6. 99.100
  libswscale      4.  7.102 /  4.  7.102
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
Input #0, image2, from 'B00375_PRC_00001.jpg':
  Duration: 00:00:00.20, start: 0.000000, bitrate: 880 kb/s
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 1200x1200 [SAR 144:144 DAR 1:1], 5 fps, 5 tbr, 5 tbn, 5 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[swscaler @ 03ba0fe0] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 03b0e2e0] using SAR=1/1
[libx264 @ 03b0e2e0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 03b0e2e0] profile High, level 4.0
[libx264 @ 03b0e2e0] 264 - core 152 r2851 ba24899 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - 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=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=5 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 'new_frame.mp4':
  Metadata:
    encoder         : Lavf57.76.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1200x1200 [SAR 1:1 DAR 1:1], q=-1--1, 5 fps, 10240 tbn, 5 tbc
    Metadata:
      encoder         : Lavc57.102.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame=    1 fps=0.0 q=23.0 Lsize=      15kB time=00:00:00.00 bitrate=1287020.4kbits/s speed=0.000332x
video:15kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 5.465249%
[libx264 @ 03b0e2e0] frame I:1     Avg QP:15.27  size: 14260
[libx264 @ 03b0e2e0] mb I  I16..4: 19.3% 79.8%  0.9%
[libx264 @ 03b0e2e0] 8x8 transform intra:79.8%
[libx264 @ 03b0e2e0] coded y,uvDC,uvAC intra: 19.1% 10.8% 0.1%
[libx264 @ 03b0e2e0] i16 v,h,dc,p: 35% 49%  6% 10%
[libx264 @ 03b0e2e0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 50% 22% 26%  0%  1%  0%  1%  0%  0%
[libx264 @ 03b0e2e0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 34% 21% 16%  4%  3%  6%  4%  5%  5%
[libx264 @ 03b0e2e0] i8c dc,h,v,p: 67% 18% 14%  1%
[libx264 @ 03b0e2e0] kb/s:570.40

在这个阶段,这两个输出文件都很好。'main'视频生成持续1秒,在文件属性中显示5 FPS,单帧也是如此,它持续0.2秒并在属性中显示5 FPS。然后通过以下命令加入这些文件:

-f concat -i concat.txt -auto_convert 1 -c copy joined_vid.mp4其中concat.txt如下所示:

文件'output.mp4'
文件'new_frame.mp4'

探针输出:

ffmpeg version N-86994-g92da230 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 7.1.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
  libavutil      55. 74.100 / 55. 74.100
  libavcodec     57.102.100 / 57.102.100
  libavformat    57. 76.100 / 57. 76.100
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 99.100 /  6. 99.100
  libswscale      4.  7.102 /  4.  7.102
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 052d8700] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'concat.txt':
  Duration: N/A, start: 0.000000, bitrate: 636 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1200x1200 [SAR 1:1 DAR 1:1], 636 kb/s, 5 fps, 5 tbr, 10240 tbn, 10 tbc
    Metadata:
      handler_name    : VideoHandler
Output #0, mp4, to 'joined_vid.mp4':
  Metadata:
    encoder         : Lavf57.76.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1200x1200 [SAR 1:1 DAR 1:1], q=2-31, 636 kb/s, 5 fps, 5 tbr, 10240 tbn, 10240 tbc
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 052d8700] Auto-inserting h264_mp4toannexb bitstream filter
frame=   13 fps=0.0 q=-1.0 Lsize=     202kB time=00:00:02.40 bitrate= 690.0kbits/s speed= 135x
video:201kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.470814%

从这里开始,新创建的'joined_vid.mp4的FPS下降,通常降至4 FPS(但已经看到它直接降至3 FPS),如图1所示。随着我逐步添加更多帧并获得新图像,FPS继续下降直到它达到1 FPS然后它保持在1。

图1

我的问题是如何在没有的情况下将我的新图像附加到视频的末尾

  • 丢帧
  • 必须使用每个新图像重新渲染整个视频
  • 删除输出文件的FPS

非常感谢任何帮助,因为这目前让我难过!


看起来是由于ffmpeg中破坏的H.264 PTS生成。解决方法是跳过B帧,因此添加-bf 0到编码命令。
吉安

@Mulvya抱歉很长的回复,这还没有解决我的问题,我仍然看到相同的事情(帧从5 - > 1)。也许附录文件中的编解码器与延时不同?
J. Scull

Answers:


2

这确实是由于H264 PTS生成错误。我在评论中的建议对我有用。但是帖子结尾处的方法也是如此。

初始文件的命令

ffmpeg -framerate 5 -start_number 1 -t 1 -i "f%d.png" -c:v libx264 -pix_fmt yuv420p -r 5 -bf 0 initial.mp4

单个框架的命令

ffmpeg -framerate 5 -i "f6.png" -c:v libx264 -pix_fmt yuv420p -bf 0 -r 5 next1.mp4
...
ffmpeg -framerate 5 -i "f10.png" -c:v libx264 -pix_fmt yuv420p -bf 0 -r 5 next5.mp4

单个联接

LIST.TXT:

file initial.mp4
file next1.mp4

ffmpeg -f concat -i list.txt -auto_convert 1 -c copy with6.mp4

with6.mp4:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'with6.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.2.100
  Duration: 00:00:01.20, start: 0.000000, bitrate: 129 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 124 kb/s, 5 fps, 5 tbr, 10240 tbn, 10 tbc (default)
    Metadata:
      handler_name    : VideoHandler

立即加入其余部分

file with6.mp4
file next2.mp4
file next3.mp4
file next4.mp4
file next5.mp4

ffmpeg -f concat -i list.txt -auto_convert 1 -c copy with6.mp4

with10.mp4:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'with10.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.2.100
  Duration: 00:00:02.00, start: 0.000000, bitrate: 168 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 320x240 [SAR 1:1 DAR 4:3], 165 kb/s, 5 fps, 5 tbr, 10240 tbn, 10 tbc (default)
    Metadata:
      handler_name    : VideoHandler

另一种方法是保持命令不变,但首先是mux到MKV,然后重新启动到MP4即

ffmpeg -f concat -i concat.txt -c copy new_output.mkv

ffmpeg -i new_output.mkv -c copy new_output.mp4

有没有针对此的错误报告?
llogan 2017年

对于这个具体问题?不,但#502是相关的。
吉安

谢谢你的回答,它似乎完美无缺!我误解的是-bf 0,我只是在加入视频时才把它放进去,因为我认为这是我需要跳过帧的时候。我无法将其转换为其他文件类型,因为所有这些处理都必须非常快速地进行。再次干杯:)
J. Scull
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.