1

I happened to find this answer: https://stackoverflow.com/questions/44981454/c-sharp-extract-mp3-file-from-mp4-file#=

And it seems to work, However, the conversion process gets stuck, and it only resumes after I have closed the application that called ffmpeg to run.

The following code is responsible for the launch of ffmpeg: (The commented lines are just from testing on how to fix the hanging)

        public static string ConvertToMp3(string inputFile, string outputFile = "extracted_audio.mp3")
    {
        Console.WriteLine("Converting mp3 to " + Path.GetFullPath("extracted_audio.mp3"));
        string mp3out = "";

        try
        {
            if (ffmpegProcess != null)
                ffmpegProcess.Kill();

            ffmpegProcess = new Process();
            ffmpegProcess.StartInfo.UseShellExecute = false;
            ffmpegProcess.StartInfo.RedirectStandardInput = true;
            ffmpegProcess.StartInfo.RedirectStandardOutput = true;
            ffmpegProcess.StartInfo.RedirectStandardError = true;
            ffmpegProcess.StartInfo.CreateNoWindow = true;
            ffmpegProcess.StartInfo.FileName = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) + @"\bin\ffmpeg.exe";
            ffmpegProcess.StartInfo.Arguments = "-i " + Path.GetFullPath(inputFile) + " -vn -f mp3 -ab 320k output " + Path.GetFullPath(outputFile);
            ffmpegProcess.Start();
            ffmpegProcess.StandardOutput.ReadToEnd();
            mp3out = ffmpegProcess.StandardError.ReadToEnd();
            //ffmpegProcess.WaitForExit();
            if (!ffmpegProcess.HasExited)
            {
                //ffmpegProcess.Kill();
            }
        }
        catch (InvalidOperationException e)
        {
            Console.WriteLine("mp3 conversion failed: " + e.Message);
            return "";
        }

        return mp3out;
    }

I have no idea what causes the halting, any ideas?

0 Answers0