Live Chat with Operator
Home > Media Handler Pro > Publish MP4 Video

MP4 Video Encoding Using ASP.NET

ASP.NET Media Handler Pro can be use to encode any format video to high quality mp4 video and audio file, add meta information to prepare it for web streaming and and make it compatible to be played on different devices and players including HTML5 and Flash.

This topic will provide sample code that can be used to

  • Create mp4 videos using libx264 codec with presets
  • Apply meta information in order to stream properly on the web.

C# Sample Code

Sample code below will generate mp4 video using libx264 codec and libx264-baseline.ffpreset preset file. You can use other preset files to generate mp4 video for ipod, iphone or high definition for computers.

Note: In case you are using free version of media handler pro, just comment properties like parameters if not supported.

MediaHandler _mhandler = new MediaHandler();
string RootPath = Server.MapPath(Request.ApplicationPath);
_mhandler.FFMPEGPath = HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");
_mhandler.InputPath = RootPath + "\\contents\\original";
_mhandler.OutputPath = RootPath + "\\contents\\mp4";
_mhandler.FileName = "Wildlife.wmv";
string presetpath = RootPath + "\\ffmpeg\\presets\\libx264-baseline.ffpreset";
_mhandler.OutputExtension = ".mp4";
_mhandler.OutputFileName = "canon_temp";
_mhandler.VCodec = "libx264";
_mhandler.Parameters = " -s 320x240 -fpre " + presetpath + "";
_mhandler.Video_Bitrate = 500;
_mhandler.Channel = 2;
_mhandler.Audio_SamplingRate = 48000;
_mhandler.Audio_Bitrate = 192;
_mhandler.FrameRate = 25;
VideoInfo info = _mhandler.Process(); // start encoding mp4 video via process method
Validation Check

This check will make sure whether video is successfully published or failed due to any reason returned through error code.

if (info.ErrorCode > 0)
{
Response.Write("Video processing failed, Error code " + info.ErrorCode + " generated");
Response.Write("<br />" + info.FFMPEGOutput + "");
return;
}

Add Meta Information to Mp4 Video

Once mp4 video published, it still have lack of streaming on the web. In order to stream it properly, you must add meta information to mp4 video to make it streameable and buffering properly.

For adding meta information and make it streameable mp4 video, we used mp4box utility with asp.net media handler pro.

Sample code below

string _mp4_org_path = "\"" + _mhandler.OutputPath + "\\canon_temp.mp4\"";
// Set mp4box path
_mhandler.MP4BoxPath = RootPath + "\\mp4box\\mp4box.exe";
// Prepare mp4box meta information parameters
_mhandler.Parameters = "-isma -hint -add " + _mp4_org_path + "";
// Set final mp4 video path
_mhandler.FileName = "Sample_Final.mp4";
// Set path of folder where final mp4 video will store
_mhandler.InputPath = _mhandler.OutputPath;
// Start mp4 meta information process
_mhandler.Set_MP4_Buffering();

All Code in One Step

Sample code shown below will publish mp4 video and set meta information for mp4 video in one step.

Two steps involve in publishing and setting meta information.

  1. Publish mp4 video as temp video e.g sample_temp.mp4
  2. Generate streameable mp4 video from temp mp4 video.
  3. Delete temp mp4 video.

Sample code below

MediaHandler _mhandler = new MediaHandler();
string RootPath = Server.MapPath(Request.ApplicationPath);
_mhandler.FFMPEGPath = HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");
_mhandler.InputPath = RootPath + "\\contents\\original";
_mhandler.OutputPath = RootPath + "\\contents\\mp4";
_mhandler.FileName = "Wildlife.wmv";
string presetpath = RootPath + "\\ffmpeg\\presets\\libx264-baseline.ffpreset";
_mhandler.OutputExtension = ".mp4";
_mhandler.OutputFileName = "canon_temp";
_mhandler.VCodec = "libx264";
_mhandler.Parameters = " -s 320x240 -fpre " + presetpath + "";
_mhandler.Video_Bitrate = 500;
_mhandler.Channel = 2;
_mhandler.Audio_SamplingRate = 48000;
_mhandler.Audio_Bitrate = 192;
_mhandler.FrameRate = 25;
VideoInfo info = _mhandler.Process(); // start encoding mp4 video via process method
// set meta information for mp4 video
string _mp4_org_path = "\"" + _mhandler.OutputPath + "\\canon_temp.mp4\"";
// Set mp4box path
_mhandler.MP4BoxPath = RootPath + "\\mp4box\\mp4box.exe";
// Prepare mp4box meta information parameters
_mhandler.Parameters = "-isma -hint -add " + _mp4_org_path + "";
// Set final mp4 video path
_mhandler.FileName = "Sample_Final.mp4";
// Set path of folder where final mp4 video will store
_mhandler.InputPath = _mhandler.OutputPath;
// Start mp4 meta information process
_mhandler.Set_MP4_Buffering();
// delete temp mp4 video
if (System.IO.File.Exists(temp_mp4_path))
System.IO.File.Delete(temp_mp4_path);
// retrieve valudes
if (info.ErrorCode > 0)
{
Response.Write("Video processing failed, Error code " + info.ErrorCode + " generated");
Response.Write("<br />" + info.FFMPEGOutput + "");
return;
}

Retrieve Video Information

Once published asp.net media handler pro will returned source and published video information as VideoInfo object. Sample information shown below

StringBuilder str = new StringBuilder();
str.Append("File Name= " + info.FileName + "<br />");
str.Append("Video Duration= " + info.Duration + "<br />");
str.Append("Video Duration in Seconds= " + info.Duration_Sec + "<br />");
// Input values
str.Append("<strong>Input Values</strong><br />");
str.Append("Video Codec= " + info.Input_Vcodec + "<br />");
str.Append("Audio Codec= " + info.Input_Acodec + "<br />");
str.Append("Video Bitrate= " + info.Input_Video_Bitrate + "<br />");
str.Append("Audio Bitrate= " + info.Input_Audio_Bitrate + "<br />");
str.Append("Audio Sampling Rate= " + info.Input_SamplingRate + "<br />");
str.Append("Audio Channel= " + info.Input_Channel + "<br />");
str.Append("Width= " + info.Input_Width + "<br />");
str.Append("Height= " + info.Input_Height + "<br />");
str.Append("Video FrameRate= " + info.Input_FrameRate + "<br />");
// Output values
str.Append("<strong>Output Values</strong><br />");
str.Append("Video Codec= " + info.Vcodec + "<br />");
str.Append("Audio Codec= " + info.Acodec + "<br />");
str.Append("Video Bitrate= " + info.Video_Bitrate + "<br />");
str.Append("Audio Bitrate= " + info.Audio_Bitrate + "<br />");
str.Append("Audio Sampling Rate= " + info.SamplingRate + "<br />");
str.Append("Audio Channel= " + info.Channel + "<br />");
str.Append("Width= " + info.Width + "<br />");
str.Append("Height= " + info.Height + "<br />");
str.Append("Video FrameRate= " + info.FrameRate + "<br />");
str.Append(".................................<br />");
str.Append("FFMPEG Output:" + info.FFMPEGOutput + "");
str.Append("Error Code= " + info.ErrorCode + "<br />");
Response.Write(str.ToString());
© 2007 - 2012, mediasoftpro.com  | Site Map | Privacy Policy | Terms of Use