Esempio n. 1
0
        public OutputPackage ConvertToFLV(string inputPath)
        {
            VideoFile vf = null;

            //try
            //{
            vf = new VideoFile(inputPath);
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}

            OutputPackage oo = ConvertToFLV(vf);

            return(oo);
        }
Esempio n. 2
0
        public OutputPackage ConvertToFLV(VideoFile input)
        {
            if (!input.infoGathered)
            {
                GetVideoInfo(input);
            }
            OutputPackage ou = new OutputPackage();

            string sourceExtension        = GetFileExension(input.Path);
            string sourceWithoutExtension = input.Path.Replace("." + sourceExtension, "");

            //GET PREVIEW IMAGE ---------------------------------------
            string previewPath   = sourceWithoutExtension + ".jpg";
            string processParams = string.Format(" -i \"{0}\" -f mjpeg -t 0.001 -ss 5 -y \"{1}\" ", input.Path, previewPath);
            string output        = RunProcess(processParams);

            ou.RawOutput = output;

            if (File.Exists(previewPath))
            {
                ou.PreviewImage     = LoadImageFromFile(previewPath);
                ou.PreviewImagePath = previewPath;
            }
            else
            { //try running again at frame 1 to get something
                processParams = string.Format("-i \"{0}\" \"{1}\" -vcodec mjpeg -ss {2} -vframes 1 -an -f rawvideo", input.Path, previewPath, 1);
                output        = RunProcess(processParams);

                ou.RawOutput = output;

                if (File.Exists(previewPath))
                {
                    ou.PreviewImage     = LoadImageFromFile(previewPath);
                    ou.PreviewImagePath = previewPath;
                }
            }

            //CONVERT THE FILE --------------------------------------------------------------------------------
            if (sourceExtension == "flv")
            {
                ou.Path   = input.Path;
                ou.Length = 0;

                byte[]       fileBytes = File.ReadAllBytes(input.Path);
                MemoryStream mems      = new MemoryStream(fileBytes);
                ou.VideoStream = mems;
                ou.Length      = (int)ou.VideoStream.Length;
            }
            else
            {
                string flvPath = sourceWithoutExtension + ".flv";
                //processParams = string.Format("-i {0} -s 480x360 -y -ar 44100 -ab 64 -f flv {1}", input.Path, flvPath);
                //string videoParam = "-b 512k -f flv -s 480x270 -vcodec libx264 -vpre hq";
                //string autidoParam = "-ar 44100 -ac 2 -ab 93k -acodec aac";
                // Params = String.Format("-i \"{0}\" {1} {2} -y \"{3}\"",
                // input.Path, videoParam, autidoParam, flvPath);

                //medium quality
                processParams = string.Format("-i \"{0}\" -acodec mp3 -ab 48k -ac 1 -ar 44100 -f flv -deinterlace -nr 500 -r 25 -b 270k -me_range 25 -i_qfactor 0.71 -g 500 \"{1}\" ",
                                              input.Path, flvPath);

                ////high quality
                //processParams = string.Format("-i \"{0}\" -acodec mp3 -ab 64k -ac 2 -ar 44100 -f flv -deinterlace -nr 500 -r 25 -b 650k -me_range 25 -i_qfactor 0.71 -g 500 \"{1}\"",
                //            input.Path, flvPath);


                Process proc = new Process();
                proc.StartInfo.Arguments              = processParams;
                proc.StartInfo.FileName               = this._ffExe;
                proc.StartInfo.UseShellExecute        = false;
                proc.StartInfo.CreateNoWindow         = true;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError  = true;

                proc.Start(); // start !
                proc.WaitForExit();

                //output = RunProcess(processParams);

                if (File.Exists(flvPath))
                {
                    ou.Path        = flvPath;
                    ou.VideoStream = LoadMemoryStreamFromFile(flvPath);
                    ou.Length      = (int)ou.VideoStream.Length;

                    try
                    {
                        File.Delete(input.Path);
                    }
                    catch (Exception) { }
                }
            }
            ou.Success = true;

            return(ou);
        }
Esempio n. 3
0
        public void GetVideoInfo(VideoFile input)
        {
            //set up the parameters for video info
            string Params = string.Format("-i {0}", input.Path);
            string output = RunProcess(Params);

            input.RawInfo = output;

            //get duration
            Regex re = new Regex("[D|d]uration:.((\\d|:|\\.)*)");
            Match m  = re.Match(input.RawInfo);

            if (m.Success)
            {
                string   duration   = m.Groups[1].Value;
                string[] timepieces = duration.Split(new char[] { ':', '.' });
                if (timepieces.Length == 4)
                {
                    input.Duration = new TimeSpan(0, Convert.ToInt16(timepieces[0]), Convert.ToInt16(timepieces[1]), Convert.ToInt16(timepieces[2]), Convert.ToInt16(timepieces[3]));
                }
            }

            //get audio bit rate
            re = new Regex("[B|b]itrate:.((\\d|:)*)");
            m  = re.Match(input.RawInfo);
            double kb = 0.0;

            if (m.Success)
            {
                Double.TryParse(m.Groups[1].Value, out kb);
            }
            input.BitRate = kb;

            //get the audio format
            re = new Regex("[A|a]udio:.*");
            m  = re.Match(input.RawInfo);
            if (m.Success)
            {
                input.AudioFormat = m.Value;
            }

            //get the video format
            re = new Regex("[V|v]ideo:.*");
            m  = re.Match(input.RawInfo);
            if (m.Success)
            {
                input.VideoFormat = m.Value;
            }

            //get the video format
            re = new Regex("(\\d{2,3})x(\\d{2,3})");
            m  = re.Match(input.RawInfo);
            if (m.Success)
            {
                int width = 0; int height = 0;
                int.TryParse(m.Groups[1].Value, out width);
                int.TryParse(m.Groups[2].Value, out height);
                input.Width  = width;
                input.Height = height;
            }
            input.infoGathered = true;
        }