Skip to content

Commit

Permalink
Capture errors from FFMPEG and show them in the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-zhao committed Jul 1, 2024
1 parent 7228b8b commit 07d3a97
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
39 changes: 35 additions & 4 deletions LibSidWiz/Outputs/FfmpegOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ namespace LibSidWiz.Outputs
{
public class FfmpegOutput : IGraphicsOutput
{
private readonly bool _throwStandardError;
private readonly Process _process;
private readonly BinaryWriter _writer;

public FfmpegOutput(string pathToExe, string filename, int width, int height, int fps, string extraArgs, string masterAudioFilename, string videoCodec, string audioCodec)
public FfmpegOutput(string pathToExe, string filename, int width, int height, int fps, string extraArgs, string masterAudioFilename, string videoCodec, string audioCodec, bool throwStandardError)
{
_throwStandardError = throwStandardError;
// Build the FFMPEG commandline
var arguments = "-y -hide_banner"; // Overwrite, don't show banner at startup

Expand Down Expand Up @@ -55,9 +57,9 @@ public FfmpegOutput(string pathToExe, string filename, int width, int height, in
Arguments = arguments,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = false,
RedirectStandardError = throwStandardError,
RedirectStandardOutput = false,
CreateNoWindow = false // makes it inline in console mode
CreateNoWindow = throwStandardError // makes it inline in console mode
}
);

Expand All @@ -71,13 +73,42 @@ public FfmpegOutput(string pathToExe, string filename, int width, int height, in

public void Write(SKImage _, byte[] data, double __, TimeSpan ___)
{
_writer.Write(data);
try
{
_writer.Write(data);
}
catch (Exception e)
{
if (_throwStandardError)
{
if (_process.HasExited)
{
throw new Exception(
$"""
FFMPEG has exited unexpectedly.
Exit code was {_process.ExitCode}.
Standard error was:
{_process.StandardError.ReadToEnd()}
""", e);
}

throw new Exception(
$"""
Cannot write to FFMPEG.
Standard error was:
{_process.StandardError.ReadToEnd()}
""", e);
}

throw;
}
}

public void Dispose()
{
// This triggers a shutdown
_process?.StandardInput.BaseStream.Close();
_process?.StandardError.Close();
// And we wait for it to finish...
_process?.WaitForExit();
_process?.Dispose();
Expand Down
15 changes: 13 additions & 2 deletions LibSidWiz/Outputs/PreviewOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,19 @@ public void Write(SKImage image, byte[] data, double fractionComplete, TimeSpan
public void Dispose()
{
_stopwatch.Stop();
_form?.Close();
_form?.Dispose();
try
{
_form?.BeginInvoke(() =>
{
_form?.Close();
_form?.Dispose();
}
);
}
catch (Exception)
{
// We might get this if exiting the program
}
}
}
}
20 changes: 14 additions & 6 deletions SidWiz/SidWizPlusGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,17 @@ private void RenderButton_Click(object sender, EventArgs e)

if (_settings.MasterAudio.IsAutomatic)
{
var filename = outputFilename + ".wav";
Mixer.MixToFile(_settings.Channels, filename, MasterMixReplayGain.Checked);
MasterAudioPath.Text = filename;
_settings.MasterAudio.Path = filename;
try
{
var filename = outputFilename + ".wav";
Mixer.MixToFile(_settings.Channels, filename, MasterMixReplayGain.Checked);
MasterAudioPath.Text = filename;
_settings.MasterAudio.Path = filename;
}
catch (Exception ex)
{
MessageBox.Show(this, $"Failed to mix audio: {ex.Message}");
}
}
else
{
Expand All @@ -817,7 +824,8 @@ private void RenderButton_Click(object sender, EventArgs e)
_programSettings.FfmpegExtraParameters,
_settings.MasterAudio.Path,
_settings.EncodeVideo.VideoCodec,
_settings.EncodeVideo.AudioCodec));
_settings.EncodeVideo.AudioCodec,
true));
}
}

Expand All @@ -833,7 +841,7 @@ private void RenderButton_Click(object sender, EventArgs e)
}
catch (Exception exception)
{
BeginInvoke(new Action(() => MessageBox.Show(exception.Message)));
BeginInvoke(new Action(() => MessageBox.Show(this, exception.Message)));
}
finally
{
Expand Down
9 changes: 5 additions & 4 deletions SidWizPlus/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@ private class Settings
public string FfMpegPath { get; set; }

// ReSharper disable once StringLiteralTypo
[Option("vcodec", HelpText = "Video codec for FFMPEG", Default = "h264")]
[Option("vcodec", Required = false, HelpText = "Video codec for FFMPEG", Default = "h264")]
public string VideoCodec { get; set; }

// ReSharper disable once StringLiteralTypo
[Option("acodec", HelpText = "Audio codec for FFMPEG", Default = "aac")]
[Option("acodec", Required = false, HelpText = "Audio codec for FFMPEG", Default = "aac")]
public string AudioCodec { get; set; }

// ReSharper disable once StringLiteralTypo
[Option("ffmpegoptions", Required = false, HelpText = "Extra commandline options for FFMPEG, e.g. to set the output format. Surround value with quotes and start with a space, e.g. \" -acodec flac\", to avoid conflicts with other parameters.", Default = "")]
[Option("ffmpegoptions", Required = false,
HelpText = "Extra commandline options for FFMPEG, e.g. to set the output format. Surround value with quotes and start with a space, e.g. \" -crf 20\", to avoid conflicts with other parameters.", Default = "")]
public string FfMpegExtraOptions { get; set; }

// ReSharper disable once StringLiteralTypo
Expand Down Expand Up @@ -784,7 +785,7 @@ private static void Render(Settings settings, List<Channel> channels)
// Try to find it
settings.FfMpegPath = FindExecutable("ffmpeg.exe");
}
outputs.Add(new FfmpegOutput(settings.FfMpegPath, settings.OutputFile, settings.Width, settings.Height, settings.FramesPerSecond, settings.FfMpegExtraOptions, settings.MasterAudioFile, settings.VideoCodec, settings.AudioCodec));
outputs.Add(new FfmpegOutput(settings.FfMpegPath, settings.OutputFile, settings.Width, settings.Height, settings.FramesPerSecond, settings.FfMpegExtraOptions, settings.MasterAudioFile, settings.VideoCodec, settings.AudioCodec, false));
}

if (settings.PreviewFrameskip > 0)
Expand Down

0 comments on commit 07d3a97

Please sign in to comment.