-1

This question already has an answer here:

I am coding a video converter using NRECO and ffmpeg everything works but when it comes at to get the Progress time i've tried

pgbConversion.Value = FFMpegConverter.ConvertProgress();

but it is impossible so i got ! ** **ConvertProgress is an even; please check C# documentation about how to add an even handler.****

from NReco please how can i get the video progress knowing that ConvertProgrss is an event


  • Are you sure it doesn't mean "event" and not even? In which case I would see what property I need to assign an expression to, probably using a lambda. I'm unfamiliar with this framework, but if you wanted to add a function to a button click event is would be something like button.onClick += (s,e) => DoSomething(); - Kirkland

2 답변


2

You write this:

FFMpegConverter.ConvertProgress +=

Your Visual Studio starts helping you out by offering to attach event handler code to the ConvertProgress event for you. It will show you a pop up, like MY visual studio is doing here, in this screenshot of me attaching an event handler to a Timer.Elapsed event, which is a totally different event, on a different object, that I'm using as an example to demonstrate how visual studio helps you attach events to things:

enter image description here

Then you press TAB in YOUR visual studio to accept the suggestion in the pop up

Visual Studio will scaffold a basic event handler for you, and you can add your code into it that you want to be run every time the event is raised. Here is what my visual studio did when I pressed tab during adding an event handler to my example using Timer:

enter image description here

(Naturally my pics show VS making a handler for Timer.Elapsed, not FFMpegConverter.ConvertProgress, because I have Timer, but I don't have that FFMpegConverter class.. "Pics are for illustration purposes only - you are NOT supposed to type the code you see in my pictures")


  • hello thanks for the answer, but i get an error on this line - DevFranzBeta
  • FFMpegConverter.ConvertProgress += new System.Timers.Timer().Elapsed += Program_Elapsed; - DevFranzBeta
  • Did you read the last line of my post? - Caius Jard
  • An object reference is required for the non-static field, method, or property 'FFMpegConverter.ConvertProgress' Cannot implicitly convert type 'void' to .... - DevFranzBeta
  • Sorry i haven't read it i would try it thx - DevFranzBeta

1

Are you sure it's not a typo and they mean "Event"? In which case I would assign lambda expression to the event, or you could just reference another method as seen below, so the documentation would be here: Lambda Expressions, Events.

Example w/o Lambda:

static void Main(string[] args)
{
    var ffMpeg = new FFMpegConverter();
    ffMpeg.ConvertProgress += FfMpeg_ConvertProgress;
    ffMpeg.ConvertMedia("input.mov", "output.mp4", Format.mp4);
}

private static void FfMpeg_ConvertProgress(object sender, ConvertProgressEventArgs e)
{
    // Percent complete as a double
    pgbConversion.Value = e.Processed.TotalSeconds / e.TotalDuration.TotalSeconds;
}

Example w/ Lambda expression:

static void Main(string[] args)
{
    var ffMpeg = new FFMpegConverter();
    ffMpeg.ConvertProgress += (s, e) => {
        pgbConversion.Value = e.Processed.TotalSeconds / e.TotalDuration.TotalSeconds;
    };
    ffMpeg.ConvertMedia("input.mov", "output.mp4", Format.mp4);
}

The event only has the total number of seconds in the video and the number of seconds that's been processed. They are also represented as TimeSpan objects. I'd advise getting the total number of seconds of each of these (returning a double), and then dividing to get a percentage complete. Of course you could use either of them as TimeSpan instances individually.


  • thanks, please i have an eror here : pgbConversion.Value = it shows that it is non-static - DevFranzBeta
  • Could you add the code? Is the class that pgbConversion instantiates a standard part of the .NET library? - Kirkland
  • How come you switched away from using the lambda version? I thought that was quite neat.. - Caius Jard
  • Wasn't directly relevant to the question. I wanted to prioritize what was specifically being asked over the technical level of the answer. Since you've asked, however, I can add it :) - Kirkland

Linked


Related

Latest