我使用.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();
我需要一种方法来将两个并发进程的信息彼此分开,以便用完全属于该进程的值来更新进度条。
最佳答案
我认为你在这里有一个误解。
由于您使用的是静态
变量,因此您会遇到并发问题。这是因为在 ASP.NET 中,static
变量由所有请求共享,因为它们每个 AppDomain 一次,直到应用程序池回收为止。
您应该公开一个 WCF 端点,您可以使用它来询问您的服务是否已完成。该服务应该运行一个队列,并为队列中的每个项目调用您的 ffmpeg 程序。您没有提到有多少用户将调用您的页面,但最好以这种方式设计,以便您可以更简单地控制允许服务器一次处理的项目数。由于您是在服务上下文而不是 ASP.NET 下运行进程,因此您遇到的问题也会减少。
关于c# - 获取 ASP.NET 中两个等待已久的并发进程的正确进度更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11512235/