Convert Any video to flv in asp.net c#

Here this code is used to convert any format video to .flv format.
Here call ReturnVideo method and pass the parameters.
here u want to add some files. those are
1)ffmpeg.exe
2)ffplay.exe
3)pthreadGC2.dll
these files are root folder files.

 private bool ReturnVideo(string fileName,string TargetName)
        {
            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 + ConfigurationManager.AppSettings["VideoGallery"];// "Admin\\VideoGallery\\";
            //Path of the original file
            outputPath = AppPath + ConfigurationManager.AppSettings["VideoGallery"];
            //Path of the converted file
            imgpath = AppPath + ConfigurationManager.AppSettings["VideoGallery"];
            //Path of the preview file     D:\ScoringStar\ClientSite\Content\ClientSite\
            string filepath = Server.MapPath(ConfigurationManager.AppSettings["VideoGallery"]+"\\" + 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(ConfigurationManager.AppSettings["VideoGallery"]+"\\" + fileName);
            }
            try
            {
                this.fuldVideo.SaveAs(filepath);
            }
            catch
            {
                return false;
            }
            string outPutFile;
            outPutFile = ConfigurationManager.AppSettings["VideoGallery"] +"\\"+ fileName;
            int i = this.fuldVideo.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" + "\"";
            string path=   fileName.Remove(fileName.IndexOf(".")) + ".flv" ;
            if(fileName.Contains(".flv")!=true)
            {
            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);

            string imagepath =  fileName.Remove(fileName.IndexOf(".")) + ".jpg";
           
            DirectoryInfo d = new DirectoryInfo(outputPath);
            FileInfo[] infos = d.GetFiles();
            foreach (FileInfo f in infos)
            {
                string compare = f.FullName.ToString().Trim();
                if (compare.Contains(path.ToString().Trim()))
                {
                    File.Move(f.FullName, f.FullName.ToString().Replace(fileName.Remove(fileName.IndexOf(".")) + ".flv", TargetName + ".flv"));
                }
                else if (compare.Contains( imagepath.ToString().Trim()))
                {
                    File.Move(f.FullName, f.FullName.ToString().Replace(fileName.Remove(fileName.IndexOf(".")) + ".jpg", TargetName + ".jpg"));
                }
            }
            if (File.Exists(filepath))
            {
                File.Delete(filepath);
            }
            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)
            {

            }
        }

Comments

Popular posts from this blog

WCF interview questions

The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program

what is Event Cache table in sharepoint