C# Sample Codes and Examples

Here you can find some sample codes and example of how to use c# to encode videos from any video format to FLV video format and others. You can use Media Handler Pro in your c# applications to encode videos to different formats and perform other processings including posting wartermark on videos, encode / convert videos to flv format, grab single or multiple thumbnails from video, extract audio from videos to generate mp3 audio files and others.

Please view some sample c# codes and examples below. If you have to know more about it, please contact us.

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

©2007 - 2008, MediaSoft Pro Inc  | Site Map