【问题标题】:create thumbnail from video in asp.net从 asp.net 中的视频创建缩略图
【发布时间】:2011-09-03 07:51:23
【问题描述】:

我想在我的应用程序中创建上传视频文件的缩略图,为此我使用了ffmpeg。代码如下:

 string thumbpath, thumbname;
        string thumbargs;
        string thumbre;
        thumbpath = Server.MapPath("~/thumbnails/");
        thumbname = thumbpath + "Test" + ".png";
        string f = Server.MapPath("~/testvideo.wmv");
        thumbargs = "-i " + f + " -vcodec png -vframes 1 -ss 00:00:07 -s 150x150 " + thumbname;
        Process thumbproc = new Process();
        thumbproc = new Process();
        thumbproc.StartInfo.FileName = Server.MapPath("~\\ffmpeg\\ffmpeg.exe");
        thumbproc.StartInfo.Arguments = thumbargs;
        thumbproc.StartInfo.UseShellExecute = false;
        thumbproc.StartInfo.CreateNoWindow = false;
        thumbproc.StartInfo.RedirectStandardOutput = false;
        try
        {
            thumbproc.Start();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        thumbproc.WaitForExit();
        thumbproc.Close();

但它抛出了一个异常:

MainModule = 'thumbproc.MainModule'
threw an exception of type 'System.ComponentModel.Win32Exception'

如果有人有想法,请告知。

【问题讨论】:

    标签: asp.net video thumbnails


    【解决方案1】:

    我建议使用“DexterLib”库来执行此操作。我已经使用这个库在托管环境中生成视频剪辑的缩略图,它就像一个魅力。

    这是您需要的。

    1. 在您的项目中添加对“DexterLib”库的引用。

    2. 添加对以下库的引用:

      using System.Drawing.Imaging;
      using System.Runtime.InteropServices;
      using DexterLib;
      
    3. 使用以下代码抓取一帧并生成缩略图:

      try
      {
          MediaDetClass loMD = new MediaDetClass();
          loMD.Filename = lsServerPath;
          loMD.CurrentStream = 0;
          loMD.WriteBitmapBits(0, 150, 150, lsFBitmapName);
      
          System.Drawing.Image loImg = System.Drawing.Image.FromFile(lsFBitmapName);
          loImg.Save(lsFJpegName, ImageFormat.Jpeg);
          loImg.Dispose();
          System.IO.File.Delete(lsFBitmapName);
      }
      catch (Exception loEx)
      {
          // Means media not supported
      }
      

    “WriteBitmapBits”方法按顺序将视频的“流时间、宽度、高度和文件名”作为输入参数,然后您可以使用该参数将缩略图保存为 JPG。如果您有任何其他问题,请告诉我。如果你想看到它的实际效果,你可以看看我在这里的一个开源门户项目:

    http://digioznetportal.svn.sourceforge.net/viewvc/digioznetportal/digioznetportal_1.1/DigiOz%20.NET%20Portal%201.1/trunk/

    单击“下载 GNU Tarball”链接以下载整个源代码,然后查看“controls/VideoUpload.ascx.cs”并逐步查看该代码以了解其工作原理。

    我希望这会有所帮助。

    谢谢, 皮特

    【讨论】:

    • 您能解释一下lsServerPath、lsFBitmapName 和lsFJpegName 是如何定义网址和路径的吗?
    猜你喜欢
    • 2011-08-11
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2012-05-19
    相关资源
    最近更新 更多