Example #1
0
        /// <summary>
        /// Call ffmpeg with provided arguments and update the progress dialog.
        /// No windows will popup. If Cancel is pressed in the progress dialog, the process will be killed.
        /// </summary>
        static public void startFFmpegProgress(string ffmpegAudioProgArgs, DialogProgress dialogProgress)
        {
            Process ffmpegProcess   = new Process();
            bool    tryAgain        = true;
            bool    useShellExecute = false;
            bool    createNoWindow  = true;

            // Try several different ways of calling ffmpeg because of the dreaded
            // "System.ComponentModel.Win32Exception: The system cannot find the drive specified" exception

            // Try relative path from subs2srs.exe
            try
            {
                ffmpegProcess.StartInfo.FileName              = ConstantSettings.PathFFmpegExe;
                ffmpegProcess.StartInfo.Arguments             = ffmpegAudioProgArgs;
                ffmpegProcess.StartInfo.UseShellExecute       = useShellExecute;
                ffmpegProcess.StartInfo.CreateNoWindow        = createNoWindow;
                ffmpegProcess.StartInfo.RedirectStandardError = true;
                ffmpegProcess.ErrorDataReceived += new DataReceivedEventHandler(DialogProgress.getFFmpegOutputHandler(dialogProgress));
                ffmpegProcess.Start();
                ffmpegProcess.BeginErrorReadLine();

                // Loop until process has exited
                while (!ffmpegProcess.HasExited)
                {
                    Thread.Sleep(100);

                    // If the Cancel button was pressed
                    if (DialogProgress.getCancelInvoke(dialogProgress))
                    {
                        try
                        {
                            ffmpegProcess.Kill();
                        }
                        catch
                        {
                            // Don't care
                        }

                        break;
                    }
                }

                tryAgain = false;
            }
            catch
            {
                try
                {
                    ffmpegProcess.Kill();
                }
                catch
                {
                    // Dont care
                }

                tryAgain = true;
            }

            // Try absolute path to ffmpeg.exe
            if (tryAgain)
            {
                ffmpegProcess = new Process();

                try
                {
                    ffmpegProcess.StartInfo.FileName              = ConstantSettings.PathFFmpegFullExe;
                    ffmpegProcess.StartInfo.Arguments             = ffmpegAudioProgArgs;
                    ffmpegProcess.StartInfo.UseShellExecute       = useShellExecute;
                    ffmpegProcess.StartInfo.CreateNoWindow        = createNoWindow;
                    ffmpegProcess.StartInfo.RedirectStandardError = true;
                    ffmpegProcess.ErrorDataReceived += new DataReceivedEventHandler(DialogProgress.getFFmpegOutputHandler(dialogProgress));
                    ffmpegProcess.Start();
                    ffmpegProcess.BeginErrorReadLine();

                    // Loop until process has exited
                    while (!ffmpegProcess.HasExited)
                    {
                        Thread.Sleep(100);

                        // If the Cancel button was pressed
                        if (DialogProgress.getCancelInvoke(dialogProgress))
                        {
                            try
                            {
                                ffmpegProcess.Kill();
                            }
                            catch
                            {
                                // Don't care
                            }

                            break;
                        }
                    }

                    tryAgain = false;
                }
                catch (Exception)
                {
                    try
                    {
                        ffmpegProcess.Kill();
                    }
                    catch
                    {
                        // Dont care
                    }

                    tryAgain = true;
                }
            }

            // Try setting PATH to include the absolute path of ffmpeg.exe
            if (tryAgain)
            {
                try
                {
                    string oldPath   = Environment.GetEnvironmentVariable("Path");
                    string ffmpegDir = Path.GetDirectoryName(ConstantSettings.PathFFmpegFullExe);

                    if (!oldPath.Contains(ffmpegDir))
                    {
                        string newPath = oldPath + ";" + ffmpegDir;
                        Environment.SetEnvironmentVariable("Path", newPath);
                    }

                    ffmpegProcess = new Process();

                    ffmpegProcess.StartInfo.FileName              = ConstantSettings.ExeFFmpeg;
                    ffmpegProcess.StartInfo.Arguments             = ffmpegAudioProgArgs;
                    ffmpegProcess.StartInfo.UseShellExecute       = useShellExecute;
                    ffmpegProcess.StartInfo.CreateNoWindow        = createNoWindow;
                    ffmpegProcess.StartInfo.RedirectStandardError = true;
                    ffmpegProcess.ErrorDataReceived += new DataReceivedEventHandler(DialogProgress.getFFmpegOutputHandler(dialogProgress));
                    ffmpegProcess.Start();
                    ffmpegProcess.BeginErrorReadLine();

                    // Loop until process has exited
                    while (!ffmpegProcess.HasExited)
                    {
                        Thread.Sleep(100);

                        // If the Cancel button was pressed
                        if (DialogProgress.getCancelInvoke(dialogProgress))
                        {
                            try
                            {
                                ffmpegProcess.Kill();
                            }
                            catch
                            {
                                // Don't care
                            }

                            break;
                        }
                    }
                }
                catch
                {
                    // Don't care
                }
            }
        }