Playing Videos - Part II

Friday, September 26, 2008

In Playing Videos-I I have been explained to upload a videos, how convert video from one format to another format and how to generate thumbnail automatically. In this post I am going to expalin how to retrive the videos and thumbnail dynamically from database.

First I am starting with DataBase part, Here I am creating a table called videos. And it contains 3 fields the are
Video_ID,Video_File,Video_Image. You can see the table design pic.




Here "Video_File" field contains Name of the video with extension. Here I am going to store the file name only. But the video file is located in server. "Video_Image" is the thumbnail file name with extension. Same as video thumbnail also located in server.

When I am uploading video, I am going to save the videofilename, Image name in database. Here I used very simple code to insert this details. Here is the code.....



private void Savedetails()
{
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 video(Video_File,Video_Image) values('" + filename + "','" + imgname + "')", con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}

You can use stored procedures in back end for the better performance. Once the video uploading is finished then I am going to show these details in DataList. Here is the code to bind the video thumbnail in Datalist



<asp:DataList ID="dtlstVideo" runat="server" GridLines="Vertical"
RepeatColumns="2" RepeatDirection="Horizontal" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="5">
<ItemTemplate>
<td><a href="Play.aspx?vid=<%# DataBinder.Eval(Container.DataItem, "Video_ID") %>">
<img src="Video/Thumb/<%# DataBinder.Eval(Container.DataItem, "Video_Image") %>" border="0" />
</a>
</td>
</ItemTemplate>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<AlternatingItemStyle BackColor="#DCDCDC" />
<ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
</asp:DataList>



when the user click on the datalist it will be redirected to play.aspx with video ID as the query string. In the
play.aspx page I am goign to bind the SWF player with video file name. Here is the code...


string vid,vfname;
protected void Page_Load(object sender, EventArgs e)
{
vid = Request.QueryString["vid"].ToString();
DataSet dst = (DataSet)Session["videods"];
DataRow[] dw = dst.Tables[0].Select("Video_ID= '"+ vid +"'");
if (dw.Length > 0)
{
vfname = dw[0]["Video_File"].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));
}

You can see the screen shot of playing a video dynamically..



Source Code






22 comments:

Unknown said...

Hi Ramky,

I have same kind of requirement how u did in the playing videos post.

i have downloaded it and converted it into vb.net as i am using vb.net to develop.

The thing is i am not getting any error and at the same time i am not getting the .swf and .jpg files in their respected folders.

i have installed AJAX in my system and i also have ffmpeg.exe in my solution.

kindly can u please help me solve this bcos it is urgent and i never worked with videos before.

Thanks

Regards
bunny

Unknown said...

Hi Ram,

Can u please give me any idea how to solve it.

Thanks
bunny

Ramky said...

Hello Bunny
I think u have to give the Read & Write permission to your working folder.

Try this one.. ok

Unknown said...

Hi Ramky,

Thanks for the code..its really working great.But here iam storing thumbnails in a folder.how can i access the images from that folder and place them in the page..
if i placed them in a page...then the user will click on the thumbail and then the video is going to be played..

can u help me on this issue..

venkata said...

hai ram

i could not able to bind the data and could not generate the thumb image ,i tried a lot and created tables and checked a lot but could not able to bind data and show thumb icon .so please help me its very urgent.

Vyas Pentakota said...

Hi Ram, good job,
but when am uploading a vid file fiel which is more that 1 mb , the app is not working.
can u give any solution for this issue. Eagerly waiting for ur reply
Thanks in advance.

Vyas Pentakota said...

when i upladed the same app in the server and when am accessing in the client machine its not working what might be the reason are there any constraints on the file size during uploading....pls do reply me its urgent

Thnks in advance

Vyas Pentakota,

simple money technique said...

Hi Ramky,

I downloaded it and it works perfect,
can you please help me to categorize it. For eg: Metacafe website.

For instance,If i choose comedy category,it should uploaded in comedy folder,and it should stream. please help me with this.

Thanks in advance.

Onkar said...

Hi,
I have url of the uploaded video and i want to create thumbnail of the video,which is already uploaded.so how i can convert will you help me out.

Unknown said...

hai,u have done a great job,actually i am soring video file in sqlserver2005 as binarydata instead of storing in appication folder,but u wrote embed data in codebehind with file name="Eati.swf", but i want this in html with value='<%# handler.ashx?id=' ,please find a solution to this,
please send code to my emailid,
my emailid is faiyazjpnce@gmail.com

thanks in advance

Unknown said...

Hi Ramky,

Great post...how could the same thing be accomplished if you were to store the video file in the database instead of on the server?

prasad said...

Hi ramky,

Thanks for the code.its really good but when i run plat.aspx iam getting error
Object reference not set to an instance of an object.

vid = Request.QueryString["vid"].ToString();

Anonymous said...

Hi ramki,im the beginner of .net. can u send ur project to my mail id with database backup.. because when implementing ur coding i faced some difficulties.so i didn't clear those things. pls send my Mail ID--suryaprasathg@gmail.com

Anonymous said...

Hi ramky,

Thanks for the code.its really good but when i run plat.aspx iam getting error
Object reference not set to an instance of an object.

vid = Request.QueryString["vid"].ToString();

umer said...

hi,
any one can send me the part-II "playing video" on my email please i need it.

thanks

umer said...

hi,
any one can send me the part-II "playing video" on my email

muhammad.umer12@hotmail.com

please i need it.

thanks

veerendra said...

HI i am gettting error 'Plh does not contain current context

Anonymous said...

Hi Ramky,

I need to redirect automatically to another page once the video ends. How can i do this in c#.Net web application.

Anonymous said...

Thumbnails are being created but theres no image as the actual video and image arent being saved but the file path is.

Any solutions please.

(im on my knees)

Ramky said...

Hey Guys,
Please find all links related to this post....
http://www.4shared.com/zip/f3DInGRc/VideoThumb.html
http://www.4shared.com/rar/XGfhLsKO/VideoThumb-II.html
http://www.4shared.com/rar/D6Wdo3ZY/ffmpeg.html

Please note that this was developed 5 years back. You may be found new version of ffmpeg so use this code for just reference purpose...

karthik said...

Hi Ramky,

I have to use ur code for playing videos, but play video in full screen option not working for me.. can u please give me some idea to solve my problem.

Unknown said...

I don't know if you still see this blog, but i just want to thank you a lot, i've tested the whole source code and it works very well.
This problem was annoying few months ago, and this post gave me the solution that i needed.
One more time thanks!