如何将一批实时图像转换和压缩为mp4视频文件



在我的硬盘目录中,我有很多图像:screenshot000001.bmp,screenshot00002.bmp……screenshot001200.bmp

我想做两件事:

  1. 用于使用cmd(命令提示符)进行测试,并将图像压缩和转换为mp4视频文件。

  2. 在我的程序中实时拍摄屏幕截图并将其保存到硬盘上,以压缩它们并实时构建mp4视频文件。

在第一部分中,我尝试键入cmd:

ffmpeg -f image2 -i screenshot%d.bmp -vcodec libx264 -b 800k video.avi

但我得到的是两个错误:

[image2@000000000 4766380]找不到路径为"screens截图%d.bmp"的文件,并且索引在0-4范围内屏幕截图%d.bmp:没有这样的文件或目录

我将ffmpeg.exe复制到图像所在的目录中。E: \屏幕截图

对于第二部分,我是如何实时截图的:

启动计时器的按钮点击事件:

private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

然后在勾选事件中:

    ScreenShot shot = new ScreenShot();
    public static int counter = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        counter++;
        shot.GetScreenShot(@"e:screenshots", "screenshot");
        if (counter == 1200)
        {
            timer1.Stop();
        }
    }

这是一次直线射击。GetScreenShot(@"e:\screenshots\","screenshot");将屏幕截图保存到硬盘。在每次保存屏幕截图后,我想实时压缩和构建mp4视频文件。

我尝试了这个,但出现了错误:

ffmpeg -f image2 -i screenshot%06d.bmp -vcodec libx264 -b 800k video.avi

错误消息:

ffmpeg version N-73165-gf1e1730 Copyright (
  built with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-vers
isynth --enable-bzlib --enable-fontconfig -
le-iconv --enable-libass --enable-libbluray
enable-libdcadec --enable-libfreetype --ena
ibilbc --enable-libmodplug --enable-libmp3l
le-libopencore-amrwb --enable-libopenjpeg -
able-libschroedinger --enable-libsoxr --ena
ble-libtwolame --enable-libvidstab --enable
 --enable-libvorbis --enable-libvpx --enabl
e-libx264 --enable-libx265 --enable-libxavs
ble-decklink --enable-zlib
  libavutil      54. 27.100 / 54. 27.100
  libavcodec     56. 45.100 / 56. 45.100
  libavformat    56. 38.102 / 56. 38.102
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 18.100 /  5. 18.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.100 /  1.  2.100
  libpostproc    53.  3.100 / 53.  3.100
[bmp @ 0000000002f77ce0] bad magic number
    Last message repeated 3 times
[image2 @ 0000000002f76380] decoding for st
[image2 @ 0000000002f76380] Could not find
 bmp, none): unspecified size
Consider increasing the value for the 'anal
screenshot%06d.bmp: could not find codec pa
Input #0, image2, from 'screenshot%06d.bmp'
  Duration: 00:00:02.88, start: 0.000000, b
    Stream #0:0: Video: bmp, none, 25 fps,
Please use -b:a or -b:v, -b is ambiguous
Codec AVOption b (set bitrate (in bits/s))
vi) has not been used for any stream. The m
e (e.g. a video option with no video stream
some encoder which was not actually used fo
Output #0, avi, to 'video.avi':
Output file #0 does not contain any stream

尝试将screenshot%d.bmp更改为screenshot%06d.bmp

我注意到,您使用的是Windows和.NET,但如果您不介意安装node.js,它有出色的实用程序来处理转换过程,node fluent ffmpeg可以为您处理许多输出选项。

更新:

好吧,这就是使用视频编码器的可悲现实——现在有更多的问题,大多数都在错误消息中描述,比如:

尝试使用-b:v代替-b来指示视频比特率。

[bmp @ 0000000002f77ce0] bad magic number

很难说这到底意味着什么,但我注意到ffmpeg在图像格式方面相当挑剔。如果你可以尝试运行转换(ImageMagic)到图像,并将格式更改为PNG或JPEG,效果可能会更好。

最新更新