create thumbnail from video in asp.net

若如初见. 提交于 2019-12-21 21:34:48

问题


I want to create thumbnail of an uploaded video file in my application for this I have used ffmpeg. The code is as follows:

 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();

but it's throwing an exception :

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

If some one has an idea then please let know.


回答1:


I suggest using "DexterLib" library to do this. I have used this library to generate thumbnails of video clips in hosted environment, and it works like a charm.

Here is what you need.

  1. Add a reference to "DexterLib" library to your project.

  2. Add a reference to the following libraries:

    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;
    using DexterLib;
    
  3. Use the following code to grab a frame and generate a thumbnail:

    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
    }
    

The "WriteBitmapBits" method takes in the "Stream Time, Width, Height and FileName" for your video in that order as input parameter which you then use to save the thumbnail as JPG. Let me know if you have any additional questions. If you want to see it in action you can take a look at an Open Source Portal Project I have here:

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

Click on "Download GNU Tarball" link to download the whole source code, then take a look at the "controls/VideoUpload.ascx.cs" and step through that code to see how it works.

I hope this helps.

Thanks, Pete



来源:https://stackoverflow.com/questions/6151348/create-thumbnail-from-video-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!