ASP.NET - FLV, MP4, Vorbis, VP8 WebM, PNG encoding in one step using Media Handler Pro


Posted July 24, 2010 0:21 AM

You can use the following sample code to publish any format video to flash flv, mp4, vp8 webm, ogg vorbis, grab thumb in one step using ASP.NET Media Handler Pro.


Note: You need to download latest ffmpeg build with support of VP8 codec


Sample Code:

_mhandler.InputPath = RootPath + "\\\\contents\\\\original";
_mhandler.OutputPath = RootPath + "\\\\contents\\\\output";
VideoInfo info = new VideoInfo();
_mhandler.FileName = "sample.wmv";
_mhandler.OutputFileName = "sample";
//***************************************
//FLV Encoding
//***************************************

_mhandler.OutputExtension = ".flv";
_mhandler.Video_Bitrate = 500;
_mhandler.Audio_Bitrate = 128;
_mhandler.Audio_SamplingRate = 44100;
_mhandler.Force = "flv";
info = _mhandler.Process();
if (info.ErrorCode > 0)
{
       Response.Write("Error occured while processing flv video");
       return;
}
_mhandler.Force = "";
//******************************************
// MP4 Encoding - HQ - 1 Pass
//******************************************

_mhandler.OutputExtension = ".mp4";
_mhandler.FrameRate = 23.97;
_mhandler.Video_Bitrate= 6144;
_mhandler.VCodec = "libx264";
_mhandler.Parameters = "-bt 8192k -coder 1 -flags +loop -cmp +chroma -partitions -parti8x8-parti4x4-partp8x8-partp4x4-partb8x8 -me_method dia -subq 2 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -bf 3 -refs 1 -directpred 3 -trellis 0 -flags2 -bpyramid-wpred-mixed_refs-dct8x8+fastpskip -wpredp 2";
info = _mhandler.Process();
if (info.ErrorCode > 0)
{
     Response.Write("Error occured while processing mp4 video");
     return;
}


//******************************************
// MP4 Encoding - Normal
//******************************************

_mhandler.OutputExtension = ".mp4";
_mhandler.FrameRate = 29.97;
_mhandler.Video_Bitrate = 200;
_mhandler.VCodec = "libx264";
_mhandler.Parameters = "-bt 240k -coder 1 -flags +loop -cmp +chroma -partitions +parti8x8+parti4x4+partp8x8+partb8x8 -me_method hex -subq 6 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -b_strategy 1 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -bf 3 -refs 2 -directpred 3 -trellis 0 -flags2 +wpred+dct8x8+fastpskip -wpredp 2";
_mhandler.Channel = 2;
_mhandler.Audio_SamplingRate = 48000;
_mhandler.Audio_Bitrate = 192;
info = _mhandler.Process();
if (info.ErrorCode > 0)
{
     Response.Write("Error occured while processing mp4 video");
     return;
}
//******************************************
// MP4 Encoding - ipod - ipone - 1 pass
//******************************************

_mhandler.OutputExtension = ".mp4";
_mhandler.FrameRate = 29.97;
_mhandler.Video_Bitrate = 512;
_mhandler.VCodec = "libx264";
_mhandler.Width = 480;
_mhandler.Height = 272;
_mhandler.AspectRatio = "480:272";
_mhandler.Parameters = "-bt 1024k -maxrate 4M -flags +loop -cmp +chroma -me_range 16 -g 300 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -rc_eq \\"blurCplx^(1-qComp)\\" -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -coder 0 -refs 1 -bufsize 4M -level 21 -partitions parti4x4+partp8x8+partb8x8 -subq 5";
_mhandler.Force = "mp4";
info = _mhandler.Process();
if (info.ErrorCode > 0)
{
      Response.Write("Error occured while processing mp4 video");
      return;
}
// ************************************
// Clear parameters
// ************************************

_mhandler.FrameRate = 0;
_mhandler.Video_Bitrate = 0;
_mhandler.VCodec = "";
_mhandler.AspectRatio = "";
_mhandler.Parameters = "";
_mhandler.Force = "";
_mhandler.Pass = 0;
_mhandler.Channel = 0;
_mhandler.Audio_Bitrate = 0;
_mhandler.Audio_SamplingRate = 0;
//************************************
// OGG Theora Encoding
//************************************

_mhandler.OutputExtension = ".ogg";
// set parameters with ogv video / ogg audio
info = _mhandler.Process();
if (info.ErrorCode > 0)
{
      Response.Write("Error occured while processing ogg file");
      return;
}
//***************************************
// VP8 WebM Encoding (Standard Version)
//***************************************

_mhandler.OutputExtension = ".webm";
_mhandler.Force = "webm";
_mhandler.AspectRatio = "4:3";
_mhandler.VCodec = "libvpx";
_mhandler.ACodec = "libvorbis";
_mhandler.Deinterlace = true;
_mhandler.Parameters = "-threads 8 -g 120 -level 216 -profile 0 -qmax 42 -qmin 10 -vb 2M -aq 50 -ac 6";
info = _mhandler.Process();
if (info.ErrorCode > 0)
{
      Response.Write("Error occured while processing webm video");
      return;
}
//****************************************************
// Grab Thumbs
//****************************************************

int calculate_mid_duration_sec = 5; // default 5 seconds
if (info.Duration_Sec > 0)
       calculate_mid_duration_sec = (int)info.Duration_Sec / 2;
//  create thumbnail
_mhandler.VCodec = "";
_mhandler.ACodec = "";
_mhandler.Force = "";
_mhandler.Parameters = "";
_mhandler.ImageName = "bear.png";
_mhandler.OutputFileName = "bear";
_mhandler.OutputExtension = "png";
_mhandler.Image_Format = "png";
_mhandler.Frame_Time = calculate_mid_duration_sec.ToString();
_mhandler.Width = 320;
_mhandler.Height = 240;
info = _mhandler.Grab_Thumb();
// check for errors
if (info.ErrorCode > 0)
{
     Response.Write("Error Occured while grabbing thumbs");
     return;
}
Response.Write("All tasks completed successfully");



Represent comments, so use code when required.


You can publish multiple format videos that can be playable on variety of ways, you can use mp4, webm, ogg vorbis to play on HTML 5 player.



Tags: html5, webm, asp.net vp8, asp.net video encoding
File Under: Media Handler Pro
comments powered by Disqus