Skip to main content

C# Sample Codes

List of c# sample codes below that can help you to use .NET Media Handler Pro in your c# projects for video publishing, thumbs grabbing, extract audio, post watermark on video and perform more media related tasks.

Steps:

Before using c# sample code in your project make sure to

i: download ffmpeg build, unzip and place ffmpeg folder with all files on root of your web application.

ii: copy mediahandlerpro.dll and paste it into /bin/ directory of your web application.

iii: create three directories /contents/original, /contents/output/, /contents/thumbs/ for storing source, output video and generated thumbs.

 

Sample code and project available with each product bundle.

Example Codes

i: Setup Required Parameters

MediaHandler _mhandler = new MediaHandler();
string RootPath = Server.MapPath(Request.ApplicationPath);
// set ffmpeg path
_mhandler.FFMPEGPath = HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");
// set source directory path
_mhandler.InputPath = RootPath + "\\contents\\original";
// set output directory path
_mhandler.OutputPath = RootPath + "\\contents\\output";
// set source filename
_mhandler.FileName = "sample.mp4";
// set output filename
_mhandler.OutputFileName = "sample.3gp";

ii: C# code for posting watermark on video.

// not for posting watermark, ffmpeg must support and exist vhook/watermark.dll
// set watermark image directory path
_mhandler.WaterMarkPath = RootPath + "\\contents\\watermark";
// set watermark image filename
_mhandler.WaterMarkImage = "watermark.gif";
// more detail about posting watermark, click here.

iii: C# code for setting important parameter that can allow user to customize output video.

// set video bitrate, depend on output video format
_mhandler.Video_Bitrate = 500;
// set audio bitrate, depend on output video format
_mhandler.Audio_Bitrate = 64;
// set audio sampling rate, depend on output video format
_mhandler.Audio_SamplingRate = 22050;
// set width of output video
_mhandler.Width = 320;
// set height of output video
_mhandler.Height = 240;
// for complete list of parameters, click here , note some parameters may or may not be used for every circumstances so use parameters when required.

iv: C# code for encoding flash flv video using .NET Media Handler Pro

// set source filename
_mhandler.FileName = "sample.mp4";
// set output filename
_mhandler.OutputFileName = "sample.flv";
// start video procesing
VideoInfo info = _mhandler.Encode_FLV();
//or
VideoInfo info = _mhandler.Process();
                    

v: C# code for encoding .3gp video using .NET Media Handler Pro

// set source filename
_mhandler.FileName = "sample.mp4";
// set output filename
_mhandler.OutputFileName = "sample.3gp";
// start  video procesing
VideoInfo info = _mhandler.Encode_3GP();
//or
VideoInfo info = _mhandler.Process();
                    

vi: C# code for encoding AVI video using .NET Media Handler Pro 5.0

// set source filename
_mhandlerFileName = "sample.mp4";
// set output filename
_mhandler.OutputFileName = "sample.avi";
// set video parameters according to pal-vcd
_mhandler.TargetFileType = "pal-vcd";
// start video  procesing
VideoInfo info = _mhandler.Encode_AVI();
//or
VideoInfo info = _mhandler.Process();

v: C# code to generate video from list of images using .NET Media Handler Pro 5.0

// i: All thumbs must be in sequest and in .jpg format e.g a_001.jpg, a_002.jpg, a_003.jpg....
// ii: FileName property below must point actual name of thumb without .jpg and sequence e.g "a_" represent "a_001.jpg...." 
_mhandlerFileName = "a_";
// set output video filename without any extension
_mhandler.OutputFileName = "a";
// set output extension
_mhandler.OutputExtension = ".flv";
// set processing
VideoInfo info = _mhandler.ImagesToVideo();

vi: C# code for grabbing single or multiple thumbs using .NET Media Handler Pro 5.0

//***************************************************
// Multiple Thumbs: Thumb Mod: Normal // Recommended for multiple thumbs as it support long length videos too
//***************************************************
// set thumb start index, e.g sample_ generate sample_001.jpg, sample_002.jpg etc
string thumb_start_index = "sample_";
// set source video filename
_mhandlerFileName = "sample.mp4";
// set output image format
_mhandler.Image_Format = "jpg";
// set image codec (optional)
_mhandler.VCodec = "image2";
// set image name, in multiple mode image index required
_mhandler.ImageName = thumb_start_index;
// set to enable multiple thumb mode
_mhandler.Multiple_Thumbs = true;
_mhandler.ThumbMode = 0;
// set total no of thumb to be generating from video.
_mhandler.No_Of_Thumbs = 15;
// set start positing / length of video from where thumb grabbing start
_mhandler.Thumb_Start_Position = 10;
// set width and height of thumb, not it must be multiple of two.
_mhandler.Width = 160;
_mhandler.Height = 120;
// start grabbing thumbs
VideoInfo info = _mhandler.Grab_Thumb();
//******************************************************
// Single Thumb
//******************************************************
_mhandler.FileName = "sample.mp4";
// set output image name
_mhandler.ImageName = "sample.jpg";
// set length in seconds from where you want to grab thumb
_mhandler.Frame_Time = "10";
//OR
//_mhandlerThumb_Start_Position = 10;
_mhandler.Image_Format = "jpg";
_mhandlerVCodec = "image2";
_mhandler.Width = 160;
_mhandler.Height = 120;
// start grabbing single thumb from video
VideoInfo info = _mhandler.Grab_Thumb();

vi: C# code for joining two or more video clips in one output using .NET Media Handler Pro 5.0

// set list of filenames to be joined with single output. not required that all clips in same format and size
_mhandler.FileNames = new string[] { "sample_01.mp4", "sample_02.mp4" };
// set output video name, without extension recommended.
_mhandler.OutputFileName = "sample_full";
// set output extension
_mhandler.OutputExtension = ".avi";
// set width and height of output video.  recommended to avoid any mismanagement in width and height of different video clips
_mhandler.Width = 320;  
_mhandler.Height = 240;
_mhandler.TargetFileType = "pal-vcd";
VideoInfo info = _mhandler.Join_Videos();

vi: C# code for splitting video or audio in two or more clips using .NET Media Handler Pro 5.0

_mhandler.FileName = "sample.avi";
_mhandler.OutputFileName = "split_video";
_mhandler.OutputExtension = ".avi";
_mhandler.Width = 320;
_mhandler.Height = 240;
_mhandler.TargetFileType = "pal-vcd"
// set length in seconds of each clip
int length_of_video = 20;
// set total no of clips to be generating
int total_clips = 10;
VideoInfo info = _mhandler.Split_Video(length_of_video);
// or
VideoInfo info = _mhandler.Split_Video(length_of_video,total_clips);

vi: C# code for retrieving information from media (video, audio, image) using .NET Media Handler Pro 5.0

 // set media filename
_mhandler.FileName = "sample.mp4";
VideoInfo info =_mhandler.Get_Info();

vi: C# code for error validation after processing completes

// start validation whether media processed properly or failed.
// info object store all information retreived from media after processing completes.
if (infoErrorCode > 0 && info.ErrorCode != 121)
{
    Response.Write("Video processing failed, Error code " + info.ErrorCode + " generated");
    return;
}
// if ErrorCode returned by function is greater than zero, means there is error occured. for detail about error codes click here

vi: C# code for retrieving information from source and output media (video, audio, image) using .NET Media Handler Pro 5.0

  // info object store all information retreived from media after processing completes.
// After errorcode validation you can retrieve source and output video information from info object as 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 />");
    // Retrieve information from source media
    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 />");
  
  // Retrieve information from output video
    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 />");
// for more detail about videoinfo object visit video output parameter section.