'How to get frame progress in c# from FFMPEG

I am first time using Process namespace in c# I am using FFMPEG to add a watermark in the video. I am successfully adding the watermark but I also want to show the progress in my program console. How can I get a frame no that on currently FFmpeg on.

To achieve this I am also using ffprobe to get the total number of frames and after that, I will divide it with current frame no so that I will get the progress. The problem is I don't know how to get the frame no while ffmpeg is doing processing on video.



Solution 1:[1]

I wrote some helper class for things like this a good number of years ago. The interesting parts were:


        private Process _ffmpeg = null;

        public override void StartRecording()
        {
            ProcessStartInfo psi = new ProcessStartInfo
            {
                FileName = PATH_TO_FF_HERE,
                Arguments = ARGS_FOR_FF_HERE,
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true
            };

            _ffmpeg = System.Diagnostics.Process.Start(psi);
            _ffmpeg.OutputDataReceived += Ffmpeg_OutputDataReceived;
            _ffmpeg.ErrorDataReceived += Ffmpeg_ErrorDataReceived;
            _ffmpeg.BeginOutputReadLine();
            _ffmpeg.BeginErrorReadLine();

            _ffmpeg.PriorityClass = ProcessPriorityClass.High;
        }

        void Ffmpeg_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null && e.Data.StartsWith("frame="))
            {
                //for example, e.Data might look like:
                //frame=113987 fps= 10 q=-1.0 Lsize=  204000kB time=03:09:58.50 bitrate= 146.6kbits/s

                //do something here
            }
        }
        void Ffmpeg_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null && e.Data.StartsWith("frame="))
            {
                //do something here
            }
        }

For my purposes I wasn't interested in the frame data, so I ignored any strings that started with "frame=", but it sounds like you want to act if you see a data that doees, maybe activate an event if some framecounter is 100 higher than it was before (I don't think you need to raise an event on every frame processed.. but you're free to do what you want!)

Solution 2:[2]

Try FFmpegArgs

string args = ....
var render = FFmpegRender
  .FromArguments(args,c => c.WithFFmpegBinaryPath("path to ffmpeg"));
render.OnEncodingProgress += RenderProgressReceived;
render.Execute();//await render.ExecuteAsync();

void RenderProgressReceived(RenderProgress progress)
{
  //todo with progress
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caius Jard
Solution 2 Tr??ng Qu?c Khánh