Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get Thumbnail Image from Video Using FFMPEG in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.27k
    Comment on it

    FFMPEG is a command line tool to convert multimedia files to another formats.
    In this article, we will see how to get thumbnail image from a video using FFMPEG in C#.

     

    Step 1: Create a page to show the thumbnail image of a video

          

    <div>
          <asp:FileUpload ID="FileUpload1" runat="server" />
          <asp:Button ID="btnGetThumbImage" runat="server" Text="GetFile" OnClick="btnGetThumbImage_Click" />
          <div>
                <asp:Image ID="imgThumbnail" runat="server" Visible="false" />
          </div>
    </div>


            

    Step 2: Write the FFMPEG exe path using web.config in <appSettings> section
     

     <add key ="FFMPEGExePath" value="D:\ffmpeg\bin\ffmpeg.exe"/>

            

    Step 3: Upload a video and save it. Get video uploaded path and thumbnail path and pass the same to the function to get the thumbnail image with the name passed to the function and display it.

     

    protected void btnGetThumbImage_Click(object sender, EventArgs e)
        {
            string fileName = FileUpload1.PostedFile.FileName;
            string userAlbumPath = String.Format("~/UserAlbums/");
            FileUpload1.SaveAs(Server.MapPath(userAlbumPath + fileName));
            string videoPath = Server.MapPath(String.Format("~/UserAlbums/{0}", fileName));
     
            string thumbnailPath = Server.MapPath(String.Format("~/UserAlbums/"));
            string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            string thumbImage = GetThumbnailImage(videoPath, thumbnailPath, newFileName);
            if (thumbImage != string.Empty)
            {
                imgThumbnail.Visible = true;
                System.Threading.Thread.Sleep(2000);
                imgThumbnail.ImageUrl = String.Format("~/UserAlbums/{0}", newFileName  +".jpg");
            }
    
        }        

      

    Step 4: Get the thumbnail image using FFMPEG

     

    public string GetThumbnailImage(string videoPath, string thumbnailPath, string fileName)
        {
            try
            {
                string thumb = thumbnailPath + fileName + ".jpg";
               
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo.FileName = Convert.ToString(ConfigurationManager.AppSettings["FFMPEGExePath"]);
                proc.StartInfo.Arguments = " -i \"" + videoPath + "\" -f image2 -ss 0:0:1 -vframes 1 -s 150x150 -an \"" + thumb + "\"";
                //The command which will be executed
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.RedirectStandardOutput = false;
                proc.Start();
    
                return thumb;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }    


     This will seek to the position of 0h:0m:1sec into the video and extract one frame (-vframes 1) from that position of 150*150 size .
       

    Hope this will help you!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: