由于 jpg 帧更改,ffmpeg 将 jpg 转换为 mp4 失败

ffmpeg conversion of jpg to mp4 fails due to to jpg frames change

我正在尝试将一组 jpg(每 5 秒拍摄一次的相机屏幕截图)编译为 mp4。 ffmpeg 命令工作正常,但生成的 mp4 缺少一些图像。我注意到 "holes" 是在 jpg "frames" 在 jpg 序列中从 1 变为 3 时创建的(见下面 file 的结果显示第一个 jpg frames 1 frames 3 第二个):

$ file 20190110060113.jpg
20190110060113.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1280x720, frames 1

$ file 20190110060119.jpg
20190110060119.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 1280x720, frames 3

并且 ffmpeg,播种:

ffmpeg -i 20190110060113.jpg -i 20190110060119.jpg
ffmpeg version 3.2.10-1~deb9u1~bpo8+1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.9.2 (Debian 4.9.2-10)
  configuration: --prefix=/usr --extra-version='1~deb9u1~bpo8+1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --disable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 34.101 / 55. 34.101
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.101 / 57. 56.101
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, image2, from '20190110060113.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 13395 kb/s
    Stream #0:0: Video: mjpeg, gray(bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
Input #1, image2, from '20190110060119.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 21463 kb/s
    Stream #1:0: Video: mjpeg, yuvj422p(pc, bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 tbr, 25 tbn, 25 tbc
At least one output file must be specified

下面是我用来从 jpg 编译 mp4 的命令:

ffmpeg -r 25 -f image2 -s 1280x720 -i /data/camipjpg/%*.jpg -vcodec libx264 -crf 25 -pix_fmt yuv420p /data/camipmgpg/20190110.mp4

出于好奇,我调查了一下,发现相机实际上有两种模式(白天和黑夜)。在夜间拍摄的图像具有 frames 1 而在 "day" 模式下拍摄的图像具有 frames 3.

ffmpeg 是否可以处理这种 jpg 更改以组合这些不同类型的 jpg?

这是输出:

ffmpeg version 3.2.10-1~deb9u1~bpo8+1 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 4.9.2 (Debian 4.9.2-10)
  configuration: --prefix=/usr --extra-version='1~deb9u1~bpo8+1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --disable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  libavutil      55. 34.101 / 55. 34.101
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.101 / 57. 56.101
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
[image2 @ 0x55aed5072ba0] Pattern type 'glob_sequence' is deprecated: use pattern_type 'glob' instead
Input #0, image2, from 'bcp/%*.jpg':
  Duration: 00:09:07.24, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: mjpeg, gray(bt470bg/unknown/unknown), 1280x720 [SAR 1:1 DAR 16:9], 25 fps, 25 tbr, 25 tbn, 25 tbc
[libx264 @ 0x55aed50b8300] using SAR=1/1
[libx264 @ 0x55aed50b8300] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
[libx264 @ 0x55aed50b8300] profile High, level 3.1
[libx264 @ 0x55aed50b8300] 264 - core 142 r2431 a5831aa - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - 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=6 lookahead_threads=1 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=25.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'test.mp4':
  Metadata:
    encoder         : Lavf57.56.101
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.64.101 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> h264 (libx264))
Press [q] to stop, [?] for help
Input stream #0:0 frame changed from size:1280x720 fmt:gray to size:1280x720 fmt:yuvj422p.937x    
[swscaler @ 0x55aed5b56e80] deprecated pixel format used, make sure you did set range correctly
Input stream #0:0 frame changed from size:1280x720 fmt:yuvj422p to size:1280x720 fmt:grayrop=3023 speed=0.717x    
Input stream #0:0 frame changed from size:1280x720 fmt:gray to size:1280x720 fmt:yuvj422p
[swscaler @ 0x55aed550fc20] deprecated pixel format used, make sure you did set range correctly
Input stream #0:0 frame changed from size:1280x720 fmt:yuvj422p to size:1280x720 fmt:grayrop=6171 speed=0.625x    
frame= 7156 fps= 16 q=-1.0 Lsize=   80319kB time=00:04:46.12 bitrate=2299.6kbits/s dup=0 drop=6525 speed=0.621x    
video:80233kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.107008%
[libx264 @ 0x55aed50b8300] frame I:30    Avg QP:23.39  size: 82468
[libx264 @ 0x55aed50b8300] frame P:2917  Avg QP:26.31  size: 21742
[libx264 @ 0x55aed50b8300] frame B:4209  Avg QP:28.37  size:  3864
[libx264 @ 0x55aed50b8300] consecutive B-frames:  0.9% 49.6% 37.1% 12.4%
[libx264 @ 0x55aed50b8300] mb I  I16..4:  7.3% 88.2%  4.6%
[libx264 @ 0x55aed50b8300] mb P  I16..4:  2.7%  4.6%  0.4%  P16..4: 37.5% 12.7% 10.4%  0.0%  0.0%    skip:31.7%
[libx264 @ 0x55aed50b8300] mb B  I16..4:  0.7%  1.1%  0.1%  B16..8: 33.4%  2.7%  0.3%  direct: 1.0%  skip:60.5%  L0:58.0% L1:37.1% BI: 4.9%
[libx264 @ 0x55aed50b8300] 8x8 transform intra:61.4% inter:88.3%
[libx264 @ 0x55aed50b8300] coded y,uvDC,uvAC intra: 34.6% 27.8% 4.0% inter: 16.8% 5.4% 0.1%
[libx264 @ 0x55aed50b8300] i16 v,h,dc,p: 36% 21% 17% 27%
[libx264 @ 0x55aed50b8300] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 21% 13% 42%  3%  4%  4%  5%  3%  4%
[libx264 @ 0x55aed50b8300] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 12% 19%  6%  8%  9%  7%  6%  5%
[libx264 @ 0x55aed50b8300] i8c dc,h,v,p: 75% 11% 13%  1%
[libx264 @ 0x55aed50b8300] Weighted P-Frames: Y:34.5% UV:6.4%
[libx264 @ 0x55aed50b8300] ref P L0: 41.0% 13.8% 22.0% 17.7%  5.5%
[libx264 @ 0x55aed50b8300] ref B L0: 39.0% 50.9% 10.1%
[libx264 @ 0x55aed50b8300] ref B L1: 84.5% 15.5%
[libx264 @ 0x55aed50b8300] kb/s:2296.19

提前感谢您的帮助。

使用 -framerate 而不是 -r。这是您遇到的主要问题。这是 image demuxer 的正确选项。 -r 可用作输出选项,但如果与图像分离器一起使用而不是 -framerate 会引入奇怪的行为。

最新版本的 ffmpeg 没有这个问题,不会向您发送垃圾邮件 frame changed from... 消息。开发正在进行中,而您的 ffmpeg 已经过时,因此我建议无论如何都要进行更新。 Downloading is simple, but you can also compile 如果你愿意的话。

您不需要声明输入宽 x 高,但如果您声明了,您应该在图像分离器中使用 -video_size 而不是 -s。不需要 -f image2。您的 glob 语法有点错误。通过这些调整,您的命令可能如下所示:

ffmpeg -framerate 25 -pattern_type glob -i "/data/camipjpg/*.jpg" -c:v libx264 -crf 25 -vf format=yuv420p -movflags +faststart /data/camipmgpg/20190110.mp4