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.
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:
publicclassVideoConnector:IVideoConnector{privatereadonlyDictionary<VideoFileType,Action<FFMpegArgumentOptions>>_optionsMap=new();publicVideoConnector(){_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);}publicStreamConvertVideo(StreamsourceStream,VideoConvertOptionsconvertOptions){varresultStream=newMemoryStream();vararguments=FFMpegArguments.FromPipeInput(newStreamPipeSource(sourceStream)).OutputToPipe(newStreamPipeSink(resultStream),options=>{if(_optionsMap.ContainsKey(convertOptions.Format)){_optionsMap[convertOptions.Format].Invoke(options);}else{thrownewInvalidOperationException($"Conversion to {convertOptions.Format.Extension} is not supported at the moment");}});arguments.ProcessSynchronously();returnresultStream;}privatevoidSetAviConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("avi");}privatevoidSetFlvConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("flv");}privatevoidSetMkvConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("matroska");}privatevoidSetMp4ConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("mp4");options.WithCustomArgument("-movflags empty_moov");}privatevoidSetWmvConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("asf");}privatevoidSetMovConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("mov");options.WithCustomArgument("-movflags empty_moov");}privatevoidSetWebmConvertOptions(FFMpegArgumentOptionsoptions){options.ForceFormat("webm");}}
Once the IVideoConnector interface is implemented, the AVI to MP4 conversion code snippet looks like this:
After v24.10 and later:
// Load the source AVI fileVideoLoadOptionsloadOptions=newVideoLoadOptions();loadOptions.SetVideoConnector(newVideoConnector());using(Converterconverter=newConverter("sample.avi",(LoadContextloadContext)=>loadOptions)){// Set the convert options for MP4 formatVideoConvertOptionsoptions=newVideoConvertOptions{Format=VideoFileType.Mp4};// Convert to MP4 formatconverter.Convert("converted.mp4",options);}
Before v24.10:
// Load the source AVI fileVideoLoadOptionsloadOptions=newVideoLoadOptions();loadOptions.SetVideoConnector(newVideoConnector());using(Converterconverter=newConverter("sample.avi",()=>loadOptions)){// Set the convert options for MP4 formatVideoConvertOptionsoptions=newVideoConvertOptions{Format=VideoFileType.Mp4};// Convert to MP4 formatconverter.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:
With v24.10 and later:
// Load the source AVI fileVideoLoadOptionsloadOptions=newVideoLoadOptions();loadOptions.SetVideoConnector(newVideoConnector());using(Converterconverter=newConverter("sample_with_audio.avi",(LoadContextloadContext)=>loadOptions)){// Set the convert optionsVideoConvertOptionsoptions=newVideoConvertOptions{ExtractAudioOnly=true,AudioFormat=AudioFileType.Ogg};// Convert to audio fileconverter.Convert("extracted_audio.ogg",options);}
Before v24.10:
// Load the source AVI fileVideoLoadOptionsloadOptions=newVideoLoadOptions();loadOptions.SetVideoConnector(newVideoConnector());using(Converterconverter=newConverter("sample_with_audio.avi",()=>loadOptions)){// Set the convert optionsVideoConvertOptionsoptions=newVideoConvertOptions{ExtractAudioOnly=true,AudioFormat=AudioFileType.Ogg};// Convert to audio fileconverter.Convert("extracted_audio.ogg",options);}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.