Sunday 16 June 2013

how to create video thumbnail in asp.net

Press Me

Press Me 
solution explore looks like:
webconfig:
<system.web>
    <httpRuntime executionTimeout="110" maxRequestLength="50000" requestLengthDiskThreshold="80" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="5000" enableKernelOutputCache="true" enableVersionHeader="true" requireRootedSaveAsPath="true" enable="true" shutdownTimeout="90" delayNotificationTimeout="5" waitChangeNotification="0" maxWaitChangeNotification="0" enableHeaderChecking="true" sendCacheControlHeader="true" apartmentThreading="false"/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
  </system.web>

design code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center">
        <tr><td colspan="2"><asp:Label ID="lb" runat="server"></asp:Label></td></tr>
            <tr>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
                <td>
                    <asp:Button ID="btn" runat="server" Text="Upload" OnClick="btn_Click" />
                </td>
            </tr>
            <tr><td>
             <asp:PlaceHolder ID="plh" runat="server"></asp:PlaceHolder>
            </td></tr>
            <tr>
                <td colspan="2">
                <asp:DataList ID="dl" runat="server" Width="100%">
                <ItemTemplate>
                <asp:ImageButton ID="im" runat="server" Height="50" Width="50"
                        ImageUrl='<%#"~/Video/Thumb/"+Eval("IMAGE") %>'
                        CommandArgument='<%#Eval("ID") %>' oncommand="im_Command" />
                </ItemTemplate>
                </asp:DataList>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
c# code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
    #region
    string postedfilename, SavePath, Filenamewithpath;
    DataClassesDataContext obj = new DataClassesDataContext();
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            fill();
    }
    void fill()
    {
        dl.DataSource = obj.EMPs.Select(b => new {b.IMAGE,b.ID});
        dl.DataBind();
        dl.RepeatColumns = obj.EMPs.ToList().Count;
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        postedfilename = FileUpload1.PostedFile.FileName;
        SavePath = AppDomain.CurrentDomain.BaseDirectory + "Video\\";
        string NewFName = postedfilename;
        NewFName = NewFName.Substring(NewFName.LastIndexOf("\\") + 1, NewFName.Length - NewFName.LastIndexOf(".")) + "." + NewFName.Substring(NewFName.LastIndexOf(".") + 1);
        Filenamewithpath = SavePath + NewFName;

        //Save The file
        FileUpload1.PostedFile.SaveAs(Filenamewithpath);

        //Start Converting
        string inputfile, outputfile, filargs;
        string withoutext;

        //Get the file name without Extension
        withoutext = Path.GetFileNameWithoutExtension(Filenamewithpath);

        //Input file path of uploaded image
        inputfile = SavePath + NewFName;

        //output file format in swf
        outputfile = SavePath + "SWF\\" + withoutext + ".swf";
        ViewState["vedio"] = withoutext + ".swf";
        //file orguments for FFMEPG
        filargs = "-i " + inputfile + " -ar 22050 " + outputfile;

        string spath;
        spath = Server.MapPath(".");
        Process proc;
        proc = new Process();
        proc.StartInfo.FileName = spath + "\\ffmpeg\\ffmpeg.exe";
        proc.StartInfo.Arguments = filargs;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = false;
        proc.StartInfo.RedirectStandardOutput = false;
        try
        {

            proc.Start();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        proc.WaitForExit();
        proc.Close();




        //Create Thumbs

        string thumbpath, thumbname;
        string thumbargs;
        string thumbre;
        thumbpath = AppDomain.CurrentDomain.BaseDirectory + "\\Video\\Thumb\\";
        thumbname = thumbpath + withoutext + "%d" + ".jpg";
        ViewState["image"] = withoutext + "1" + ".jpg";
        thumbargs = "-i " + inputfile + " -vframes 1 -ss 00:00:07 -s 150x150 " + thumbname;
        Process thumbproc = new Process();
        thumbproc = new Process();
        thumbproc.StartInfo.FileName = spath + "\\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();
        File.Delete(inputfile);
        EMP o = new EMP();
        o.IMAGE = ViewState["image"].ToString();
        o.VEDIO = ViewState["vedio"].ToString();
        obj.EMPs.InsertOnSubmit(o);
        obj.SubmitChanges();
        fill();
        lb.Text = "Data Save sucessfully.";
    }
    protected void im_Command(object sender, CommandEventArgs e)
    {
        string vfname = obj.EMPs.Where(b => b.ID == Convert.ToInt32(e.CommandArgument.ToString())).Select(b => new { b.VEDIO }).ToList()[0].VEDIO.ToString();
        string plyr = "<embed src='Players/flvplayer.swf' width='425' height='355' bgcolor='#FFFFFF' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' flashvars='file=Video/SWF/" + vfname + "&autostart=false&frontcolor=0xCCCCCC&backcolor=0x000000&lightcolor=0x996600&showdownload=false&showeq=false&repeat=false&volume=100&useaudio=false&usecaptions=false&usefullscreen=true&usekeys=true'></embed>";
        plh.Controls.Add(new LiteralControl(plyr));
    }
}
 page looks like:

press me

No comments:

Post a Comment