Skip to content

Commit

Permalink
Fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaily committed Apr 22, 2018
1 parent 8e47748 commit 1204e4a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
24 changes: 19 additions & 5 deletions MovieBarCodeGenerator/FfmpegWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ public FfmpegWrapper(string ffmpegExecutablePath)
FfmpegExecutablePath = ffmpegExecutablePath;
}

private Process StartFfmpegInstance(string args)
private Process StartFfmpegInstance(string args, bool redirectError = false)
{
try
{
var process = Process.Start(new ProcessStartInfo
{
FileName = FfmpegExecutablePath,
Arguments = args,
// Warning: if a standard stread is redirected but is not read,
// its buffer might fill up and block the whole process.
// Only redirect a standard stream if you read it!
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardError = redirectError,
UseShellExecute = false,
CreateNoWindow = true,
});
Expand All @@ -45,12 +48,14 @@ public TimeSpan GetMediaDuration(string inputPath, CancellationToken cancellatio
{
var args = $"-i \"{inputPath}\"";

var process = StartFfmpegInstance(args);
var process = StartFfmpegInstance(args, redirectError: true);

using (cancellationToken.Register(() => process.Kill()))
{
var output = process.StandardError.ReadToEnd();

cancellationToken.ThrowIfCancellationRequested();

var match = Regex.Match(output, @"Duration: (.*?),");
if (match.Success)
{
Expand All @@ -75,10 +80,19 @@ public IEnumerable<Bitmap> GetImagesFromMedia(string inputPath, int frameCount,

var process = StartFfmpegInstance(args);

using (cancellationToken.Register(() => process.Kill()))
IEnumerable<Bitmap> GetLazyStream()
{
return ReadBitmapStream(process.StandardOutput.BaseStream);
using (cancellationToken.Register(() => process.Kill()))
{
foreach (var item in ReadBitmapStream(process.StandardOutput.BaseStream))
{
yield return item;
}
cancellationToken.ThrowIfCancellationRequested();
}
}

return GetLazyStream();
}

private IEnumerable<Bitmap> ReadBitmapStream(Stream stdout)
Expand Down
1 change: 1 addition & 0 deletions MovieBarCodeGenerator/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private async void generateButton_Click(object sender, EventArgs e)
_cancellationTokenSource.Cancel();
_cancellationTokenSource = null;
generateButton.Text = GenerateButtonText;
progressBar1.Value = progressBar1.Minimum;
return;
}

Expand Down

0 comments on commit 1204e4a

Please sign in to comment.