Hello,
I am attempting to use hangfire on one of my sites to handle some video processing, but I cannot seem to get it to fully complete my task. Here is an example of my code:
// steps leading to this point ensure that the video file has been uploaded
BackgroundJob.Enqueue(() => FileHelper.ProcessVideo(ffmpegPath, videoID, originalVideoServerPath, videoServerPath, thumbServerPath));
Here is an example of my synchronous ProcessVideo method (using ffmpeg.exe):
/// <summary>
/// Converts the uploaded video into .mp4 and generates a thumbnail preview.
/// </summary>
/// <param name="ffmpegPath">The server path of ffmpeg.exe</param>
/// <param name="videoID">ID of the video record in database</param>
/// <param name="originalVideoPath">Path of the uploaded video on the server</param>
/// <param name="newVideoPath">Path of the newly converted video after it has been processed</param>
/// <param name="thumbnailPath">Path of the thumbnail preview after it has been processed</param>
public static void ProcessVideo(string ffmpegPath, int videoID, string originalVideoPath, string newVideoPath, string thumbnailPath)
{
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo(ffmpegPath);
var videos = new Videos();
var video = videos.Get(videoID);
if (!video.IsProcessed)
{
// convert the video to a lower quality .mp4
startInfo.Arguments = string.Format("-i \"{0}\" -c:v libx264 -crf 30 -preset medium -c:a copy \"{1}\"",
originalVideoPath, newVideoPath);
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
// generate a thumbnail preview of the video
startInfo.Arguments = string.Format("-i \"{0}\" -f image2 \"{1}\"", originalVideoPath, thumbnailPath);
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
// update the video to show that processing has completed
video.IsProcessed = true;
videos.Update(video);
// delete the original video
Delete(originalVideoPath);
}
}
In this example, the compressed mp4 video will be converted, but the thumbnail is never generated. Also, the originally uploaded video is never deleted.
Do I need to do something else to ensure that the entire ProcessVideo method is completed before the process times out? I assume that is what happens during the conversion of the video, because if I re-arrange the thumbnail generation to come before the video compression, both are completed. However, the original file is still not deleted, and my database call to update video.IsProcessed is not reached either.
I would appreciate any suggestions on this. Thank you,
Chris
p.s. I should also mention that the example I have been using only takes a minute or so of actual processing time on the server.