Support stopping video encoding

This commit is contained in:
Jaex 2019-11-25 14:10:47 +03:00
parent e0d00c320e
commit 9eb37efa1f
4 changed files with 66 additions and 27 deletions

View file

@ -51,6 +51,7 @@ public class FFmpegCLIManager : ExternalCLIManager
public StringBuilder Output { get; private set; }
public bool IsEncoding { get; set; }
public bool ShowError { get; set; }
public bool StopRequested { get; set; }
public bool TrackEncodeProgress { get; set; }
public TimeSpan VideoDuration { get; set; }
public TimeSpan EncodeTime { get; set; }
@ -73,6 +74,7 @@ public bool Run(string args)
protected bool Run(string path, string args)
{
StopRequested = false;
int errorCode = Open(path, args);
IsEncoding = false;
bool result = errorCode == 0;
@ -89,6 +91,8 @@ protected bool Run(string path, string args)
public override void Close()
{
StopRequested = true;
if (IsProcessRunning && process != null)
{
if (closeTryCount >= 2)

View file

@ -295,6 +295,7 @@ private void InitializeComponent()
this.Name = "VideoConverterForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ShareX - Video converter";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.VideoConverterForm_FormClosing);
((System.ComponentModel.ISupportInitialize)(this.tbVideoQuality)).EndInit();
this.tcMain.ResumeLayout(false);
this.tpOptions.ResumeLayout(false);

View file

@ -36,7 +36,8 @@ public partial class VideoConverterForm : Form
public string FFmpegFilePath { get; private set; }
public VideoConverterOptions Options { get; private set; }
private bool ready;
private bool formClosing, formReady, encoding;
private FFmpegCLIManager ffmpeg;
public VideoConverterForm(string ffmpegFilePath, VideoConverterOptions options)
{
@ -55,12 +56,12 @@ public VideoConverterForm(string ffmpegFilePath, VideoConverterOptions options)
cbVideoCodec.SelectedIndex = (int)Options.VideoCodec;
tbVideoQuality.SetValue(tbVideoQuality.Minimum + tbVideoQuality.Maximum - Options.VideoQuality);
ready = true;
formReady = true;
}
private void UpdateOptions()
{
if (ready)
if (formReady)
{
Options.InputFilePath = txtInputFilePath.Text;
Options.OutputFolderPath = txtOutputFolder.Text;
@ -100,7 +101,7 @@ private void UpdateOptions()
break;
}
if (ready)
if (formReady)
{
Options.VideoQuality = tbVideoQuality.Minimum + tbVideoQuality.Maximum - tbVideoQuality.Value;
}
@ -120,7 +121,7 @@ private bool StartEncoding()
if (File.Exists(FFmpegFilePath) && !string.IsNullOrEmpty(Options.InputFilePath) && File.Exists(Options.InputFilePath) &&
!string.IsNullOrEmpty(Options.OutputFolderPath) && !string.IsNullOrEmpty(Options.OutputFileName))
{
using (FFmpegCLIManager ffmpeg = new FFmpegCLIManager(FFmpegFilePath))
using (ffmpeg = new FFmpegCLIManager(FFmpegFilePath))
{
ffmpeg.ShowError = true;
ffmpeg.TrackEncodeProgress = true;
@ -130,7 +131,7 @@ private bool StartEncoding()
string args = Options.GetFFmpegArgs();
result = ffmpeg.Run(args);
if (result)
if (result && !ffmpeg.StopRequested)
{
Helpers.OpenFolderWithFile(outputFilePath);
}
@ -140,16 +141,19 @@ private bool StartEncoding()
return result;
}
private void Manager_EncodeProgressChanged(float percentage)
{
this.InvokeSafe(() => pbProgress.Value = (int)percentage);
}
private Task<bool> StartEncodingAsync()
{
return Task.Run(StartEncoding);
}
private void Manager_EncodeProgressChanged(float percentage)
{
if (!formClosing)
{
this.InvokeSafe(() => pbProgress.Value = (int)percentage);
}
}
private void txtInputFilePath_TextChanged(object sender, EventArgs e)
{
UpdateOptions();
@ -204,19 +208,49 @@ private void tbVideoQuality_ValueChanged(object sender, EventArgs e)
private async void btnEncode_Click(object sender, EventArgs e)
{
UpdateOptions();
pbProgress.Value = 0;
btnEncode.Enabled = false;
bool result = await StartEncodingAsync();
if (result)
if (!encoding)
{
pbProgress.Value = 100;
}
encoding = true;
btnEncode.Enabled = true;
// TODO: Translate
btnEncode.Text = "Stop encoding";
UpdateOptions();
pbProgress.Value = 0;
bool result = await StartEncodingAsync();
if (!formClosing)
{
if (result && ffmpeg != null && !ffmpeg.StopRequested)
{
pbProgress.Value = 100;
}
else
{
pbProgress.Value = 0;
}
// TODO: Translate
btnEncode.Text = "Start encoding";
}
encoding = false;
}
else if (ffmpeg != null)
{
ffmpeg.Close();
}
}
private void VideoConverterForm_FormClosing(object sender, FormClosingEventArgs e)
{
formClosing = true;
if (ffmpeg != null)
{
ffmpeg.Close();
}
}
}
}

View file

@ -100,7 +100,7 @@ private set
private Rectangle captureRectangle;
private ImageCache imgCache;
private FFmpegCLIManager ffmpeg;
private bool stopRequest;
private bool stopRequested;
public ScreenRecorder(ScreenRecordOutput outputType, ScreencastOptions options, Screenshot screenshot, Rectangle captureRectangle)
{
@ -144,7 +144,7 @@ public void StartRecording()
if (!IsRecording)
{
IsRecording = true;
stopRequest = false;
stopRequested = false;
if (OutputType == ScreenRecordOutput.FFmpeg)
{
@ -164,7 +164,7 @@ private void RecordUsingCache()
{
try
{
for (int i = 0; !stopRequest && (frameCount == 0 || i < frameCount); i++)
for (int i = 0; !stopRequested && (frameCount == 0 || i < frameCount); i++)
{
Stopwatch timer = Stopwatch.StartNew();
@ -173,7 +173,7 @@ private void RecordUsingCache()
imgCache.AddImageAsync(img);
if (!stopRequest && (frameCount == 0 || i + 1 < frameCount))
if (!stopRequested && (frameCount == 0 || i + 1 < frameCount))
{
int sleepTime = delay - (int)timer.ElapsedMilliseconds;
@ -196,7 +196,7 @@ private void RecordUsingCache()
public void StopRecording()
{
stopRequest = true;
stopRequested = true;
if (ffmpeg != null)
{