Added encode button functionality

This commit is contained in:
Jaex 2019-11-17 04:03:54 +03:00
parent 7bf8d2a740
commit 1e3c20b582
7 changed files with 94 additions and 47 deletions

View file

@ -35,8 +35,9 @@ public abstract class ExternalCLIManager : IDisposable
public event DataReceivedEventHandler OutputDataReceived;
public event DataReceivedEventHandler ErrorDataReceived;
public bool IsProcessRunning { get; private set; }
protected Process process;
protected bool processRunning;
public virtual int Open(string path, string args = null)
{
@ -71,12 +72,12 @@ public virtual int Open(string path, string args = null)
try
{
processRunning = true;
IsProcessRunning = true;
process.WaitForExit();
}
finally
{
processRunning = false;
IsProcessRunning = false;
}
return process.ExitCode;
@ -110,7 +111,7 @@ private void cli_ErrorDataReceived(object sender, DataReceivedEventArgs e)
public void WriteInput(string input)
{
if (processRunning && process != null && process.StartInfo != null && process.StartInfo.RedirectStandardInput)
if (IsProcessRunning && process != null && process.StartInfo != null && process.StartInfo.RedirectStandardInput)
{
process.StandardInput.WriteLine(input);
}
@ -118,7 +119,7 @@ public void WriteInput(string input)
public virtual void Close()
{
if (processRunning && process != null)
if (IsProcessRunning && process != null)
{
process.CloseMainWindow();
}

View file

@ -37,6 +37,7 @@ public class FFmpegCLIManager : ExternalCLIManager
{
public string FFmpegPath { get; private set; }
public StringBuilder Output { get; private set; }
public bool ShowError { get; set; }
public FFmpegCLIManager(string ffmpegPath)
{
@ -58,42 +59,61 @@ private void FFmpeg_DataReceived(object sender, DataReceivedEventArgs e)
}
}
public bool Run(string args)
{
return Run(FFmpegPath, args);
}
private bool Run(string path, string args)
{
int errorCode = Open(path, args);
bool result = errorCode == 0;
if (!result && ShowError)
{
// TODO: Translate
new OutputBox(Output.ToString(), "FFmpeg error").ShowDialog();
}
return result;
}
public VideoInfo GetVideoInfo(string videoPath)
{
VideoInfo videoInfo = new VideoInfo();
videoInfo.FilePath = videoPath;
Open(FFmpegPath, string.Format("-i \"{0}\"", videoPath));
string output = Output.ToString();
Match matchInput = Regex.Match(output, @"Duration: (?<Duration>\d{2}:\d{2}:\d{2}\.\d{2}),.+?start: (?<Start>\d+\.\d+),.+?bitrate: (?<Bitrate>\d+) kb/s", RegexOptions.CultureInvariant);
if (matchInput.Success)
if (Run($"-i \"{videoPath}\""))
{
videoInfo.Duration = TimeSpan.Parse(matchInput.Groups["Duration"].Value);
//videoInfo.Start = TimeSpan.Parse(match.Groups["Start"].Value);
videoInfo.Bitrate = int.Parse(matchInput.Groups["Bitrate"].Value);
}
else
{
return null;
}
string output = Output.ToString();
Match matchVideoStream = Regex.Match(output, @"Stream #\d+:\d+(?:\(.+?\))?: Video: (?<Codec>.+?) \(.+?,.+?, (?<Width>\d+)x(?<Height>\d+).+?, (?<FPS>\d+(?:\.\d+)?) fps",
RegexOptions.CultureInvariant);
Match matchInput = Regex.Match(output, @"Duration: (?<Duration>\d{2}:\d{2}:\d{2}\.\d{2}),.+?start: (?<Start>\d+\.\d+),.+?bitrate: (?<Bitrate>\d+) kb/s", RegexOptions.CultureInvariant);
if (matchVideoStream.Success)
{
videoInfo.VideoCodec = matchVideoStream.Groups["Codec"].Value;
videoInfo.VideoResolution = new Size(int.Parse(matchVideoStream.Groups["Width"].Value), int.Parse(matchVideoStream.Groups["Height"].Value));
videoInfo.VideoFPS = double.Parse(matchVideoStream.Groups["FPS"].Value, CultureInfo.InvariantCulture);
}
if (matchInput.Success)
{
videoInfo.Duration = TimeSpan.Parse(matchInput.Groups["Duration"].Value);
//videoInfo.Start = TimeSpan.Parse(match.Groups["Start"].Value);
videoInfo.Bitrate = int.Parse(matchInput.Groups["Bitrate"].Value);
}
else
{
return null;
}
Match matchAudioStream = Regex.Match(output, @"Stream #\d+:\d+(?:\(.+?\))?: Audio: (?<Codec>.+?)(?: \(|,)", RegexOptions.CultureInvariant);
Match matchVideoStream = Regex.Match(output, @"Stream #\d+:\d+(?:\(.+?\))?: Video: (?<Codec>.+?) \(.+?,.+?, (?<Width>\d+)x(?<Height>\d+).+?, (?<FPS>\d+(?:\.\d+)?) fps",
RegexOptions.CultureInvariant);
if (matchAudioStream.Success)
{
videoInfo.AudioCodec = matchAudioStream.Groups["Codec"].Value;
if (matchVideoStream.Success)
{
videoInfo.VideoCodec = matchVideoStream.Groups["Codec"].Value;
videoInfo.VideoResolution = new Size(int.Parse(matchVideoStream.Groups["Width"].Value), int.Parse(matchVideoStream.Groups["Height"].Value));
videoInfo.VideoFPS = double.Parse(matchVideoStream.Groups["FPS"].Value, CultureInfo.InvariantCulture);
}
Match matchAudioStream = Regex.Match(output, @"Stream #\d+:\d+(?:\(.+?\))?: Audio: (?<Codec>.+?)(?: \(|,)", RegexOptions.CultureInvariant);
if (matchAudioStream.Success)
{
videoInfo.AudioCodec = matchAudioStream.Groups["Codec"].Value;
}
}
return videoInfo;

View file

@ -158,7 +158,7 @@ private void InitializeComponent()
//
this.btnEncode.Location = new System.Drawing.Point(16, 264);
this.btnEncode.Name = "btnEncode";
this.btnEncode.Size = new System.Drawing.Size(424, 23);
this.btnEncode.Size = new System.Drawing.Size(424, 24);
this.btnEncode.TabIndex = 14;
this.btnEncode.Text = "Start encoding";
this.btnEncode.UseVisualStyleBackColor = true;

View file

@ -25,13 +25,7 @@ You should have received a copy of the GNU General Public License
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -39,12 +33,14 @@ namespace ShareX.MediaLib
{
public partial class VideoConverterForm : Form
{
public string FFmpegFilePath { get; private set; }
public VideoConverterOptions Options { get; private set; }
private bool ready;
public VideoConverterForm(VideoConverterOptions options)
public VideoConverterForm(string ffmpegFilePath, VideoConverterOptions options)
{
FFmpegFilePath = ffmpegFilePath;
Options = options;
InitializeComponent();
@ -73,6 +69,35 @@ private void UpdateOptions()
}
}
private bool StartEncoding()
{
bool result = false;
if (!string.IsNullOrEmpty(Options.InputFilePath) && File.Exists(Options.InputFilePath) && !string.IsNullOrEmpty(Options.OutputFilePath))
{
using (FFmpegCLIManager manager = new FFmpegCLIManager(FFmpegFilePath))
{
manager.ShowError = true;
string outputFilePath = Options.OutputFilePath;
string args = Options.GetFFmpegArgs();
result = manager.Run(args);
if (result)
{
Helpers.OpenFolderWithFile(outputFilePath);
}
}
}
return result;
}
private async Task<bool> StartEncodingAsync()
{
return await Task.Run(StartEncoding);
}
private void txtInputFilePath_TextChanged(object sender, EventArgs e)
{
UpdateOptions();
@ -120,9 +145,13 @@ private void nudVideoQuality_ValueChanged(object sender, EventArgs e)
UpdateOptions();
}
private void btnEncode_Click(object sender, EventArgs e)
private async void btnEncode_Click(object sender, EventArgs e)
{
btnEncode.Enabled = false;
await StartEncodingAsync();
btnEncode.Enabled = true;
}
}
}

View file

@ -23,12 +23,8 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShareX.MediaLib
{

View file

@ -170,7 +170,7 @@ public DirectShowDevices GetDirectShowDevices()
public override void Close()
{
if (processRunning)
if (IsProcessRunning)
{
if (closeTryCount >= 2)
{

View file

@ -854,7 +854,8 @@ public static void OpenVideoConverter(TaskSettings taskSettings = null)
return;
}
VideoConverterForm videoConverterForm = new VideoConverterForm(new VideoConverterOptions());
VideoConverterOptions options = new VideoConverterOptions();
VideoConverterForm videoConverterForm = new VideoConverterForm(taskSettings.CaptureSettings.FFmpegOptions.FFmpegPath, options);
videoConverterForm.Show();
}