adrifcastr
adrifcastr

Reputation: 31

C# redirect ffmpeg output to textbox in realtime

So I've been reading a lot through stackoverflow, and I found a lot of questions trying to deal with a similar problem as mine, but none of the solutions worked for me. So here is what I have:

I have a WPF GUI application which launches ffmpeg by the following code:

        private void convertbutton_Click(object sender, RoutedEventArgs e)
    {
        string resdir = AppDomain.CurrentDomain.BaseDirectory + "\\res";
        Extract("ADC", AppDomain.CurrentDomain.BaseDirectory + "\\res", "res", "ffmpeg.exe");

        string ffdir = AppDomain.CurrentDomain.BaseDirectory + "\\res\\ffmpeg.exe";
        string arg = @"-progress progresslog.txt -y -activation_bytes ";
        string arg1 = @" -i ";
        string arg2 = @" -ab 80k -vn ";
        string abytes = bytebox.Text;
        string arguments = arg + abytes + arg1 + openFileDialog1.FileName + arg2 + saveFileDialog1.FileName;

        Process ffm = new Process();
        ffm.StartInfo.FileName = ffdir;
        ffm.StartInfo.Arguments = arguments;
        ffm.StartInfo.CreateNoWindow = true;
        ffm.StartInfo.RedirectStandardOutput = true;
        ffm.StartInfo.RedirectStandardError = true;
        ffm.StartInfo.UseShellExecute = false;
        ffm.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();
        ffm.Start();
        ffm.WaitForExit();

        ffm.Close();

        MessageBox.Show("Conversion Complete!");

        File.Delete("progresslog.txt");
        Directory.Delete(resdir, true);
    }

So on this application I do also have other processes, which display their output to the textbox after finishing what they are doing, so but since ffmpeg outputs it's conversion progress in the cmd output, I need to get exactly that being displayed in the textbox in realtime.

And I am pretty sure that ffmpeg outputs into stderr same w/ ffprobe.

I'd appreciate any help, since days of googleing didn't help me with anything so far. Thanks in advance.

Upvotes: 2

Views: 855

Answers (1)

Marius
Marius

Reputation: 1679

Try to add these lines around ffm.Start(). This should write the ffmpeg output to the Debug console. If that works you can adjust the code to write the output to your WPF GUI.

ffm.EnableRaisingEvents = true;
ffm.OutputDataReceived += (s, ea) => { Debug.WriteLine($"STD: {ea.Data}"); };
ffm.ErrorDataReceived += (s, ea) => { Debug.WriteLine($"ERR: {ea.Data}"); };
ffm.Start();
ffm.BeginOutputReadLine();
ffm.BeginErrorReadLine();

See also this SO post

Upvotes: 2

Related Questions