Convert videos

About video file formats

Everyone is familiar with videos that we daily watch on media devices such as Televisions, Cinema screens, and social media channels such as Facebook, Twitter, YouTube, and many others. But have you ever wondered how these videos are stored and the formats in which these can be uploaded for viewing? What is the best file format for your video to be hosted on different storage media? Video file formats comprise many different video types for storing videos and to be used for various purposes. Popular video file formats that you may have come across include AVI, MOV, WMV, M4V, and MP4.

Supported video file conversions

FromTo
AVIAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
Video: AVI, FLV, MKV, MOV, MP4, WEBM, WMV
FLVAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
Video: AVI, FLV, MKV, MOV, MP4, WEBM, WMV
MKVAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
Video: AVI, FLV, MKV, MOV, MP4, WEBM, WMV
MOVAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
Video: AVI, FLV, MKV, MOV, MP4, WEBM, WMV
MP4Audio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
Video: AVI, FLV, MKV, MOV, MP4, WEBM, WMV
WEBMAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
Video: AVI, FLV, MKV, MOV, MP4, WEBM, WMV
WMVAudio: AAC, AC3, AIFF, FLAC, M4A, MP3, OGG, WAV, WMA
Video: AVI, FLV, MKV, MOV, MP4, WEBM, WMV

Convert to another video format

With GroupDocs.Conversion you can easily convert your video file into another video file format.

To allow video conversions GroupDocs.Conversion provides an extension point to offload actual video conversion to the video processing library, but at the same time gives you the simplicity of conversion setup. The extension point is the IVideoConnector interface.

At first, you must decide which video processing library you will use. Different libraries have different setup processes.

In our example, we will use the FFMPEG library. We recommend using Chocolatey to install FFMPG and all needed dependencies. To install FFMPEG with Chocolatey, run the following console command:

choco install ffmpeg

Once the video processing library is installed, you must implement the IVideoConnector interface. For this implementation, the FFMpegCore](https://www.nuget.org/packages/FFMpegCore) NuGet package must be installed in your project. The following snippet provides a sample implementation:

public class VideoConnector : IVideoConnector
{
    private readonly Dictionary<VideoFileType, Action<FFMpegArgumentOptions>> _optionsMap = new();
    public VideoConnector()
    {
        _optionsMap.Add(VideoFileType.Avi, SetAviConvertOptions);
        _optionsMap.Add(VideoFileType.Flv, SetFlvConvertOptions);
        _optionsMap.Add(VideoFileType.Mkv, SetMkvConvertOptions);
        _optionsMap.Add(VideoFileType.Mp4, SetMp4ConvertOptions);
        _optionsMap.Add(VideoFileType.Wmv, SetWmvConvertOptions);
        _optionsMap.Add(VideoFileType.Mov, SetMovConvertOptions);
        _optionsMap.Add(VideoFileType.Webm, SetWebmConvertOptions);
    }

    public Stream ConvertVideo(Stream sourceStream, VideoConvertOptions convertOptions)
    { 
        var resultStream = new MemoryStream();


        var arguments = FFMpegArguments
            .FromPipeInput(new StreamPipeSource(sourceStream))
            .OutputToPipe(new StreamPipeSink(resultStream), options =>
            {
                if (_optionsMap.ContainsKey(convertOptions.Format))
                {
                    _optionsMap[convertOptions.Format].Invoke(options);
                }
                else
                {
                    throw new InvalidOperationException(
                        $"Conversion to {convertOptions.Format.Extension} is not supported at the moment");
                }
            });
        arguments.ProcessSynchronously();
            
        return resultStream;
    }

    private void SetAviConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("avi");
    }

    private void SetFlvConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("flv");
    }

    private void SetMkvConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("matroska");
    }

    private void SetMp4ConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("mp4");
        options.WithCustomArgument("-movflags empty_moov");
    }

    private void SetWmvConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("asf");
    }

    private void SetMovConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("mov");
        options.WithCustomArgument("-movflags empty_moov");
    }

    private void SetWebmConvertOptions(FFMpegArgumentOptions options)
    {
        options.ForceFormat("webm");
    }

}

Once the IVideoConnector interface is implemented, the AVI to MP4 conversion code snippet looks like this:

// Load the source AVI file
VideoLoadOptions loadOptions = new VideoLoadOptions();
loadOptions.SetVideoConnector(new VideoConnector());
using (Converter converter = new Converter("sample.avi", () => loadOptions))
{
    // Set the convert options for MP4 format
    VideoConvertOptions options = new VideoConvertOptions {
        Format = VideoFileType.Mp4
    };
    // Convert to MP4 format
    converter.Convert("converted.mp4", options);
}

Put it simply - you install the video processing library, implement the IVideoConnector interface which links the GroupDocs.Conversion with video processing library, load a video file into the Converter class providing the IVideoConnector instance, select the desired output format and GroupDocs.Conversion does all the rest.

Note
Refer to the API reference for more conversion options and customizations.

Extract audio track

Extracting an audio track from a video is similar to converting video, however, you need to set the ExtractAudioOnly property to true and specify the desired output format in the AudioFormat property:

// Load the source AVI file
VideoLoadOptions loadOptions = new VideoLoadOptions();
loadOptions.SetVideoConnector(new VideoConnector());
using (Converter converter = new Converter("sample_with_audio.avi", () => loadOptions))
{
    // Set the convert options
    VideoConvertOptions options = new VideoConvertOptions
    {
        ExtractAudioOnly = true,
        AudioFormat = AudioFileType.Ogg
    };
    // Convert to audio file
    converter.Convert("extracted_audio.ogg", options);
}