System.Diagnostics.Process程序继续运行

时间:2015-11-17 15:53:48

标签: c# ffmpeg process.start

我正在尝试使用命令行应用程序中的ffmpeg库将一些avi文件转换为mkv。我做了一个允许我这样做的方法:

public bool convertAvitoMkv(string path, string sourceName, string destName)
    {

        bool returncode = false;
        try
        {
            string comando = string.Format("-i {0} -c:v libx264 -crf 19 -preset slow -c:a libfaac -b:a 192k -ac 2 {1}", string.Format("{0}\\{1}",path,sourceName), string.Format("{0}\\{1}",path,destName) + ".mkv");
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "ffmpeg.exe";
            startInfo.WorkingDirectory = programPath;
            startInfo.CreateNoWindow = true;
            startInfo.Arguments = comando;
            process.StartInfo = startInfo;
            process.Start();
            while (!process.HasExited)
            {
                process.Kill(); 
                //continue hasta que termine la ejecucion
            }

            returncode = process.HasExited;
        }
        catch (Exception ex)
        {

        }
        return returncode;
    }

以前的版本没有while(!process.HasExited),但我添加了它,因为我注意到有很多ffmpeg.exe运行的实例,我必须手动关闭。我怎样才能保证每个转换都已完成并且每个ffmpeg实例都已关闭? 问候

2 个答案:

答案 0 :(得分:0)

public bool concatenarArchivos(string archivos, string path, string destName)
    {
        bool returncode = false;
        try
        {
            string[] extensions = {".mp4"};
            string[] dirContents = System.IO.Directory.GetFiles(path, "*.*").Where(f => extensions.Contains(new FileInfo(f).Extension.ToLower())).ToArray();
            if (dirContents.Length > 0)
            {
                string comando = string.Format("-f concat -i {0} -c copy {1}", archivos, string.Format("{0}\\{1}", path, destName));
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = "ffmpeg.exe";
                startInfo.WorkingDirectory = programPath;
                startInfo.CreateNoWindow = true;
                startInfo.Arguments = comando;
                process.StartInfo = startInfo;
                process.Start();
                process.WaitForExit();
                //process.Close();
                process.Dispose();
                //returncode = process.HasExited;
            }
        }
        catch (Exception ex)
        {

        }
        return returncode;
    }

答案 1 :(得分:0)

我有同样的问题。调试时我发现当我使用WaitForExit时,它会无限期地等待。当我使用!process.HasExited的while循环时,它也没有通过while循环。

一旦我为标准输出和错误添加了重定向,并且读取了它开始按预期工作的流。

似乎当存在某种输出(常规和/或错误)时,该过程会一直等到输出处理完毕。在它不会退出之前。

如下更改代码很可能会解决您的问题。

public bool convertAvitoMkv(string path, string sourceName, string destName)
    {

        bool returncode = false;
        try
        {
            string comando = string.Format("-i {0} -c:v libx264 -crf 19 -preset slow -c:a libfaac -b:a 192k -ac 2 {1}", string.Format("{0}\\{1}",path,sourceName), string.Format("{0}\\{1}",path,destName) + ".mkv");
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "ffmpeg.exe";
            startInfo.WorkingDirectory = programPath;
            startInfo.CreateNoWindow = true;
            startInfo.Arguments = comando;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            process.StartInfo = startInfo;
            process.Start();

            StreamReader readerOut = process.StandardOutput;
            StreamReader readerErr = process.StandardError;
            // Process the readers e.g. like follows
            string errors = readerErr.ReadToEnd();
            string output = readerOut.ReadToEnd();

            while (!process.HasExited)
            {
                continue; 
            }

            returncode = process.HasExited;
        }
        catch (Exception ex)
        {

        }
        return returncode;
    }