开发者

C# Problem Reading Console Output to string

开发者 https://www.devze.com 2023-02-28 16:11 出处:网络
i want to launch ffmpeg from my app and retrive all console output that ffmpeg produces. Thing seems obvious, i followed many forum threads/articles like this one but i have problem, though i follow a

i want to launch ffmpeg from my app and retrive all console output that ffmpeg produces. Thing seems obvious, i followed many forum threads/articles like this one but i have problem, though i follow all information included there I seem to end up in dead end.

String that should contain output from ffmpeg is always empty. I've tried to see where is the problem so i made simple c# console application that only lists all execution parameters that are passed to 开发者_开发问答ffmpeg, just to check if problem is caused by ffmpeg itself. In that case everything work as expected.

I also did preview console window of my app. When i launch ffmpeg i see all the output in console but the function that should recieve that output for further processing reports that string was empty. When my param-listing app is launched the only thing I see is the expected report from function that gets output.

So my question is what to do to get ffmpeg output as i intended at first place.

Thanks in advance MTH


This is a long shot, but have you tried redirecting StandardError too?


Here is a part of my ffmpeg wrapper class, in particular showing how to collect the output and errors from ffmpeg.

I have put the Process in the GetVideoDuration() function just so you can see everything in the one place.

Setup:

My ffmpeg is on the desktop, ffPath is used to point to it.

namespace ChildTools.Tools
{
    public class FFMpegWrapper
    {
        //path to ffmpeg (I HATE!!! MS special folders)
        string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";
    //outputLines receives each line of output, only if they are not zero length
        List<string> outputLines = new List<string>();


    //In GetVideoDuration I only want the one line of output and in text form.
    //To get the whole output just remove the filter I use (my search for 'Duration') and either return the List<>
    //Or joint the strings from List<> (you could have used StringBuilder, but I find a List<> handier.

        public string GetVideoDuration(FileInfo fi)
        {
            outputLines.Clear();
    //I only use the information flag in this function
            string strCommand = string.Concat(" -i \"", fi.FullName, "\"");
    //Point ffPath to my ffmpeg
            string ffPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\ffmpeg.exe";

            Process processFfmpeg = new Process();

            processFfmpeg.StartInfo.Arguments = strCommand;
            processFfmpeg.StartInfo.FileName = ffPath;

    //I have to say that I struggled for a while with the order that I setup the process.
    //But this order below I know to work


            processFfmpeg.StartInfo.UseShellExecute = false;
            processFfmpeg.StartInfo.RedirectStandardOutput = true;
            processFfmpeg.StartInfo.RedirectStandardError = true;
            processFfmpeg.StartInfo.CreateNoWindow = true;
            processFfmpeg.ErrorDataReceived += processFfmpeg_OutData;
            processFfmpeg.OutputDataReceived += processFfmpeg_OutData;
            processFfmpeg.EnableRaisingEvents = true;
            processFfmpeg.Start();
            processFfmpeg.BeginOutputReadLine();
            processFfmpeg.BeginErrorReadLine();
            processFfmpeg.WaitForExit();

    //I filter the lines because I only want 'Duration' this time
            string oStr = "";
            foreach (string str in outputLines)
            {
                if (str.Contains("Duration"))
                {
                    oStr = str;
                }
            }
    //return a single string with the duration line

            return oStr;
        }

        private void processFfmpeg_OutData(object sender, DataReceivedEventArgs e)
        {
    //The data we want is in e.Data, you must be careful of null strings
            string strMessage = e.Data;
            if outputLines != null && strMessage != null && strMessage.Length > 0)
            {
                outputLines.Add(string.Concat( strMessage,"\n"));
        //Try a Console output here to see all of the output. Particularly
        //useful when you are examining the packets and working out timeframes
        //Console.WriteLine(strMessage);
            }
        } 
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消