How to convert video files into swf formate in Asp.net


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
using QuartzTypeLib;
public partial class uploadvideo : System.Web.UI.Page
{
string postedfilename, SavePath, Filenamewithpath;
private IMediaPosition m_objMediaPosition = null;
private FilgraphManager m_objFilterGraph = null;
protected void Page_Load(object sender, EventArgs e)
{
string y = Convert.ToString(Session[“uname”]);
if (Convert.ToString(Session[“uname”]) == “”)
{
Response.Redirect(“~/login.aspx”);
}
getcategories();
}
public void getcategories()
{
SqlConnection con = new SqlConnection(“Data Source=SOHOY39;Initial Catalog=vyas;User ID=sa;Password=123123”);
con.Open();
SqlCommand login = new SqlCommand(“select video_category from video_categories”, con);
SqlDataAdapter da = new SqlDataAdapter(“select video_category from video_categories”, con);
SqlDataReader dr;
dr = login.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr.GetString(0));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int vid_id;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“TestConnectionString”].ConnectionString);
con.Open();
SqlCommand vidid = new SqlCommand(“select max(video_id+1) from videos”, con);
SqlDataReader dr = vidid.ExecuteReader();
dr.Read();
vid_id = dr.GetInt32(0);
Session[“vidid”] = vid_id.ToString();
//SqlCommand insert = new SqlCommand(“insert into videos values(‘” + vid_id + “‘,'” + video_save_path + “‘,'” + video_thumb + “‘,'” + Session[“uname”] + “‘,'” + dt.ToShortDateString() + “‘,'” + txtkeywords.Text + “‘,'” + DropDownList1.SelectedItem.Text + “‘)”, con);
con.Close();
//new code
if (FileUpload1 != null || FileUpload1.PostedFile != null)
{
postedfilename = FileUpload1.PostedFile.FileName;
m_objFilterGraph = new FilgraphManager();
m_objFilterGraph.RenderFile(FileUpload1.PostedFile.FileName);
m_objMediaPosition = m_objFilterGraph as IMediaPosition;
// for total time -start
int s = (int)m_objMediaPosition.Duration;
int h = s / 3600;
int m = (s – (h * 3600)) / 60;
s = s – (h * 3600 + m * 60);
string duration = String.Format(“{0:D2}:{1:D2}:{2:D2}”, h, m, s);
// end
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\\” + vid_id + “.swf”;
Session[“outputfile1”] = outputfile.ToString();
Session[“outputfile”] = vid_id + “.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\\”;
string dbthumbname = thumbpath + vid_id +”1.jpg”;
Session[“dbthumbname”] = dbthumbname.ToString();
thumbname = thumbpath + vid_id + “%d” + “.jpg”;
Session[“thumbname”] = vid_id + “1” + “.jpg”;
thumbargs = “-i ” + inputfile + ” -vframes 1 -ss 00:00:07 -s 150×150 ” + 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);
Savedetails(duration);
RegularExpressionValidator2.Visible = false;
lblmsg.Text = “Video Uploaded Successfully”;
lblmsg.Visible = true;
//hyp.NavigateUrl = “Play.aspx”;
}
}
private void Savedetails(string duration)
{
DateTime dt = new DateTime();
dt = DateTime.Now;
string filename, imgname;
filename = Session[“outputfile”].ToString();
imgname = Session[“thumbname”].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“TestConnectionString”].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(“insert into videos values(‘” + Convert.ToString(Session[“vidid”]) + “‘,'” + Convert.ToString(Session[“outputfile1”]) + “‘,'” + Convert.ToString(Session[“dbthumbname”]) + “‘,'” + txttitle.Text + “‘,'” + Convert.ToString(Session[“uname”]) + “‘,'” + dt + “‘,'” + txtkeywords.Text.Trim() + “‘,'” + DropDownList1.SelectedItem.Text + “‘,'” + duration + “‘)”, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Diagnostics;
using QuartzTypeLib;
public partial class uploadvideo : System.Web.UI.Page
{
string postedfilename, SavePath, Filenamewithpath;
private IMediaPosition m_objMediaPosition = null;
private FilgraphManager m_objFilterGraph = null;
protected void Page_Load(object sender, EventArgs e)
{
string y = Convert.ToString(Session[“uname”]);
if (Convert.ToString(Session[“uname”]) == “”)
{
Response.Redirect(“~/login.aspx”);
}
getcategories();
}
public void getcategories()
{
SqlConnection con = new SqlConnection(“Data Source=SOHOY39;Initial Catalog=vyas;User ID=sa;Password=123123”);
con.Open();
SqlCommand login = new SqlCommand(“select video_category from video_categories”, con);
SqlDataAdapter da = new SqlDataAdapter(“select video_category from video_categories”, con);
SqlDataReader dr;
dr = login.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr.GetString(0));
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int vid_id;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“TestConnectionString”].ConnectionString);
con.Open();
SqlCommand vidid = new SqlCommand(“select max(video_id+1) from videos”, con);
SqlDataReader dr = vidid.ExecuteReader();
dr.Read();
vid_id = dr.GetInt32(0);
Session[“vidid”] = vid_id.ToString();
//SqlCommand insert = new SqlCommand(“insert into videos values(‘” + vid_id + “‘,'” + video_save_path + “‘,'” + video_thumb + “‘,'” + Session[“uname”] + “‘,'” + dt.ToShortDateString() + “‘,'” + txtkeywords.Text + “‘,'” + DropDownList1.SelectedItem.Text + “‘)”, con);
con.Close();
//new code
if (FileUpload1 != null || FileUpload1.PostedFile != null)
{
postedfilename = FileUpload1.PostedFile.FileName;
m_objFilterGraph = new FilgraphManager();
m_objFilterGraph.RenderFile(FileUpload1.PostedFile.FileName);
m_objMediaPosition = m_objFilterGraph as IMediaPosition;
// for total time -start
int s = (int)m_objMediaPosition.Duration;
int h = s / 3600;
int m = (s – (h * 3600)) / 60;
s = s – (h * 3600 + m * 60);
string duration = String.Format(“{0:D2}:{1:D2}:{2:D2}”, h, m, s);
// end
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\\” + vid_id + “.swf”;
Session[“outputfile1”] = outputfile.ToString();
Session[“outputfile”] = vid_id + “.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\\”;
string dbthumbname = thumbpath + vid_id +”1.jpg”;
Session[“dbthumbname”] = dbthumbname.ToString();
thumbname = thumbpath + vid_id + “%d” + “.jpg”;
Session[“thumbname”] = vid_id + “1” + “.jpg”;
thumbargs = “-i ” + inputfile + ” -vframes 1 -ss 00:00:07 -s 150×150 ” + 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);
Savedetails(duration);
RegularExpressionValidator2.Visible = false;
lblmsg.Text = “Video Uploaded Successfully”;
lblmsg.Visible = true;
//hyp.NavigateUrl = “Play.aspx”;
}
}
private void Savedetails(string duration)
{
DateTime dt = new DateTime();
dt = DateTime.Now;
string filename, imgname;
filename = Session[“outputfile”].ToString();
imgname = Session[“thumbname”].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[“TestConnectionString”].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(“insert into videos values(‘” + Convert.ToString(Session[“vidid”]) + “‘,'” + Convert.ToString(Session[“outputfile1”]) + “‘,'” + Convert.ToString(Session[“dbthumbname”]) + “‘,'” + txttitle.Text + “‘,'” + Convert.ToString(Session[“uname”]) + “‘,'” + dt + “‘,'” + txtkeywords.Text.Trim() + “‘,'” + DropDownList1.SelectedItem.Text + “‘,'” + duration + “‘)”, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
}
Advertisements

2 thoughts on “How to convert video files into swf formate in Asp.net

Thanks a lot for visiting this site .. Please leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s