Thursday, 1 November 2012

Convert Video File to FLV using ASP.Net(C#)


Convert Video File to FLV using ASP.Net(C#)


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 wrtitren:-

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

2 comments:

  1. thanks for the usefull post.but i have an error.wheni select some video files with .mp4 extensions everything is good but some of them i receive the connection was reset page when i click on submit button for upload the video file.what is the reason?

    ReplyDelete
    Replies
    1. Hi mohammad,

      I think the issue is with your browser.. Are you using Mozilla firefox..

      If yes,Then refer these links:

      http://support.mozilla.org/en-US/questions/767877
      https://support.mozilla.org/en-US/questions/666486
      http://support.mozilla.org/en-US/questions/935320
      http://support.mozilla.org/en-US/questions/667431

      Delete

Thank You for Your Comments. We will get back to you soon.

back to top