C#/ASP.Net running process in background within application within Apache2/Mod_Mono environment

187 views Asked by At

I am creating a web application that allows users to upload a video. when the user submits the form, the page saves the uploaded file to a folder, and then call ffmpeg in the background to convert the uploaded .mp4 file to .webm. The issue is, ffmpeg is not starting. I should mention that this is being tested on an Ubuntu 18.10 server using Apache2 and Mod_Mono to run the ASP.Net/C# code.

I have attempted to look around for some resolutions to this, and found that using BackgroundWorkers might've allowed me to do what I wanted to do.

However, when I tried to use BackgroundWorkers as described in that article, it appears to only be running the ffmpeg process for a split second to convert the video file. the .webm file gets created with a file size of 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();
}

essentially, what I would like to have happen is when the user submits the form and uploads the video, is to have it saved to "/var/webfolder/savedvideos/", and then run a background process for ffmpeg that converts the user uploaded .mp4 to a .webm file. I don't need to have anything setup to stop the process or check its progress, just start it in the background and forget about it while allowing the user to do other things on the site.

UPDATE (My apologies if this should be done as a separate answer/question, I was not sure)

I have actually figured out that the reason it wasn't running was due to the fact that, I was basically attempting to specify the StandardOutput in the arguments, which apparently C#/ASP.NET don't like. Therefore, by removing '2> \"" + convLogName + "\" &"' from the process.StartInfo.Arguments, the process now runs, as long as UseShellExecute = true.

To work around the lack of output logs as a means of determining if ffmpeg is finished running, I added process.WaitForExit(). If I do this however, then the BackgroundWorker causes the page to hang while it waits for ffmpeg to finish, which i don't want. Is there any way to well and truly allow this process to run in the background without freezing the GUI while its doing that?

0

There are 0 answers