MP4 Encoding & Setting Meta Information via Media Handler Pro

25 October 2010

Here we provide sample code that can help u to use Media Handler Pro component in your application (c#, to

i: Publish any format video to high quality mp4 format using libx264 codec and preset files
ii: Set meta information to mp4 video in order to stream properly on web.

We recommend you to download latest ffmpeg build with presets support from

i: Sample Code for MP4 Video using libx264 codec and preset files

private string PresetFilePath = // path of preset file u are doing to use for mp4 encoding. preset files located withing ffmpeg/preset/ folder.
_mhandler.OutputExtension = ".mp4";
_mhandler.OutputFileName = "sample_temp";
_mhandler.VCodec = "libx264";
_mhandler.Parameters = " -bt 240k -fpre " + this.PresetFilePath + "";
_mhandler.Video_Bitrate = 200;
_mhandler.Channel = 2;
_mhandler.Audio_SamplingRate = 48000;
_mhandler.Audio_Bitrate = 192;
_mhandler.FrameRate = 29.97;
info = _mhandler.Process();

The code above will create mp4 video with name 'sample_temp.mp4', In order to stream this video on web you need to add meta information otherwise players will first download complete video and will then play it.
For adding meta information we are using mp4box with Media Handler Pro. U can use the following code to set meta information and make streamable mp4 video from 'sample_temp.mp4' video via mp4box utility.

string MP4BoxPath = // path to mp4box utility in similar way as ffmpeg or preset file path.
_mhandler.MP4BoxPath = MP4BoxPath;
string  _mp4_temp_path = // complete path of output sample_temp.mp4 video
_mhandler.Parameters = "-isma -hint -add " + _mp4_temp_path + "";
_mhandler.FileName = sample_temp.mp4.Replace("_temp.mp4",".mp4"); // replace temp.mp4 with normal mp4 video, remove temp video after creating streamable mp4 video. e.g (sample.mp4)
_mhandler.InputPath = this.OutputPath;

Set_MP4_Buffering() will create streamable mp4 video (sample.mp4) from (sample_temp.mp4) video.

After creation you can delete sample_temp.mp4 video.