
Concurrent Encoding
This example will help you implementing concurrent video processing in the background and providing publishing status to the application in real-time using asp.net.
Example Code
protected string SourceFileName = "wildlife.wmv";
protected string PublishedFileName = "wildlife";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.Params["sfile"] != null)
{
this.SourceFileName = Request.Params["sfile"].ToString();
}
if (Request.Params["file"] != null)
{
this.PublishedFileName = Request.Params["file"].ToString();
}
}
}
public static List<MediaHandler> _lst = new List<MediaHandler>();
[WebMethod]
public static string EncodeVideo(string Source, string Published)
{
var_mhandler = new MediaHandler();
var RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
_mhandler.FFMPEGPath = HttpContext.Current.Server.MapPath("~\\ffmpeg\\bin\\ffmpeg.exe");
_mhandler.InputPath = RootPath + "\\contents\\original";
_mhandler.OutputPath = RootPath + "\\contents\\mp4";
_mhandler.BackgroundProcessing = true;
//_mhandler.FileName = "wildlife.wmv";
_mhandler.FileName = Source;
_mhandler.OutputFileName = Published;
string presetpath = RootPath + "\\ffmpeg\\presets\\libx264-ipod640.ffpreset";
_mhandler.Parameters = " -b:a 192k -b:v 500k -fpre \"" + presetpath + "\"";
_mhandler.OutputExtension = ".mp4";
_mhandler.VCodec = "libx264";
_mhandler.ACodec = "libvo_aacenc";
_mhandler.Channel = 2;
//******************************
// 360p mp4 encoding
//******************************
_mhandler.Width = 640;
_mhandler.Height = 380;
_mhandler.Audio_SamplingRate = 44100;
//*******************************
// 480p mp4 encoding
//*******************************
//_mhandler.Width = 854;
//_mhandler.Height = 480;
//_mhandler.Video_Bitrate = 1000;
//_mhandler.Audio_SamplingRate = 44100;
//_mhandler.Audio_Bitrate = 128;
//*******************************
// 720p mp4 encoding
//*******************************
//_mhandler.Width = 1280;
//_mhandler.Height = 720;
//_mhandler.Video_Bitrate = 2200;
//_mhandler.Audio_SamplingRate = 44100;
//_mhandler.Audio_Bitrate = 96;
//*******************************
// 1080p mp4 encoding
//*******************************
//_mhandler.Width = 1920;
//_mhandler.Height = 1080;
//_mhandler.Video_Bitrate = 2900;
//_mhandler.Audio_SamplingRate = 44100;
//_mhandler.Audio_Bitrate = 152;
_mhandler.ProcessMedia();
if (_mhandler.vinfo.ErrorCode > 0)
{
// validation error occured, don't proceed more
return _mhandler.vinfo.ErrorCode.ToString();
}
else
{
_mhandler.vinfo.ProcessID = Guid.NewGuid().ToString(); // unique guid to attach with each process to identify proper object on progress bar and get info request
_lst.Add(_mhandler);
return _mhandler.vinfo.ProcessID;
}
}
[WebMethod]
public static string GetProgressStatus(string ProcessID)
{
string completed_process = "0";
if (_lst.Count > 0)
{
int i = 0;
for (i = 0; i <= _lst.Count - 1; i++)
{
if (_lst[i].vinfo.ProcessID == ProcessID)
{
completed_process = Math.Round(_lst[i].vinfo.ProcessingCompleted, 2).ToString();
}
}
}
return completed_process;
}
[WebMethod]
public static string GetInformation(string ProcessID)
{
var str = new StringBuilder();
str.Append("<div class=\"item_pad_4\">\n");
str.Append("<table class=\"table table-bordered table-striped\">\n");
str.Append("<thead><tr>\n");
str.Append("<th>Property</th>\n");
str.Append("<th>Information</th>\n");
str.Append("</tr></thead>\n");
str.Append("<tbody>\n");
if (_lst.Count > 0)
{
int i = 0;
for (i = 0; i <= _lst.Count - 1; i++)
{
if (_lst[i].vinfo.ProcessID == ProcessID)
{
if (_lst[i].vinfo.ErrorCode > 0)
{
// error occurs in processing
str.Append("<tr><td>Error Code</td><td>" + _lst[i].vinfo.ErrorCode + "</td></tr>\n");
str.Append("<tr><td>Error Message</td><td>" + _lst[i].vinfo.ErrorMessage + "</td></tr>\n");
str.Append("<tr><td>FFMPEG Output</td><td>" + _lst[i].vinfo.FFMPEGOutput + "</td></tr>\n");
}
else
{
str.Append("<tr><td>Error Code</td><td>No Error Occured</td></tr>\n");
}
str.Append("<tr><td>File Name</td><td>" + _lst[i].vinfo.FileName + "</td></tr>\n");
str.Append("<tr><td>Video Duration</td><td>" + _lst[i].vinfo.Duration + "(" + _lst[i].vinfo.Duration_Sec + " seconds)</td></tr>\n");
str.Append("<tr><td colspan=\"2\">Output Values</td></tr>\n");
if (_lst[i].vinfo.Vcodec != "")
{
str.Append("<tr><td>Video Codec</td><td>" + _lst[i].vinfo.Vcodec + "</td></tr>\n");
}
if (_lst[i].vinfo.Acodec != "")
{
str.Append("<tr><td>Audio Codec</td><td>" + _lst[i].vinfo.Acodec + "</td></tr>\n");
}
if (_lst[i].vinfo.Video_Bitrate != "")
{
str.Append("<tr><td>Video Bitrate</td><td>" + _lst[i].vinfo.Video_Bitrate + "</td></tr>\n");
}
if (_lst[i].vinfo.Audio_Bitrate != "")
{
str.Append("<tr><td>Audio Bitrate</td><td>" + _lst[i].vinfo.Audio_Bitrate + "</td></tr>\n");
}
if (_lst[i].vinfo.SamplingRate != "")
{
str.Append("<tr><td>Audio Sampling Rate</td><td>" + _lst[i].vinfo.SamplingRate + "</td></tr>\n");
}
if (_lst[i].vinfo.Channel != "")
{
str.Append("<tr><td>Audio Channel</td><td>" + _lst[i].vinfo.Channel + "</td></tr>\n");
}
if (_lst[i].vinfo.Width > 0)
{
str.Append("<tr><td>Width</td><td>" + _lst[i].vinfo.Width + "</td></tr>\n");
}
if (_lst[i].vinfo.Height > 0)
{
str.Append("<tr><td>Height</td><td>" + _lst[i].vinfo.Height + "</td></tr>\n");
}
if (_lst[i].vinfo.FrameRate != "")
{
str.Append("<tr><td>Framerate</td><td>" + _lst[i].vinfo.FrameRate + "</td></tr>\n");
}
str.Append("<tr><td colspan=\"2\">Optional Information</td></tr>\n");
if (_lst[i].vinfo.Producer != "")
{
str.Append("<tr><td>Producer</td><td>" + _lst[i].vinfo.Producer + "</td></tr>\n");
}
if (_lst[i].vinfo.Music != "")
{
str.Append("<tr><td>Music</td><td>" + _lst[i].vinfo.Music + "</td></tr>\n");
}
if (_lst[i].vinfo.Footage != "")
{
str.Append("<tr><td>Footage</td><td>" + _lst[i].vinfo.Footage + "</td></tr>\n");
}
if (_lst[i].vinfo.ProcessingCompleted >= 100)
{
// remove from list of corrent processes if processes reach this point
// store all information of completed process and remove it from list of concurrent processes
// e.g
VideoInfo current_uploaded_video_info = _lst[i].vinfo;
_lst.Remove(_lst[i]);
}
}
}
}
str.Append("</tbody>\n");
str.Append("</table>\n");
str.Append("</div>\n");
return str.ToString();
}