I implemented background video processing using .net ffmpeg wrapper http://www.mediasoftpro.com with progress bar indication to calculate how much video is processed and send information to web page to update progress bar indicator. Its working fine if only single process works at a time, but in case of two concurrent processes (start two video publishing at once let say from two different computers), progress bar suddenly mixed progress status. Here is my code where i used static objects to properly send information of single instance to progress bar.
static string FileName = "grey_03";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.Params["file"] != null)
{
FileName = Request.Params["file"].ToString();
}
}
}
public static double ProgressValue = 0;
public static MediaHandler _mhandler = new MediaHandler();
[WebMethod]
public static string EncodeVideo()
{
// MediaHandler _mhandler = new MediaHandler();
string RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
_mhandler.FFMPEGPath = HttpContext.Current.Server.MapPath("~\\ffmpeg_july_2012\\bin\\ffmpeg.exe");
_mhandler.InputPath = RootPath + "\\contents\\original";
_mhandler.OutputPath = RootPath + "\\contents\\mp4";
_mhandler.BackgroundProcessing = true;
_mhandler.FileName = "Grey.avi";
_mhandler.OutputFileName =FileName;
string presetpath = RootPath + "\\ffmpeg_july_2012\\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;
_mhandler.ProcessMedia();
return _mhandler.vinfo.ErrorCode.ToString();
}
[WebMethod]
public static string GetProgressStatus()
{
return Math.Round(_mhandler.vinfo.ProcessingCompleted, 2).ToString();
// if vinfo.processingcomplete==100, then you can get complete information from vinfo object and store it in database and perform other processing.
}
Here is jquery functions responsible for updating progress bar indication after every second etc.
$(function () {
$("#vprocess").on({
click: function (e) {
ProcessEncoding();
var IntervalID = setInterval(function () {
GetProgressValue(IntervalID);
}, 1000);
return false;
}
}, '#btn_process');
});
function GetProgressValue(intervalid) {
$.ajax({
type: "POST",
url: "concurrent_03.aspx/GetProgressStatus",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Do something interesting here.
$("#pstats").text(msg.d);
$("#pbar_int_01").attr('style', 'width: ' + msg.d + '%;');
if (msg.d == "100") {
$('#pbar01').removeClass("progress-danger");
$('#pbar01').addClass("progress-success");
if (intervalid != 0) {
clearInterval(intervalid);
}
FetchInfo();
}
}
});
}
The problem arises due to static mediahandler object
public static MediaHandler _mhandler = new MediaHandler();
I need a way to keep two concurrent processes information separate from each other in order to update progress bar with value exactly belong to that process.