2011-05-27 85 views
2

我想在我的應用程序中創建已上傳的視頻文件的縮略圖,因爲我已使用ffmpeg。代碼如下:從asp.net中的視頻創建縮略圖

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' 

如果有人有一個想法,那麼請讓知道。

回答

0

我建議使用「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壓縮包」鏈接,下載整個源代碼,然後坐看一下「controls/VideoUpload.ascx.cs」,然後瀏覽一下代碼,看看它是如何工作的。

我希望這會有所幫助。

感謝, 皮特

+0

你能解釋一下什麼是'lsServerPath','lsFBitmapName'和'lsFJpegName'你怎麼定義的URL和路徑? – 2015-01-30 18:13:44