2013-03-29 154 views
22

我想要顯示我網站上列出的視頻的縮略圖,我想從視頻中獲取單個幀(從特定時間)並將它們顯示爲縮略圖。獲取C#中的視頻文件的縮略圖圖像#

我試試這個http://ramcrishna.blogspot.com/2008/09/playing-videos-like-youtube-and.html,但不工作。

使用.NET C#可以嗎?

+1

HTTP的可能重複:// stackoverflow.com/questions/155314/how-do-i-get-a-video-thumbnail-in-net –

+1

定義「不工作」。那麼解決方案不適合你嗎? – David

回答

12

您可以通過編程方式執行FFmpeg來生成縮略圖圖像文件。然後打開圖像文件,然後根據需要使用它。

下面是一些示例代碼:

public static Bitmap GetThumbnail(string video, string thumbnail) 
{ 
    var cmd = "ffmpeg -itsoffset -1 -i " + '"' + video + '"' + " -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 " + '"' + thumbnail + '"'; 

    var startInfo = new ProcessStartInfo 
    { 
     WindowStyle = ProcessWindowStyle.Hidden, 
     FileName = "cmd.exe", 
     Arguments = "/C " + cmd 
    }; 

    var process = new Process 
    { 
     StartInfo = startInfo 
    }; 

    process.Start(); 
    process.WaitForExit(5000); 

    return LoadImage(thumbnail); 
} 

static Bitmap LoadImage(string path) 
{ 
    var ms = new MemoryStream(File.ReadAllBytes(path)); 
    return (Bitmap)Image.FromStream(ms); 
} 
+2

爲什麼'WaitForExit(5000) '? –

+0

@PaRiMaLRaJ你試過這段代碼嗎?你必須傳遞什麼參數'thumbnail'?我甚至創建了一個擴展名爲.bmp的文件,並通過了(C:\ Users \ Public \ Videos \ Sample Videos \ test1 .bmp)縮略圖,但它給我一個錯誤在代碼的最後一行=「參數無效」。你能幫我嗎。在此先感謝 – CodeEngine

+0

此程序是否會運行在服務器/遠程虛擬機上部署的應用程序。 我打算爲用戶在我的網站上上傳的視頻生成縮略圖。位圖應該即時生成。 由於這將在服務器上運行,這種方法會工作嗎?儘管這在我的本地機器上完美工作。 – aditya

46

