Convert Audio, Video to Any Format using C#

Have you ever wondered how VLC is able to play almost any kind of video or audio format, yet many others cannot? The secret behind VLC is ffplay, an amazing open source project that combines hundreds of codecs into one small library. The people that made ffplay also made ffmpeg, which, among many other things, can convert any video or audio file to almost any kind of format. 

Project link: http://ffmpeg.org/

Note: You’ll need to either download and build the executable from the source code, or much easier, you can download a Win32 or Win64 build from an ffmpeg unofficial build provider:

Download link: http://ffmpeg.arrozcru.org/builds/

The library supports tens of codecs, many that are obsolete or that no one has heard of. The library is very configurable but also very smart, guessing most of the media’s parameters (resolution, bit rate, etc.) without you having to input it. Just give this library a source file and a destination file and format, and it will do the rest. 

In this article, I’ll briefly describe an attached ffmpeg C# wrapper I wrote so that you can bring this enormously useful tool to your applications. I abstracted the actual conversion logic, so that you can plug in your converter into my shell using the following interface:

public interface IConverter
{
       Process StartConversion(Conversion conversion);
}
The StartConversion method takes a Conversion object, which holds the input, output destinations, the final format, and some other properties. It returns a System.Diagnostics.Process object which is the .NET representation of a Windows process. 

If you want to plug in your own IConverter, you can extend my class ConversionManager and override the method:

protected override IConverter CreateConverter(string converterPath)
{
       return new FFMpegWrapper(converterPath);
}
where FFMpegWrapper is your IConverter.

You can use my FFMpegWrapper directly too without using the ConversionManager, but both need to be initialized with the path to the ffmpeg.exe  executable on your machine. 

The ConversionManager method supports synchronous and asynchronous conversion. It will also manage one or more async conversions for you, and exposes events for when the conversion starts, progresses, and finishes. 

Here is a basic overview of the public methods provided:

public void Convert(string source, string destination)

public void Convert(string source, string destination, string optionalArguments)

public Conversion ConvertAsync(string source, string destination)

public Conversion ConvertAsync(string source, string destination, string optionalArguments)
However, since I am just started a separate process and channeling the output through the event, the my wrapper won’t actually know how much of the conversion is left, or whether it finished successfully. It will just know when it finished. 

If you are interested, take a look at the provided project to see how I wrap the ffmpeg executable using System.Diagnostics.ProcessStartInfo. 

Enjoy your converted media!

Ivolo.Tools.Conversion Source.zip (4,33 kb)

Ivolo.Tools.Conversion_win32.dll (9,50 kb)