Apache2 / Mod_Mono环境中的应用程序在后台运行C#/ ASP.Net

时间:2019-01-15 19:45:08

标签: c# asp.net linux .net-4.5 mod-mono

我正在创建一个Web应用程序,允许用户上传视频。当用户提交表单时,页面会将上载的文件保存到一个文件夹,然后在后台调用ffmpeg将上载的.mp4文件转换为.webm。问题是,ffmpeg无法启动。我应该提到,这是在使用Apache2和Mod_Mono的Ubuntu 18.10服务器上运行ASP.Net/C#代码的测试。

我试图四处寻找解决方法,发现使用BackgroundWorkers可能使我可以做我想做的事情。

但是,当我按照该文章所述尝试使用BackgroundWorkers时,似乎只是在一瞬间运行ffmpeg进程来转换视频文件。 .webm文件的大小为0 kb。

protected void btnModalEditor_Click(object sender, EventArgs e)
{
    string baseDir = "/var/webfolder/"; //This line is actually obtained through an ini file on the site.
    string filePath = "/var/webfolder/savedvideos/"; //Folder the video is saved to.
    fileName = Path.GetFileNameWithoutExtension(editVideoUp.FileName);
    fileExt = Path.GetExtension(editVideoUp.FileName);
    editVideoUp.SaveAs(filePath + fileName + fileExt);
    dbAccess.CreateDBEntry(fileName); //Creates an entry in my database for the video.
    string vidID = dbAccess.LastInsertID().ToString(); //gets the id of the entry created in the mySql database.
    //Create the BackgroundWorker to call the process.
    BackgroundWorker vidConvProcess = new BackgroundWorker();
    vidConvProcess.DoWork += new DoWorkEventHandler(ConvertVideo);
    vidConvProcess.WorkerReportsProgress = false;
    vidConvProcess.WorkerSupportsCancellation = false;
    vidConvProcess.RunWorkerAsync(argument: filePath + "," + fileName + "," + fileExt + "," + baseDir + "," + vidID);
}

private static void ConvertVideo(object sender, DoWorkEventArgs e)
{
    string vidFile = e.Argument.ToString();
    string[] fPart = vidFile.Split(',');
    string convID = fPart[4];
    string convLogName = fPart[3] + "ConversionLogs/convers-" + convID + ".txt";
    // Long running background operation. should be able to run and forget about.
    Process process = new Process();
    process.StartInfo.FileName = "ffmpeg";
    process.StartInfo.Arguments = "-y -i \"" + fPart[0] + fPart[1] + fPart[2] + "\" -r 29.97 -vf scale=-1:720 -crf 17 -preset medium -max_muxing_queue_size 2000 \"" + fPart[0] + fPart[1] + ".webm\" -async 1 -vsync passthrough -frame_drop_threshold 20 2> \"" + convLogName + "\" &";
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.CreateNoWindow = true;
    process.Start();
    process.WaitForExit();
}

基本上,我想发生的事情是,当用户提交表单并上传视频时,是将其保存到“ / var / webfolder / savedvideos /”,然后为ffmpeg运行后台进程进行转换用户将.mp4上传到.webm文件。我不需要进行任何设置即可停止该过程或检查其进度,只需在后台启动它,而无需担心它,而允许用户在网站上执行其他操作即可。

UPDATE (我很抱歉,如果应该将其作为单独的答案/问题来做,我不确定)

我实际上已经发现它没有运行的原因是由于我基本上试图在参数中指定StandardOutput,这显然是C#/ ASP.NET不喜欢的。因此,通过从process.StartInfo.Arguments中删除'2> \“” + convLogName +“ \”&“',该过程现在只要运行UseShellExecute = true就可以运行。

要解决缺少输出日志作为确定ffmpeg是否已完成运行的方法,我添加了process.WaitForExit()。但是,如果我这样做,则BackgroundWorker会在等待ffmpeg完成时导致页面挂起,这是我不希望的。有什么方法可以很好地,真正地允许此过程在后台运行,而无需在执行该操作时冻结GUI?

0 个答案:

没有答案