代码之家  ›  专栏  ›  技术社区  ›  Sandeep Kumar M

如何将视频的第一帧保存为图像?

  •  10
  • Sandeep Kumar M  · 技术社区  · 16 年前

    我想提取上传视频的第一帧,并保存为图像文件。
    可能的视频格式有mpeg、avi和wmv。

    还有一件事需要考虑的是,我们正在创建一个ASP.NET网站。

    3 回复  |  直到 16 年前
        1
  •  7
  •   Cipi    16 年前

    您可以使用FFMPEG作为一个单独的进程(最简单的方法),并让它为您解码第一个IDR。这里有一个类FFMPEG,它有GetThumbnail()方法,向它传递视频文件的地址、要生成的JPEG图像的地址和图像的分辨率:

    using System.Diagnostics;
    using System.Threading; 
    
    public class FFMPEG
    {
        Process ffmpeg;
    
        public void exec(string input, string output, string parametri)
        {
            ffmpeg = new Process();
    
            ffmpeg.StartInfo.Arguments = " -i " + input+ (parametri != null? " "+parametri:"")+" "+output; 
            ffmpeg.StartInfo.FileName = "utils/ffmpeg.exe";
            ffmpeg.StartInfo.UseShellExecute = false;
            ffmpeg.StartInfo.RedirectStandardOutput = true;
            ffmpeg.StartInfo.RedirectStandardError = true;
            ffmpeg.StartInfo.CreateNoWindow = true;
    
            ffmpeg.Start();
            ffmpeg.WaitForExit();
            ffmpeg.Close();     
        }
    
    
        public void GetThumbnail(string video, string jpg, string velicina)
        {
            if (velicina == null) velicina = "640x480";
            exec(video, jpg, "-s "+velicina);
        }
    }
    

    FFMPEG f = new FFMPEG();
    f.GetThumbnail("videos/myvid.wmv", "images/thumb.jpg", "1200x223");
    

    要想让它起作用,你必须ffmpeg.exe或更改代码以查找ffmpeg.exe.

    在.NET中使用FFMPEG还有其他方法,比如.NET包装器,你可以在谷歌上搜索它们。他们基本上在这里做同样的事情,只是更好。所以如果FFMPEG完成了您的工作,我建议您使用.NET包装器。

        2
  •  3
  •   eeerahul Stalin Pimentel    14 年前

    ffmpeg.StartInfo.Arguments =" -i c:\MyPath\MyVideo -vframes 1 c:\MyOutputPath\MyImage%d.jpg"
    

    而不是

    ffmpeg.StartInfo.Arguments = " -i " + input+ (parametri != null? " "+parametri:"")+" "+output;
    

    我不知道是什么原因,但是第二个提到的参数行在我的机器上不起作用,而当我像第一个命令一样更改参数时,它工作正常。

        3
  •  -1
  •   Piskvor left the building Rohit Kumar    16 年前

    用编程方式处理视频的最佳工具可能是FFMpeg。它支持多种格式,甚至wmv。我怀疑甚至有 .net wrapper

    推荐文章