Wednesday, December 3, 2008

Convert Video to .Flv Using c#.net on Web.

19 comments
Having trouble playing .flv in IIS click here for solution

Having trouble Setting up IIS click here for solution

The method to convert video to .flv is too easy. You can download following files from .net
1)ffmpeg.exe
2) ffplay.exe
3) pthreadGC2.dll

After downloading all the files

Follow the steps written:-

1)Make a new .net web site or windows application.
2)Copy and paste all the 3 above written files to root location
3)Copy and Paste code written below
4)Put an upload to page and rename to “ fileuploadImageVideo”
5)put and button and rename to btn_Submit
6)Make 3 folders OriginalVideo, ConvertVideo, Thumbs
7)Import Class “using System.IO;”



private bool ReturnVideo(string fileName)
{
string html = string.Empty;
//rename if file already exists

int j = 0;
string AppPath;
string inputPath;
string outputPath;
string imgpath;
AppPath = Request.PhysicalApplicationPath;
//Get the application path
inputPath = AppPath + "OriginalVideo";
//Path of the original file
outputPath = AppPath + "ConvertVideo";
//Path of the converted file
imgpath = AppPath + "Thumbs";
//Path of the preview file
string filepath = Server.MapPath("~/OriginalVideo/" + fileName);
while (File.Exists(filepath))
{
j = j + 1;
int dotPos = fileName.LastIndexOf(".");
string namewithoutext = fileName.Substring(0, dotPos);
string ext = fileName.Substring(dotPos + 1);
fileName = namewithoutext + j + "." + ext;
filepath = Server.MapPath("~/OriginalVideo/" + fileName);
}
try
{
this.fileuploadImageVideo.SaveAs(filepath);
}
catch
{
return false;
}
string outPutFile;
outPutFile = "~/OriginalVideo/" + fileName;
int i = this.fileuploadImageVideo.PostedFile.ContentLength;
System.IO.FileInfo a = new System.IO.FileInfo(Server.MapPath(outPutFile));
while (a.Exists == false)
{

}
long b = a.Length;
while (i != b)
{

}


string cmd = " -i \"" + inputPath + "\\" + fileName + "\" \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";
ConvertNow(cmd);
string imgargs = " -i \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\" -f image2 -ss 1 -vframes 1 -s 280x200 -an \"" + imgpath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".jpg" + "\"";
ConvertNow(imgargs);


return true;
}
private void ConvertNow(string cmd)
{
string exepath;
string AppPath = Request.PhysicalApplicationPath;
//Get the application path
exepath = AppPath + "ffmpeg.exe";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = exepath;
//Path of exe that will be executed, only for "filebuffer" it will be "flvtool2.exe"
proc.StartInfo.Arguments = cmd;
//The command which will be executed
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();

while (proc.HasExited == false)
{

}
}
protected void btn_Submit_Click(object sender, EventArgs e)
{
ReturnVideo(this.fileuploadImageVideo.FileName.ToString());
}


Now run the application select a video file, that will get converted and come to ConvertVideo Folder

You can also download this ex. with Code from

Click Here to download

19 comments:

Shakti Singh Dulawat said...

nice code by you
keep sharing knowledge

صالح said...
This comment has been removed by the author.
صالح said...

It is great article. However, I downloaded the source code, but it does not work. I mean I tried to convert the video available in the original video folder, but it creates flv file with zero size and it does not create a thumb picture. I wonder what is wrong?

Oki.Nandoko.Rakhim said...

to get it worked try to change this code
:
string cmd = " -i \"" + inputPath + "\\" + fileName + "\" -ar 22050 \"" + outputPath + "\\" + fileName.Remove(fileName.IndexOf(".")) + ".flv" + "\"";

see the bold one.

Anonymous said...

I'm tried with .mpg video file, it's working. But .avi file type converted to .flv file with 0kb. Which type of video file support by this?

Please help me.

Thanks,
Santanu

Anonymous said...

Hi

I tried it locally and everything works properly. But when I upload it to remote windows server it's didn't work. I didn't get any error. Please help me what is wrong?

Thanks,
Santanu

Anonymous said...

i have tried this code but button click event is not called. It doesnt work properly

Sohil said...

nice code.
This code is working when I try to convert files upto 100kb or 200 kb. but it do not convert the file when the size of file if more than 1 MB. It shows the error "Connection Interrupted". can you trace the problem and show the possible solution???

please reply me on vohrasohilhis@gmail.com

Jasper said...

Use Media Manager Pro at http://www.all4dotnet.com/

Heo_moi said...

How many format can it convert ?

Narender said...

Hi Heo,

I have not tried all the formats but, still I am sure I is enable to convert from any format to flv.

Thanks,
Narender Singh

Kevin said...

I followed the instructions and the code ran perfect the first time. Thank you :)

Anonymous said...

Since this sample uses the "System.Diagnostics.Process" class, this will not work on a web application hosted in a shared hosting environment (or at least those that I know about), because this requires a "full-trust" level on the server, which most shared hosting providers won't let you have, because it is a big security risk - so does any one know of a way to convert video to flv in a "medium-trust" environment, or is that simply just impossible?

Anonymous said...

please show me the way to add watermark to video (.flv) using ffmpeg on windows.

thanks a lot

Dinesh said...

when i click on the button at runtime the textbox clears,but no video is uploaded plz give way out sir,plz i need it urgently...

Narender said...

Hi,

Please check then code of process. Run that command in command prompt may your command of FFMPEG.exe is having some error. if it running fine then.... please send me your code so,I can check and revert back to you.

Thanks,
Narender Singh

Anonymous said...

Thx. It works. But I wanted to have a progress bar on the page, please help....

Regards,
Alan

sd said...

1.Some time Conert video file size is 0KB.

2. It did not convert .swf file

Please give me a good solution for above problem

SMTH said...

Hello
i have try the code it is work fine and nice work but the problem is the size for the video must be less than 1 MB, how can i increasing the video size ???
please answer me on suhib_hammoudah@hotmail.com