2019-11-17 07:18:58 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
|
|
|
|
Copyright (c) 2007-2019 ShareX Team
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using ShareX.HelpersLib;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2019-11-17 08:34:32 +13:00
|
|
|
|
namespace ShareX.MediaLib
|
2019-11-17 07:18:58 +13:00
|
|
|
|
{
|
|
|
|
|
public partial class VideoConverterForm : Form
|
|
|
|
|
{
|
2019-11-17 14:03:54 +13:00
|
|
|
|
public string FFmpegFilePath { get; private set; }
|
2019-11-17 10:04:02 +13:00
|
|
|
|
public VideoConverterOptions Options { get; private set; }
|
|
|
|
|
|
|
|
|
|
private bool ready;
|
|
|
|
|
|
2019-11-17 14:03:54 +13:00
|
|
|
|
public VideoConverterForm(string ffmpegFilePath, VideoConverterOptions options)
|
2019-11-17 07:18:58 +13:00
|
|
|
|
{
|
2019-11-17 14:03:54 +13:00
|
|
|
|
FFmpegFilePath = ffmpegFilePath;
|
2019-11-17 10:04:02 +13:00
|
|
|
|
Options = options;
|
|
|
|
|
|
2019-11-17 07:18:58 +13:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
ShareXResources.ApplyTheme(this);
|
2019-11-17 08:34:32 +13:00
|
|
|
|
|
2019-11-20 02:52:34 +13:00
|
|
|
|
UpdateOptions();
|
|
|
|
|
|
2019-11-22 21:26:08 +13:00
|
|
|
|
txtInputFilePath.Text = Options.InputFilePath;
|
|
|
|
|
txtOutputFolder.Text = Options.OutputFolderPath;
|
|
|
|
|
txtOutputFileName.Text = Options.OutputFileName;
|
2019-11-17 08:34:32 +13:00
|
|
|
|
cbVideoCodec.Items.AddRange(Helpers.GetEnumDescriptions<ConverterVideoCodecs>());
|
2019-11-17 10:04:02 +13:00
|
|
|
|
cbVideoCodec.SelectedIndex = (int)Options.VideoCodec;
|
2019-11-22 21:26:08 +13:00
|
|
|
|
tbVideoQuality.SetValue(tbVideoQuality.Minimum + tbVideoQuality.Maximum - Options.VideoQuality);
|
2019-11-17 10:04:02 +13:00
|
|
|
|
|
|
|
|
|
ready = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateOptions()
|
|
|
|
|
{
|
|
|
|
|
if (ready)
|
|
|
|
|
{
|
|
|
|
|
Options.InputFilePath = txtInputFilePath.Text;
|
|
|
|
|
Options.OutputFolderPath = txtOutputFolder.Text;
|
|
|
|
|
Options.OutputFileName = txtOutputFileName.Text;
|
|
|
|
|
Options.VideoCodec = (ConverterVideoCodecs)cbVideoCodec.SelectedIndex;
|
2019-11-20 02:52:34 +13:00
|
|
|
|
}
|
2019-11-17 10:04:02 +13:00
|
|
|
|
|
2019-11-20 02:52:34 +13:00
|
|
|
|
switch (Options.VideoCodec)
|
|
|
|
|
{
|
|
|
|
|
case ConverterVideoCodecs.x264:
|
|
|
|
|
case ConverterVideoCodecs.x265:
|
|
|
|
|
case ConverterVideoCodecs.vp8:
|
|
|
|
|
case ConverterVideoCodecs.vp9:
|
|
|
|
|
case ConverterVideoCodecs.xvid:
|
|
|
|
|
tbVideoQuality.Enabled = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
tbVideoQuality.Enabled = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-11-20 01:53:19 +13:00
|
|
|
|
|
2019-11-20 02:52:34 +13:00
|
|
|
|
switch (Options.VideoCodec)
|
|
|
|
|
{
|
|
|
|
|
case ConverterVideoCodecs.x264:
|
|
|
|
|
case ConverterVideoCodecs.x265:
|
|
|
|
|
tbVideoQuality.Minimum = 0;
|
|
|
|
|
tbVideoQuality.Maximum = 51;
|
|
|
|
|
break;
|
|
|
|
|
case ConverterVideoCodecs.vp8:
|
|
|
|
|
case ConverterVideoCodecs.vp9:
|
|
|
|
|
tbVideoQuality.Minimum = 0;
|
|
|
|
|
tbVideoQuality.Maximum = 63;
|
|
|
|
|
break;
|
|
|
|
|
case ConverterVideoCodecs.xvid:
|
|
|
|
|
tbVideoQuality.Minimum = 1;
|
|
|
|
|
tbVideoQuality.Maximum = 31;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-11-18 13:00:24 +13:00
|
|
|
|
|
2019-11-21 01:53:14 +13:00
|
|
|
|
if (ready)
|
|
|
|
|
{
|
|
|
|
|
Options.VideoQuality = tbVideoQuality.Minimum + tbVideoQuality.Maximum - tbVideoQuality.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lblVideoQualityValue.Text = Options.VideoQuality.ToString();
|
2019-11-18 13:00:24 +13:00
|
|
|
|
|
2019-11-20 02:52:34 +13:00
|
|
|
|
txtArguments.Text = Options.GetFFmpegArgs();
|
|
|
|
|
|
|
|
|
|
btnEncode.Enabled = !string.IsNullOrEmpty(Options.InputFilePath) && !string.IsNullOrEmpty(Options.OutputFolderPath) &&
|
|
|
|
|
!string.IsNullOrEmpty(Options.OutputFileName);
|
2019-11-17 10:04:02 +13:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 14:03:54 +13:00
|
|
|
|
private bool StartEncoding()
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
2019-11-18 13:00:24 +13:00
|
|
|
|
if (!string.IsNullOrEmpty(Options.InputFilePath) && File.Exists(Options.InputFilePath) && !string.IsNullOrEmpty(Options.OutputFolderPath) &&
|
|
|
|
|
!string.IsNullOrEmpty(Options.OutputFileName))
|
2019-11-17 14:03:54 +13:00
|
|
|
|
{
|
|
|
|
|
using (FFmpegCLIManager manager = new FFmpegCLIManager(FFmpegFilePath))
|
|
|
|
|
{
|
|
|
|
|
manager.ShowError = true;
|
2019-11-23 03:14:24 +13:00
|
|
|
|
manager.TrackEncodeProgress = true;
|
|
|
|
|
manager.EncodeProgressChanged += Manager_EncodeProgressChanged;
|
2019-11-17 14:03:54 +13:00
|
|
|
|
|
|
|
|
|
string outputFilePath = Options.OutputFilePath;
|
|
|
|
|
string args = Options.GetFFmpegArgs();
|
|
|
|
|
result = manager.Run(args);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
Helpers.OpenFolderWithFile(outputFilePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 03:14:24 +13:00
|
|
|
|
private void Manager_EncodeProgressChanged(float percentage)
|
|
|
|
|
{
|
|
|
|
|
this.InvokeSafe(() => pbProgress.Value = (int)percentage);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 14:02:51 +13:00
|
|
|
|
private Task<bool> StartEncodingAsync()
|
2019-11-17 14:03:54 +13:00
|
|
|
|
{
|
2019-11-19 14:02:51 +13:00
|
|
|
|
return Task.Run(StartEncoding);
|
2019-11-17 14:03:54 +13:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 10:04:02 +13:00
|
|
|
|
private void txtInputFilePath_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdateOptions();
|
2019-11-17 07:18:58 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnInputFilePathBrowse_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
using (OpenFileDialog ofd = new OpenFileDialog())
|
|
|
|
|
{
|
|
|
|
|
if (ofd.ShowDialog(this) == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
string filePath = ofd.FileName;
|
|
|
|
|
txtInputFilePath.Text = filePath;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(txtOutputFolder.Text))
|
|
|
|
|
{
|
|
|
|
|
txtOutputFolder.Text = Path.GetDirectoryName(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(txtOutputFileName.Text))
|
|
|
|
|
{
|
|
|
|
|
txtOutputFileName.Text = Path.GetFileNameWithoutExtension(filePath) + "-output";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 10:04:02 +13:00
|
|
|
|
private void txtOutputFolder_TextChanged(object sender, EventArgs e)
|
2019-11-17 07:18:58 +13:00
|
|
|
|
{
|
2019-11-17 10:04:02 +13:00
|
|
|
|
UpdateOptions();
|
|
|
|
|
}
|
2019-11-17 07:18:58 +13:00
|
|
|
|
|
2019-11-22 21:26:08 +13:00
|
|
|
|
private void btnOutputFolderBrowse_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Helpers.BrowseFolder(txtOutputFolder);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 10:04:02 +13:00
|
|
|
|
private void txtOutputFileName_TextChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdateOptions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cbVideoCodec_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
UpdateOptions();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 02:52:34 +13:00
|
|
|
|
private void tbVideoQuality_ValueChanged(object sender, EventArgs e)
|
2019-11-17 10:04:02 +13:00
|
|
|
|
{
|
|
|
|
|
UpdateOptions();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 14:03:54 +13:00
|
|
|
|
private async void btnEncode_Click(object sender, EventArgs e)
|
2019-11-17 10:04:02 +13:00
|
|
|
|
{
|
2019-11-17 14:19:10 +13:00
|
|
|
|
UpdateOptions();
|
|
|
|
|
|
2019-11-23 03:14:24 +13:00
|
|
|
|
pbProgress.Value = 0;
|
2019-11-17 14:19:10 +13:00
|
|
|
|
pbProgress.Visible = true;
|
|
|
|
|
btnEncode.Visible = false;
|
2019-11-17 14:03:54 +13:00
|
|
|
|
|
|
|
|
|
await StartEncodingAsync();
|
|
|
|
|
|
2019-11-17 14:19:10 +13:00
|
|
|
|
btnEncode.Visible = true;
|
|
|
|
|
pbProgress.Visible = false;
|
2019-11-17 07:18:58 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|