1

So I'm using the code below to convert a specified file, by running ffmpeg, and I need the progress to be visible in a progressbar, so I#d need to get it to display the cmd output to my textbox in realtime and from there get it to being displayed by the progressbar, any help?

 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 =  @"-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();

        Directory.Delete(resdir, true);
    }

FFMPEG Output regulary looks like this:

size=    4824kB time=00:08:13.63 bitrate=  80.1kbits/s speed=  33x
adrifcastr
  • 31
  • 1
  • 4
  • @Borian could be, but I thought this might be about how to redirect the processes output correctly and get the actual data. – Thomas Flinkow Mar 19 '18 at 12:46
  • 1
    Execute it asyncronous and show the progress bar when launched. After the asyn has ended hide the progress bar. – Cleptus Mar 19 '18 at 12:50
  • 1
    https://stackoverflow.com/questions/11441517/ffmpeg-progress-bar-encoding-percentage-in-php has info about parsing progress information from ffmpeg – Borian Mar 19 '18 at 12:52

1 Answers1

0

You don't show the code for the progress viewer, but this is how you get the output of the ffmpeg.exe and (only the relevant parts):

Process ffm = new Process()
{
    RedirectStandardError = true,
    RedirectStandardOutput = true
};

ffm.OutputDataReceived += this.HandleOutputData;
ffm.ErrorDataReceived += this.HandleErrorData;

and in these methods you can handle the writing to the TextBox and the viewer:

private void HandleOutputData(object sender, DataReceivedEventArgs e)
{
    // The new data is contained in e.Data
    MyProgressViewer.Update(e.Data);
    this.myTextBox.Text += e.Data;
}

private void HandleErrorData(object sender, DataReceivedEventArgs e)
{
    // The new data is contained in e.Data
    MyProgressViewer.Update(e.Data);
    this.myTextBox.Text += e.Data;

    // Additional error handling here.
}

As for the parsing: apparently,

ffmpeg now has a progress option, which gives output more easily parsed.

See this answer for more details.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • since ffmpeg just outputs in stderr the stdout parts are useless I guess, but I still don't get how this will convert the conversion progress into the progressbar value, since ffmpeg has a certain output format while converting – adrifcastr Mar 19 '18 at 12:49
  • @adrifcastr You're right about that, if it only utilized the `stderr`. About the conversion: that's up to you ;-) you don't show us the format, e.g. does it print "42%" or "42" etc. You would need to parse the value to an `int` and set it to the progress bar then. – Thomas Flinkow Mar 19 '18 at 12:51
  • wait a sec, I'll add it to the OP – adrifcastr Mar 19 '18 at 12:54
  • `HandleErrorData()` will never be called, because you forgot to add `ffm.BeginErrorReadLine()`. – VladStepu2001 Aug 08 '22 at 20:53