FFmpeg的是可用於在一些位置,以提取視頻幀中合適的工具。 如上所述,或只是利用現有的.NET包裝可以調用ffmpeg.exe(如Video converter for .NET(免費)獲得縮略圖只用一行代碼:

var ffMpeg = new NReco.VideoConverter.FFMpegConverter(); 
ffMpeg.GetVideoThumbnail(pathToVideoFile, thumbJpegStream,5); 
+2

謝謝,最好的解決方案。 –

+1

我該如何使用它?我已經通過nuget添加了「Install-Package NReco.Application.Web」,但不能使用這些代碼行。 – gsiradze

+3

您已經安裝了錯誤的軟件包。正確的是「Install-Package NReco.VideoConverter」(NReco.Application.Web是關於ASP.NET應用程序的NReco框架)。 –

-1
[HttpPost] 
     [Route("UploadImages")] 
     public HttpResponseMessage Post() 
     { 
      HttpResponseMessage response = new HttpResponseMessage(); 
      var httpRequest = HttpContext.Current.Request; 
      if (httpRequest.Files.Count > 0) 
      { 
       var docfiles = new List<string>(); 
       foreach (string file in httpRequest.Files) 
       { 
        var postedFile = httpRequest.Files[file]; 
        var filePath1 = HttpContext.Current.Server.MapPath("~/ImgFolder/" + postedFile.FileName); 

        Stream strm = postedFile.InputStream; 

        CreateThumbnail(strm, postedFile.FileName); 

        Compressimage(strm, filePath1, postedFile.FileName); 


       } 
       response = Request.CreateResponse(HttpStatusCode.Created, docfiles); 
      } 
      else 
      { 
       response = Request.CreateResponse(HttpStatusCode.BadRequest); 
      } 
      return response; 
     } 
     public static void **CreateThumbnail**(Stream sourcePath, string filename) 
     { 
      Image image = Image.FromStream(sourcePath); 
      Image thumb = image.GetThumbnailImage(120, 120,() => false, IntPtr.Zero); 
      var filePath1 = HttpContext.Current.Server.MapPath("~/Thumbnail/" + filename); 

      thumb.Save(filePath1 + filename); 

     } 

     public static void Compressimage(Stream sourcePath, string targetPath, String filename) 
     { 


      try 
      { 
       using (var image = Image.FromStream(sourcePath)) 
       { 
        float maxHeight = 900.0f; 
        float maxWidth = 900.0f; 
        int newWidth; 
        int newHeight; 
        string extension; 
        Bitmap originalBMP = new Bitmap(sourcePath); 
        int originalWidth = originalBMP.Width; 
        int originalHeight = originalBMP.Height; 

        if (originalWidth > maxWidth || originalHeight > maxHeight) 
        { 

         // To preserve the aspect ratio 
         float ratioX = (float)maxWidth/(float)originalWidth; 
         float ratioY = (float)maxHeight/(float)originalHeight; 
         float ratio = Math.Min(ratioX, ratioY); 
         newWidth = (int)(originalWidth * ratio); 
         newHeight = (int)(originalHeight * ratio); 
        } 
        else 
        { 
         newWidth = (int)originalWidth; 
         newHeight = (int)originalHeight; 

        } 
        Bitmap bitMAP1 = new Bitmap(originalBMP, newWidth, newHeight); 
        Graphics imgGraph = Graphics.FromImage(bitMAP1); 
        extension = Path.GetExtension(targetPath); 
        if (extension == ".png" || extension == ".gif") 
        { 
         imgGraph.SmoothingMode = SmoothingMode.AntiAlias; 
         imgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; 
         imgGraph.DrawImage(originalBMP, 0, 0, newWidth, newHeight); 


         bitMAP1.Save(targetPath, image.RawFormat); 

         bitMAP1.Dispose(); 
         imgGraph.Dispose(); 
         originalBMP.Dispose(); 
        } 
        else if (extension == ".jpg") 
        { 

         imgGraph.SmoothingMode = SmoothingMode.AntiAlias; 
         imgGraph.InterpolationMode = InterpolationMode.HighQualityBicubic; 
         imgGraph.DrawImage(originalBMP, 0, 0, newWidth, newHeight); 
         ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); 
         Encoder myEncoder = Encoder.Quality; 
         EncoderParameters myEncoderParameters = new EncoderParameters(1); 
         EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L); 
         myEncoderParameters.Param[0] = myEncoderParameter; 
         bitMAP1.Save(targetPath, jpgEncoder, myEncoderParameters); 

         bitMAP1.Dispose(); 
         imgGraph.Dispose(); 
         originalBMP.Dispose(); 

        } 


       } 

      } 
      catch (Exception) 
      { 
       throw; 

      } 
     } 


     public static ImageCodecInfo GetEncoder(ImageFormat format) 
     { 

      ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders(); 

      foreach (ImageCodecInfo codec in codecs) 
      { 
       if (codec.FormatID == format.Guid) 
       { 
        return codec; 
       } 
      } 
      return null; 
     } 
+1

這是使用webapi的圖像縮略圖。 –

+1

雖然此代碼片段可能會解決問題,但[包括解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。也請儘量不要使用解釋性註釋來擠佔代碼,因爲這會降低代碼和解釋的可讀性! – FrankerZ

+0

這不是一個答案。 OP想要獲得**視頻**的縮略圖。 – mbomb007