如何使ffmpeg对工作目录和参数的try/catch抛出异常?



如果我将FileName(outputDirectory)设置为null,它将抛出异常。

类当我使用ffmpeg.exe的进程:

public void Start()
{
process = new Process();
process.StartInfo.FileName = outputDirectory; // Change the directory where ffmpeg.exe is.  
process.EnableRaisingEvents = false;
process.StartInfo.WorkingDirectory = workingDirectory; // The output directory  
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true; //Redirect stdin
process.StartInfo.CreateNoWindow = true;
process.Start();
errorMessage = false;
startRecord = true;
}

但如果我将工作目录设置为null或输入一些无效参数,如"sadasdasd"它不会在工作目录和参数上抛出异常。

In form:

private void recordStripMenuItem_Click(object sender, EventArgs e)
{
recordToggle = !recordToggle;
if (recordToggle)
{
try
{
record.workingDirectory = settingsForm.workingDirectory;
record.outputDirectory = settingsForm.outputDirectory;
record.arguments = settingsForm.arguments;
record.Start();
}
catch(Exception ex)
{
recordStripMenuItem.Text = "Record";
Icon = iconGreen;
TextInfo("Waiting");
recordToggle = false;
MessageBox.Show("Arguments are not valid : " + ex.Message);

}
if (!FFmpeg_Capture.errorMessage)
{
settingsForm.Close();
recordStripMenuItem.Text = "Stop";
Icon = iconRed;
TextInfo("Recording");
}
}
else
{
recordStripMenuItem.Text = "Record";
Icon = iconGreen;
TextInfo("Waiting");
record.Stop();
}
}

变量记录是带有ffmpeg进程的FFmpeg_Capture类的实例。它只在FileName(outputDirectory)上抛出异常。

在start()方法中添加以下代码,如果工作目录不存在则抛出异常:

public void Start(){
if(!System.IO.Directory.Exists(this.workingDirectory){
throw new Exception("workingDirectory doesn't exist");
}

//.. the rest of the code

}

最新更新