Tuesday, January 11, 2011

Live conversion of video files to .flv

Lately I have been interested into how exactly http://youtube.com does all the conversion of different kinds of videos into flv.
Without much of a searching I have found a way to convert it and it's quite easy.

You need 3 items to begin with:

these are a complete solution to record, covert and stream audio and video.

Now is time for the webpage itself.
1. Go into Internet Information Service Manager and create a new .net web application.
2. Copy the 3 files above into the created web application directory.
3. In the application directory create 2 folders: "OriginalVideo" and "ConvertVideo"
4. Give those folders security permissions so that we can upload to them
5. Create a new page. Name it however you desire it doesn't matter.
6. Put on 2 controls on it. 1 Button with ID="btnSubmit" OnClick="btnSubmit_Click"
and a FileUpload Control with ID="fileuploadVideo"
7. Copy the code below into the code behind

The code that I am posting is in C#

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.fileuploadVideo.SaveAs(filepath);

}

catch

{

return false;

}

string outPutFile;

outPutFile = "~/OriginalVideo/" + fileName;

int i = this.fileuploadVideo.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 btnSubmit_Click(object sender, EventArgs e)
{
ReturnVideo(this.fileuploadVideo.FileName.ToString());
}

And there you go! A simple conversion.

Big thanks to Bahadur Pur Rajoa for the help in this solution.