ASP.NET MP4 Encoding

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 detail sample codes which can help you publishing high quality mp4 videos using libx264 preset files and add meta information in order to stream properly on the web.

For more topics and discussion about media handler pro component visit click here


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.

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("

" + 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.

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();

Complete Code

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.

  • Publish mp4 video as temp video e.g sample_temp.mp4
  • Generate streameable mp4 video from temp mp4 video.
  • Delete temp mp4 video.
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("

" + info.FFMPEGOutput + ""); return; }

Retrieve Information From Video

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("Input Values + "<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("Output Values + "<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 + "<br />");
str.Append("Error Code= " + info.ErrorCode + "<br />");
Response.Write(str.ToString());

Click here for detail about retrieving information from video