日期:2014-05-17  浏览次数:20826 次

C#调用FFMPEG时路径问题
C#调用FFMPEG转换视频问题

代码如下:
C# code

public string gvCommand = "-y -i " + @"E:\Basic\test.avi" + " -vcodec h263 -b 30000 -r 6 -s qcif -acodec libopencore_amrnb -ac 1 -ar 8000 -ab 10.2k -f 3gp " + @"D:\test.3gp";
public string gvFilepath = @"E:\Basic\test.avi";

        public void ConvertVideo()
        {
            Process p = new Process();//建立外部调用线程
            p.StartInfo.FileName = @"..\..\Basic\ffmpeg.exe";//要调用外部程序的绝对路径
        (成功)p.StartInfo.Arguments = gvCommand ;
        (成功)p.StartInfo.Arguments = "-y -i " + gvFilepath + " -vcodec h263 -b 30000 -r 6 -s qcif -acodec libopencore_amrnb -ac 1 -ar 8000 -ab 10.2k -f 3gp " + @"D:\test.3gp"; ;//参数(这里就是FFMPEG的参数了)
            p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
            p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
            p.StartInfo.CreateNoWindow = false;//不创建进程窗口
            p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
            p.Start();//启动线程
            p.BeginErrorReadLine();//开始异步读取
            p.WaitForExit();//阻塞等待进程结束
            p.Close();//关闭进程
            p.Dispose();//释放资源
        }


  当我直接把命令全部赋值给Arguments时,转换是成功的;
  当我使用gvFilepath再赋值给Arguments时,转换也是成功的;
  可是当我把文件路径写到DataGridView中,再通过foreach读取出来使用时就不行了
C# code

        public void mGetDataSetFilepath()
        {
            foreach (DataRow i in gvVideolist.Tables["VideoList"].Rows)
            {
                gvFilepath = i["文件路径"].ToString();
                gvCommand = "-y -i " + gvFilepath + " -vcodec h263 -b 30000 -r 6 -s qcif -acodec libopencore_amrnb -ac 1 -ar 8000 -ab 10.2k -f 3gp " + @"D:\test.3gp";
                this.ConvertVideo();
            }
        }


  这样会转换失败,提示"系统找不到指定的文件。"
  我单步调试对比过成功和失败时gvCommand和gvFilepath的字符串内容,都完全一样,为什么会这样呢?
  请指教,谢谢^_^

------解决方案--------------------
嗯,这个错误就是这样的,你的路径不对的,Application.StartupPath根文件是:当前程序目录\bin\Debug