0

I am trying to make an exe program to change a video format by using FFMPEG instead of doing it in the terminal. The formatted video will be saved in the download folder. I have tried my code below and got no output response. I wonder if I used process() and StartInfo correctly, as examples I found and the documentation just confused me. I have double-checked the ffmpeg.exe is in the bin folder and the StartInfo() is just for getting information, which is under Process(). This is why Process() can access the information and use Start() to start the process. Please help and correct my understanding. Below is part of my code:

private void convertButton_Click(object sender, EventArgs e)
    {
        String input = filepathTextBox.Text;
        String outputResolution = resolutionLabel.Text;
        String output;
        String outputFileType;
        int inputLength = input.Length;
        int l = 0;
        for (int i = (inputLength - 1); inputLength > -1; i--)
        {
            if (input[i] == '.')
            {
                l = i;
                break;
            }
        }
        output = input.Substring(0, l - 1);
        outputFileType = input.Substring(l + 1, inputLength - 1);
        Process process = new Process();
        process.StartInfo.UseShellExecute = true;
        process.StartInfo.FileName = "ffmpeg.exe";
        process.StartInfo.WorkingDirectory = @"C:\Users\User\Downloads\ffmpeg-2023-05-15-git-2953ebe7b6-full_build\bin";
        process.StartInfo.Arguments = "ffmpeg -i" + @"C:\Users\User\Downloads\file_example_MP4_640_3MG.mp4" + "-s 320x240 -r 25 -b:v 500000 -pix_fmt yuv420p -c:v libx264     -vprofile baseline -level  2.1 -x264opts  stitchable=1:level=3.0:keyint=15:ref=1:merange=16:mvrange=32 -acodec pcm_s16le -ar 16000 -ac 1" + @"C:\Users\User\Downloads\440.mp4";
        process.Start();
    }

Output: "myprogram.exe(CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.15\System.Diagnostics.Process.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled." The thread 0x79e0 has exited with code 0 (0x0).

TSLee
  • 33
  • 6
  • Have you tested to see if the command would work in FFMPEG? I would try throwing that to the console for what's being made for the string and then manually test that first. – TechLoom May 17 '23 at 17:15
  • @TechLoom Yes, I did. The command worked and I have found the new formatted video. – TSLee May 17 '23 at 17:36
  • @TechLoom I have updated the command. It had a few mistakes due to auto-formatting. – TSLee May 17 '23 at 18:09
  • 1
    I still think you have a formatting issue with the string that's being made. You have `"ffmpeg -i" + @"C:\Users\User\Downloads\file_example_MP4_640_3MG.mp4"` That means it'll output ffmpeg -iC:\Users\User\Downloads\file_example_MP4_640_3MG.mp4 you have to include the spaces for when you are adding the strings together. – TechLoom May 17 '23 at 19:17
  • 1
    `Filename` should be the fully-qualified path to `ffmpeg.exe`. Very rarely is `UseShellExecute = true;` used, most likely one should set it to `UseShellExecute = false;` Why do you have `ffmpeg` as part of the arguments? There are some additional properties for [Process](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-7.0) that should be set. You may need to set [WorkingDirectory](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.workingdirectory?view=net-7.0), but should try to avoid hard coding the path. – Tu deschizi eu inchid May 17 '23 at 19:42
  • The following may be of interest: https://stackoverflow.com/a/68429794/10024425, and https://stackoverflow.com/a/71846819/10024425, [String interpolation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated), and [Verbatim text - @ in variables, attributes, and string literals](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim). – Tu deschizi eu inchid May 17 '23 at 19:45

1 Answers1

0

The Argument should be like

 $"-i {input} -s 320x240 -r 25 -b:v 500000 -pix_fmt yuv420p -c:v libx264 -vprofile baseline -level 2.1 -x264opts  stitchable=1:level=3.0:keyint=15:ref=1:merange=16:mvrange=32 -acodec pcm_s16le -ar 16000 -ac 1 {output}",

as the filename is assigned. Once the process is kicked off, the process will go to the following path and execute the exe file with the command, the argument.

For my code above, I executed ffmpeg inside a ffmpeg exe. That's why it was not working.

idchi
  • 761
  • 1
  • 5
  • 15
TSLee
  • 33
  • 6