我使用带有进度条指示的 .net ffmpeg 包装器http://www.mediasoftpro.com实现了后台视频处理,以计算处理了多少视频并将信息发送到网页以更新进度条指示器。如果一次只有一个进程工作,它工作正常,但在两个并发进程的情况下(从两台不同的计算机同时开始两个视频发布),进度条突然混合进度状态。这是我的代码,我使用静态对象将单个实例的信息正确发送到进度条。
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.
}
这是负责在每秒等之后更新进度条指示的 jquery 函数。
$(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();
}
}
});
}
由于静态媒体处理程序对象而出现问题
public static MediaHandler _mhandler = new MediaHandler();
我需要一种方法来保持两个并发进程信息彼此分开,以便更新进度条,其值恰好属于该进程。