create thumbnail from video in asp.net
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 开发者_如何学Python(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.
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.
Add a reference to "DexterLib" library to your project.
Add a reference to the following libraries:
using System.Drawing.Imaging; using System.Runtime.InteropServices; using DexterLib;
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
精彩评论