Convert Video

About Video File Formats

Everyone is familiar of 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 saved 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 of a number of different video types for storing videos and to be used for various purpose. Populate Video file formats that you may have come across include AVI, MOV, WMV, M4V, and MP4.

Convert from AVI

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

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

You first must decide which video processing library will use. Different libraries have different setup process.

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

choco install ffmpeg

Once video processing library is installed you must implement IVideoConnector. For this implementation the FFMpegCore nuget package must be installed in your project. The following snippet provides 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 IVideoConnector is implemented AVI to MP4 conversion code snippet will look 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 video processing library, impement IVideoConnector which links GroupDocs.Conversion with video processing library, load a video file into Converter providing the IVideoConnector instance, select desired output format and all the rest will be done by GroupDocs.Conversion.

© Aspose Pty Ltd 2001-2022. All Rights Reserved.