完整版ffmpeg使用情况

 

 protected void Page_Load(object sender, EventArgs e)
        {
            string filePath = @"D:/Prjects/MT147/example/01/";
            //将是视频转为SWF格式
            //ffmpeg bin目录 @"D:/tools/ffmpeg/bin/";
            string ffmpegBinPath = "D:/tools/ffmpeg/bin/";
            //需要转化的视频目录
            //string convertDirPath = Server.MapPath("/resource/example");
           
            //开始转化
            AllVideoConvertToSWF(filePath, ffmpegBinPath);

        }


 #region 视频转swf

        /// <summary>
        /// 将视频转为SWF格式
        /// </summary>
        /// <param name="ffmpegBinPath">ffmpeg 工具bin 目录</param>
        /// <param name="videoSourceFileName">视频完全路径</param>
        /// <param name="targetSwfFileName">转为SWF文件完全路径</param>
        /// <returns></returns>
        private bool VideoConvertToSWF(string ffmpegBinPath, string videoSourceFileName, string targetSwfFileName)
        {

            try
            {
                Process process = new Process();
                process.StartInfo.FileName = "cmd.exe";//确定程序名 

                //"cd C:\Users\Administrator\"
                string startCommandContent = @"cd C:\Users\Administrator\";

                string sCommand = startCommandContent;

                process.StartInfo.Arguments = sCommand;//确定程式命令行 
                process.StartInfo.UseShellExecute = false;//Shell的使用 
                process.StartInfo.RedirectStandardInput = true;//重定向输入 
                process.StartInfo.RedirectStandardOutput = true;//重定向输出 
                process.StartInfo.RedirectStandardError = true;//重定向输出错误   
                process.StartInfo.CreateNoWindow = true;//设置置不显示示窗口 
                process.Start();//00  

                //跳到C盘 c:
                process.StandardInput.WriteLine("c:");

                // cd C:\Users\Administrator\
                process.StandardInput.WriteLine(sCommand);//也可以用这种方式输入入要行的命令 

                string cdName = ffmpegBinPath.Substring(0, 1);

                //跳到ffmpeg bin目录对应的磁盘中 如: d:
                process.StandardInput.WriteLine(cdName + ":");

                //"cd C:\Users\Administrator\Desktop\ffmpeg\bin\"
                string commandCDFfmpegBin = @"cd {0}";

                commandCDFfmpegBin = string.Format(commandCDFfmpegBin, @ffmpegBinPath);

                process.StandardInput.WriteLine(@commandCDFfmpegBin);

                //如果码特率太多转化出错       
                //@"ffmpeg -i C:\Users\Administrator\Desktop\ffmpeg\test.avi -to 00:30 c:\ttt.avi";

                //规定一定的码特率
                //ffmpeg -i C:\Users\Administrator\Desktop\ffmpeg\test.avi -to 00:30 -ar 22050 -y c:\ccc.avi

                string commandVideoContent = @"ffmpeg -i {0} -to 00:30 -ar 22050 -y {1}";

                commandVideoContent = string.Format(commandVideoContent, @videoSourceFileName, @targetSwfFileName);

                process.StandardInput.WriteLine(@commandVideoContent);

                process.StandardInput.WriteLine("exit");//要得加上Exit要不然下一行程式  

                //string result = process.StandardOutput.ReadToEnd(); //输出出流取得命令行结果

                //Response.Write(result);

                return true;

            }
            catch (Exception ex)
            {
                Response.Write(@videoSourceFileName + "转为WSF文件失败<br />");
                Response.Write(ex.Message + "<br />");

                return false;

            }

        }

        #endregion

        #region 批量转换函数

        /// <summary>
        /// 列出指定目录下及所其有子目录及子目录里更深层目录里的文件中的视频文件进行转化  
        /// </summary>
        /// <param name="convertDirPath">需要转化的视频目录路径</param>
        /// <param name="ffmpegBinPath">转化工具ffmpeg bin目录路径</param>
        public void AllVideoConvertToSWF(string convertDirPath, string ffmpegBinPath)
        {

            DirectoryInfo dir = new DirectoryInfo(convertDirPath);

            //.flv,.wav,.swf,.avi,.mp3,.mp4,.rm,.rmvb,.mpg,.wmv,.3gp,.amv,.mov
            string exts = ".flv,.wav,.swf,.avi,.mp3,.mp4,.rm,.rmvb,.mpg,.wmv,.3gp,.amv,.mov";



            //找到该目录下的文件  
            FileInfo[] fi = dir.GetFiles();

            foreach (FileInfo f in fi)
            {

                string currentExtensionName = Path.GetExtension(f.FullName).ToLower();

                //处理原格式为swf文件和生成的swf文件
                if (currentExtensionName == ".swf")
                {
                    int exIndex = f.FullName.LastIndexOf(currentExtensionName);

                    string beforeFileName = f.FullName.Remove(exIndex);

                    string beforeExtensionName = Path.GetExtension(beforeFileName);

                    //当前文件已经生成swf文件,不给继续生成swf文件
                    if (!string.IsNullOrEmpty(beforeExtensionName) && exts.Contains(beforeExtensionName))
                    {
                        continue;
                    }
                }


                if (System.IO.File.Exists(f.FullName + ".swf"))
                {
                    continue;
                }

                string ext = Path.GetExtension(f.FullName).ToLower();

                if (exts.Contains(ext))
                {
                    try
                    {
                        bool isSuccess = VideoConvertToSWF(ffmpegBinPath, f.FullName, f.FullName + ".swf");
                        Response.Write("完整路径:" + f.FullName.ToString() + " 生成SWF文件路径:" + f.FullName + ".swf  生成状态:" + isSuccess.ToString() + "<br>");
                    }
                    catch (Exception ex)
                    {
                        Response.Write("完整路径:" + f.FullName.ToString() + " 生成错误:" + ex.Message + "<br>");
                    }

                }
            }


            //获取子目录
            DirectoryInfo[] subDir = dir.GetDirectories();

            foreach (var d in subDir)
            {
                AllVideoConvertToSWF(d.FullName, ffmpegBinPath);
            }
        }

        #endregion

 

posted @ 2014-10-21 14:39  大空白纸  阅读(518)  评论(0编辑  收藏  举报