天天看点

利用ffmpeg把视频文件转换为flv文件

private static void ConvertAVItoFLV(string pathname)

        {

            //文件名处理

            string tmpFile = System.Environment.CurrentDirectory + "http://www.cnblogs.com/happyday56/admin/file://flv//";

            string filename = Path.GetFileName(pathname);

            tmpFile += filename;

            string outFile = Path.ChangeExtension(tmpFile, ".flv");

            //转化处理,采用异步调用

            //Control.CheckForIllegalCrossThreadCalls = false; //取消线程安全保护模式!注意的是这里不设置为false的话会(流输出事件的处理)产生异常.

            string FFmpegArguments = @" -i " + pathname + " -ab 56 -ar 22050 -b 500 -r 15 -s 640x480 " + outFile;

            Process p = new Process();//建立外部调用线程

            p.StartInfo.FileName = @"F:\ffmpeg\ffmpeg_full_SDK_V3.0\Libs\ffmpeg.exe";//要调用外部程序的相对路径

            p.StartInfo.Arguments = FFmpegArguments;//参数(这里就是MENCODER的参数了)

            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)

            p.StartInfo.RedirectStandardOutput = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)我这里用的是mencoder

            p.StartInfo.CreateNoWindow = true;//不创建进程窗口

            p.OutputDataReceived += new DataReceivedEventHandler(putInfo);//外部程序(这里是mencoder)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN

            p.Start();//启动线程

            p.BeginOutputReadLine();//开始异步读取

            Console.WriteLine("阻塞等待进程ffmpeg结束.");

            p.WaitForExit();//阻塞等待进程结束

            p.Close();//关闭进程

            p.Dispose();//释放资源

           Console.WriteLine("avi转化flv成功"+pathname);

        } 

继续阅读