After looking at some of FFMPEG wrappers that are available, I noticed that some of them at least try to retrieve duration property directly from the console output of the file that is being encoded. I will try to explain why this is not such a good idea. First and foremost FFMPEG is note designed for this purpose. There is another tool called FFPROBE that is capable of retrieving all necessary data in friendlier JSON and XML formats. However let’s assume that we want to do that using FFMPEG. In the typical situation when we convert some audio or video file, right before we see statistics, FFMPEG displays all additional information about streams, metadata etc. There could be 40 to 50 lines of details including single tag we want which is file duration in seconds. If we take this console output and match it using Regex, we will find this tag and almost immediately we will get empty string. It is because FFMPEG output stream is constantly updated. We will need some way of retaining duration value as constant and this is where things get complicated. . If we include duration property in EncodingEventArgs, we will be sending empty strings to UI and duration will be shown only once.
To spare ourselves trouble is better to completely hide banner information. That’s why I included hard coded “-v quiet” option in Convert method. Option “-stats “ensures that we can only see actual conversion statistics. This way there will be no delay is sending that information to UI.
We could probe the file for single purpose of getting duration property. We can do that by omitting output file parameter. FFMPEG would complain that is missing something, but it would work.
ffmpeg.exe –i inputFile
It’s questionable whether we can use DoWork method for this purpose. We probably could create another private method that would subscribe to ErrorDataReceived event. However it looks like we will be trying to do two different things with the same method, convert and probe. Another way will be to create separate DoProbe method or Prober class that will simply copy most of the code from EncodingEngine class. It sounds repetitive.
I will post later article about FFPROBE wrapper that I created. It has an option of selecting XML or JSON file formats. Output string can be easily parsed and manipulated and it contains much more information that what we can get through FFMPEG.