Azure Batch with C# Application using System.Diagnostics.Process

Dan Hickman 21 Reputation points
2022-08-11T14:13:58.677+00:00

I am using Azure Batch with a C# application. The overall C# application is responsible for doing many things. Unfortunately, 1 operation that works on my local machine does not work within an Azure Batch application.

The operation that works locally but not as a Azure C# application is programmatically starting a System.Diagnostics.Process that executes a FFmpeg argument to take a screenshot of a video. The process seems to run, but, the jpg file is not created. Locally, the code will create a jpg but as an Azure Batch app, the file is not created.

I have set the image file to be created in the AZ_BATCH_TASK_WORKING_DIR folder. I have reduced the code to just do the 1 operation that does not work as a Azure application and here it is:

using System;  
using System.Diagnostics;  
using System.IO;  
  
namespace BatchAppWithFfmpegProcess  
{  
    internal class Program  
    {  
        static void Main(string[] args)  
        {  
  
#if DEBUG  
            string workingDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);  
#else  
           /*  
                Within each Tasks directory, the Batch service creates a working directory (wd) whose unique path is   
                specified by the AZ_BATCH_TASK_WORKING_DIR environment variable. This directory provides read/write access   
                to the task. The task can create, read, update, and delete files under this directory.   
                This directory is retained based on the RetentionTime constraint that is specified for the task.  
                The stdout.txt and stderr.txt files are written to the Tasks folder during the execution of the task.  
             */  
            string workingDirectory = Environment.GetEnvironmentVariable("AZ_BATCH_TASK_WORKING_DIR");  
#endif  
            var applicationPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);  
            string ffmpegPath = Path.Combine(applicationPath, @"lib\");  
  
            string videoFilePath = Path.Combine(applicationPath, "MichaelJacksonSmoothCriminal_Trimmed.mp4");  
            string thumbnailFilePath = Path.Combine(workingDirectory, "MichaelJacksonSmoothCriminal_Trimmed.jpg");  
  
            if (File.Exists(thumbnailFilePath))  
            {  
                File.Delete(thumbnailFilePath);  
            }  
  
            string arguments =  $"-i \"{videoFilePath}\" -ss 00:00:01.000 -frames:v 1 \"{thumbnailFilePath}\"";  
  
            var startInfo = new ProcessStartInfo  
            {  
                FileName = ffmpegPath + $"\\ffmpeg.exe",  
                Arguments = arguments,  
                RedirectStandardError = false,  
                RedirectStandardOutput = true,  
                CreateNoWindow = true,  
                UseShellExecute = false,  
                WorkingDirectory = ffmpegPath  
            };  
  
            using (var process = new Process { StartInfo = startInfo })  
            {  
  
                process.Start();  
                process.WaitForExit();  
            }  
            if (File.Exists(thumbnailFilePath))  
            {  
                Console.WriteLine("Hurray, it worked!!!");  
            }  
            else  
            {  
                Console.WriteLine("File was not created.");  
            }  
        }  
    }  
}  

Perhaps it is impossible to use System.Diagnostics.Process to create files? I have tried to make this as easy as possible to reproduce with the following:

clone the code at:
https://github.com/Dapp3rDanH/AzBatchFfmpegProcess.git

Using Visual Studio 2022, "Publish" BatchAppWithFfmpegProcess code to a Folder using
"Deployment mode" = Self-Contained
Target Framework of net5.0
Target runtime = win-x64.

Create a "BatchAppWithFfmpegProcess.zip" zip file of the publish folder. Make sure the BatchAppWithFfmpegProcess.exe is in the root of the zip.

Create a Batch account.

Add a Batch application using the BatchAppWithFfmpegProcess.zip file with appId of BatchAppWithFfmpegProcess and a version of 1.

Add a Pool called "Pool1" with 1 dedicated node using microsoftwindowsserver windowsserver 2022-datacenter-core (latest). Add BatchAppWithFfmpegProcess version 1 to Pool1.

Create a new Job with a Task with the command of:
cmd /c %AZ_BATCH_APP_PACKAGE_BatchAppWithFfmpegProcess#1%\BatchAppWithFfmpegProcess.exe

If you check out the stdout.txt file of the task, you will see "File was not created". Any way to get this to work?

Thanks!
Dan

Azure Batch
Azure Batch
An Azure service that provides cloud-scale job scheduling and compute management.
296 questions
{count} votes