![如何从视频中获取帧而不丢失其中的一些?](https://linux22.com/image/1352992/%E5%A6%82%E4%BD%95%E4%BB%8E%E8%A7%86%E9%A2%91%E4%B8%AD%E8%8E%B7%E5%8F%96%E5%B8%A7%E8%80%8C%E4%B8%8D%E4%B8%A2%E5%A4%B1%E5%85%B6%E4%B8%AD%E7%9A%84%E4%B8%80%E4%BA%9B%EF%BC%9F.png)
我用 30 张 PNG 图像、15 帧/秒创建了一个 AVI 文件:
ffmpeg -i %02d.png -r 15 video.avi
但当我尝试从 AVI 中获取图像时:
ffmpeg -i src.avi %02d.png
我只得到了 19 张图片,而不是 30 张!我还尝试了以下两种方法:
ffmpeg -i src.avi -r 15 %02d.png
ffmpeg -r 15 -i src.avi %02d.png
两者结果相同。不用说,有缺失的帧(例如从第 4 帧跳到第 6 帧)。这是怎么回事?我该如何解决?
第一个命令的输出(ffmpeg -i %02d.png -r 15 video.avi
):
ffmpeg version N-50314-gf6fff8e Copyright (c) 2000-2013 the FFmpeg developers
built on Feb 25 2013 21:57:41 with gcc 4.7.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 17.103 / 52. 17.103
libavcodec 54. 92.100 / 54. 92.100
libavformat 54. 63.100 / 54. 63.100
libavdevice 54. 3.103 / 54. 3.103
libavfilter 3. 41.100 / 3. 41.100
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
Input #0, image2, from '%02d.png':
Duration: 00:00:01.20, start: 0.000000, bitrate: N/A
Stream #0:0: Video: png, rgb24, 713x386 [SAR 3779:3779 DAR 713:386], 25 fps, 25 tbr, 25 tbn, 25 tbc
Output #0, avi, to 'video.avi':
Metadata:
ISFT : Lavf54.63.100
Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p, 713x386 [SAR 1:1 DAR 713:386], q=2-31, 200 kb/s, 15 tbn, 15 tbc
Stream mapping:
Stream #0:0 -> #0:0 (png -> mpeg4)
Press [q] to stop, [?] for help
frame= 19 fps=0.0 q=2.0 Lsize= 46kB time=00:00:01.26 bitrate= 297.1kbits/s dup=0 drop=11
video:40kB audio:0kB subtitle:0 global headers:0kB muxing overhead 15.248175%
第二条命令的输出(ffmpeg -i video.avi %02d.png
):
ffmpeg version N-50314-gf6fff8e Copyright (c) 2000-2013 the FFmpeg developers
built on Feb 25 2013 21:57:41 with gcc 4.7.2 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype --enable-libgsm --enable-libilbc --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 17.103 / 52. 17.103
libavcodec 54. 92.100 / 54. 92.100
libavformat 54. 63.100 / 54. 63.100
libavdevice 54. 3.103 / 54. 3.103
libavfilter 3. 41.100 / 3. 41.100
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
Input #0, avi, from 'video.avi':
Metadata:
encoder : Lavf54.63.100
Duration: 00:00:01.27, start: 0.000000, bitrate: 297 kb/s
Stream #0:0: Video: mpeg4 (Simple Profile) (FMP4 / 0x34504D46), yuv420p, 713x386 [SAR 1:1 DAR 713:386], 15 tbr, 15 tbn, 15 tbc
Output #0, image2, to '%02d.png':
Metadata:
encoder : Lavf54.63.100
Stream #0:0: Video: png, rgb24, 713x386 [SAR 1:1 DAR 713:386], q=2-31, 200 kb/s, 90k tbn, 15 tbc
Stream mapping:
Stream #0:0 -> #0:0 (mpeg4 -> png)
Press [q] to stop, [?] for help
frame= 19 fps=0.0 q=0.0 Lsize=N/A time=00:00:01.26 bitrate=N/A
video:129kB audio:0kB subtitle:0 global headers:0kB muxing overhead -100.016665%
答案1
输入和输出的帧速率相同
用作-framerate
输入选项来设置图像输入的帧速率。例如:
ffmpeg -framerate 15 -i %02d.png -pix_fmt yuv420p output.mp4
如果省略此选项,则将应用默认值-framerate 25
。如果您仅提供输入帧速率(如上例所示),则 ffmpeg 将继承相同的输出帧速率,而不会丢失或重复帧。
-pix_fmt yuv420p
建议在输出 H.264 视频时添加兼容性。
输入和输出的帧速率不同
如果您想改变输入帧率和输出帧率,请添加-r
输出选项。如果您希望输入具有较低的帧率,但为了兼容性而具有更标准的输出帧率,这可能会很有用:
ffmpeg -framerate 1 -i input_%03d.png -r 24 -pix_fmt youv420p output.mp4
上面的例子将每秒显示一个输入帧,但输出帧将被复制,因此输出实际上将以每秒 24 帧的速度播放。