C# Sample Codes and Examples

Note: New version of Media Handler Pro available, for media handler pro latest version documentation and sample codes visit product homepage .

Below are some c# sample codes for encoding and publishing videos from one format to another format, especially to Flash FLV format.

You can use Media Handler Pro in your C# applications to encode or publish videos to different formats, grab thumbnails from videos, extract audio from videos, post watermark on videos, split videos in many clips and other processings.

Available Classes and Functions

Convert video to FLV format:

Sample Code

//After uploading video

//Create object of Media_handler class

Media_handler _mediahandler=new Media_handler();

// Set root path of web application

string rootpath =Server.MapPath(Request.ApplicationPath);

//Set ffmpeg.exe path

string _ffmpegpath =HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");

//Set path of source video directory, path where original video exist //'
string inputpath = rootpath + "\\Images\\Default";

//Set path of destination video directory, path where generated, encoded video will be store

string outputpath = rootpath + "\\Images\\FLV";

// Call Convert_Media function of Media_handler class to convert selected video to flv //format

// Note sample available here written according to full version of component, free version //slightly have different return string than full version. please view product documentation //for more help.

//Method No 1

string fullstring = _mediahandler.Convert_Media(filename, _ffmpegpath, InputPath, OutputPath, 320, 240, 360, 25, 32, 22050, "0", true);

//this function return flv video name in free version and many outputs in the form of //string 'separated by comma in full version. e.g str = "FileName:04db71f6-a.flv,Duration: //00:00:29.8, 'start: 0.000000,size: 176x144,SamplingRate: 44100 Hz"

//Method No 2

string fullstring = _mediahandler.Convert_Media(filename, _ffmpegpath, InputPath, OutputPath, "320x240", 360, 25, 32, 22050, "0", true);

// Method No 3

string fullstring = _mediahandler.Convert_Media(filename, _ffmpegpath, InputPath, OutputPath, "320x240", 32, 44100, "0", true); Recommended for highest quality flv video

// Method No 4

string fullstring = _mediahandler.Convert_Media(filename, _ffmpegpath, InputPath, OutputPath, 32, 44100, "0", true); Recommended for highest quality flv video

// Method No 5

string fullstring = _mediahandler.Convert_Media(filename, _ffmpegpath, InputPath, OutputPath, 360, 32, 22050, "0", true);


Set Meta-Information for FLV Videos:

Sample Code

//After converting video to flv format or video is already in flv format

//Create object of Media_handler class

Media_handler _mediahandler=new Media_handler();

// Set root path of web application

string rootpath =Server.MapPath(Request.ApplicationPath);

// Set flvtool path
string _flvtoolpath =HttpContext.Current.Server.MapPath("~\\flvtool\\flvtool2.exe");

// Set path of source video directory, path where original video exist //'
string outputpath = rootpath + "\\Images\\FLV";

//Call Set_Buffering function to set meta-information to flv video

_mediahandler.Set_Buffering(outfile, _flvtoolpath, Path);

 

Grab image thumbnail from video:

Sample Code

//After uploading video and successfully converting to flv format

//Create object of Media_handler class

Media_handler _mediahandler=new Media_handler();

// Set root path of web application

string RootPath = Server.MapPath(Request.ApplicationPath)

//Set ffmpeg.exe path

string _ffmpegpath =HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");

// Set path of source video directory, path where original video exist //'
string inputpath = rootpath + "\\Images\\Default";

//Set path of destination video directory, path where generated, encoded video will be store

string outputpath = rootpath + "\\Images\\FLV";

//Set path of image directory, path where generated thumbnail image will be store

string thumbpath = rootpath + "\\Images\\Thumbs";

// Call Grab_Image function of Media_handler class to capture thumbnail from video

string image_name = _mediahandler.Grab_Image(outfile, _ffmpegpath, OutputPath, ThumbPath, "00:00:03", "png", 110, 90);

// Where image_name is the name of thumbnail that is captured and stored in thumbpath directory.
//For capturing multiple thumbnails in a sequence use.

string multiplethubms = _mediahandler.Grab_Multiple_Image(outfile, _ffmpegpath, InputPath, OutputPath, 4, "00:00:02", "jpg", 110, 90);
//where 4 shows number of frames or thumbnails to be capture.


Convert any format video to MPEG format:

Sample Code

//Create object of Media_handler class

Media_handler _mediahandler=new Media_handler();

//Set root path of web application

string rootpath =Server.MapPath(Request.ApplicationPath);

// Set ffmpeg.exe path

string _ffmpegpath =HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");

//Set path of source video directory, path where original video exist //'
string inputpath = rootpath + "\\Images\\Default";

//Set path of destination video directory, path where generated, encoded video will be store

string outputpath = rootpath + "\\Images\\MPEG";

//Call Convert_Media_To_MPEG function of Media_handler class to convert video to mpg format.

string mpg_video = _mediahandler.Convert_Media_To_MPEG(outfile, _ffmpegpath, InputPath, OutputPath);
//where mpg_video is the name of mpg video that is generated and stored in mpg folder.

Convert any format video to AVI format:

Sample Code

//Create object of Media_handler class

Media_handler _mediahandler=new Media_handler();

// Set root path of web application

string rootpath =Server.MapPath(Request.ApplicationPath);

// Set ffmpeg.exe path

string _ffmpegpath =HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");

//Set path of source video directory, path where original video exist //'
string inputpath = rootpath + "\\Images\\Default";

//Set path of destination video directory, path where generated, encoded video will be store

string outputpath = rootpath + "\\Images\\AVI";

//Call Convert_Media_To_AVI function of Media_handler class to convert video to avi format.

string avi_video = _mediahandler.Convert_Media_To_AVI(outfile, _ffmpegpath, InputPath, OutputPath);
//where avi_video is the name of avi video that is generated and stored in avi folder.

Convert any format video to 3gp format:

Sample Code

//Create object of Media_handler class

Media_handler _mediahandler=new Media_handler();

//Set root path of web application

string rootpath =Server.MapPath(Request.ApplicationPath);

// Set ffmpeg.exe path

string _ffmpegpath =HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");

' Set path of source video directory, path where original video exist //'
string inputpath = rootpath + "\\Images\\Default";

//Set path of destination video directory, path where generated, encoded video will be store

string outputpath = rootpath + "\\Images\\3gp";

// Call Convert_Media_To_3gp function of Media_handler class to convert video to avi format.

string 3gp_video = _mediahandler.Convert_Media_To_3gp(outfile, _ffmpegpath, InputPath, OutputPath);
//where 3gp_video is the name of 3gp video that is generated and stored in 3gpfolder.

Execute custom FFMPEG commands:

Sample Code

//Create object of Media_handler class

Media_handler _mediahandler=new Media_handler();

//Set root path of web application

string rootpath =Server.MapPath(Request.ApplicationPath);

//Set ffmpeg.exe path

string _ffmpegpath =HttpContext.Current.Server.MapPath("~\\ffmpeg\\ffmpeg.exe");

//Call Execute_FFMPEG function of Media_handler class to execute custom ffmpeg commands according to your own choice.

_mediahandler.Execute_FFMPEG(_ffmpegpath, "-i c:\\test.avi -vcodec mp3 c:\\test.flv");