ShareX/ShareX.ScreenCaptureLib/Forms/FFmpegOptionsForm.cs

527 lines
18 KiB
C#
Raw Normal View History

2014-05-10 12:23:47 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2018-01-02 03:59:14 +13:00
Copyright (c) 2007-2018 ShareX Team
2014-05-10 12:23:47 +12:00
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)
2016-02-15 20:29:43 +13:00
using ShareX.HelpersLib;
2016-08-28 04:51:20 +12:00
using ShareX.MediaLib;
2016-02-15 20:29:43 +13:00
using ShareX.ScreenCaptureLib.Properties;
2014-05-09 15:14:53 +12:00
using System;
using System.Diagnostics;
2014-05-09 15:14:53 +12:00
using System.Drawing;
using System.IO;
2016-12-03 05:35:34 +13:00
using System.Linq;
2018-08-03 23:01:12 +12:00
using System.Threading.Tasks;
2014-05-09 15:14:53 +12:00
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.ScreenCaptureLib
2014-05-09 15:14:53 +12:00
{
public partial class FFmpegOptionsForm : Form
2014-05-09 15:14:53 +12:00
{
2015-06-26 02:06:26 +12:00
public ScreencastOptions Options { get; private set; }
2018-01-29 01:29:39 +13:00
public string DefaultToolsFolder { get; set; }
2014-05-09 15:14:53 +12:00
2014-07-03 20:33:24 +12:00
private bool settingsLoaded;
2014-05-13 20:57:05 +12:00
public FFmpegOptionsForm(ScreencastOptions options)
2014-05-09 15:14:53 +12:00
{
InitializeComponent();
Icon = ShareXResources.Icon;
Options = options;
2015-06-26 02:06:26 +12:00
eiFFmpeg.ObjectType = typeof(FFmpegOptions);
cboVideoCodec.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegVideoCodec>());
cboAudioCodec.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegAudioCodec>());
cbx264Preset.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegPreset>());
cbGIFStatsMode.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegPaletteGenStatsMode>());
2016-12-03 05:35:34 +13:00
cbNVENCPreset.Items.AddRange(Helpers.GetEnums<FFmpegNVENCPreset>().Select(x => $"{x} ({x.GetDescription()})").ToArray());
cbGIFDither.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegPaletteUseDither>());
2014-07-03 20:33:24 +12:00
SettingsLoad();
2014-05-09 15:14:53 +12:00
}
private async Task SettingsLoad()
2014-05-09 15:14:53 +12:00
{
2014-07-03 20:33:24 +12:00
settingsLoaded = false;
2014-05-12 13:13:42 +12:00
// General
#if STEAM || WindowsStore
cbOverrideFFmpegPath.Checked = Options.FFmpeg.OverrideCLIPath;
gbFFmpegExe.Enabled = Options.FFmpeg.OverrideCLIPath;
#else
cbOverrideFFmpegPath.Visible = false;
#endif
2014-05-10 12:23:47 +12:00
txtFFmpegPath.Text = Options.FFmpeg.CLIPath;
2015-06-26 02:06:26 +12:00
txtFFmpegPath.SelectionStart = txtFFmpegPath.TextLength;
await RefreshSourcesAsync();
#if WindowsStore
btnInstallHelperDevices.Visible = false;
btnHelperDevicesHelp.Visible = false;
lblHelperDevices.Visible = false;
#endif
cboVideoCodec.SelectedIndex = (int)Options.FFmpeg.VideoCodec;
cboAudioCodec.SelectedIndex = (int)Options.FFmpeg.AudioCodec;
tbUserArgs.Text = Options.FFmpeg.UserArgs;
2014-05-12 13:13:42 +12:00
// x264
nudx264CRF.SetValue(Options.FFmpeg.x264_CRF);
cbx264Preset.SelectedIndex = (int)Options.FFmpeg.x264_Preset;
2014-05-12 13:13:42 +12:00
// VPx
nudVP8Bitrate.SetValue(Options.FFmpeg.VPx_bitrate);
2014-05-12 13:13:42 +12:00
// Xvid
nudXvidQscale.SetValue(Options.FFmpeg.XviD_qscale);
2016-12-03 05:35:34 +13:00
// NVENC
nudNVENCBitrate.SetValue(Options.FFmpeg.NVENC_bitrate);
cbNVENCPreset.SelectedIndex = (int)Options.FFmpeg.NVENC_preset;
// GIF
cbGIFStatsMode.SelectedIndex = (int)Options.FFmpeg.GIFStatsMode;
cbGIFDither.SelectedIndex = (int)Options.FFmpeg.GIFDither;
2014-05-15 18:13:49 +12:00
// AAC
tbAACBitrate.Value = Options.FFmpeg.AAC_bitrate / 32;
// Vorbis
tbVorbis_qscale.Value = Options.FFmpeg.Vorbis_qscale;
2014-05-15 18:13:49 +12:00
// MP3
2014-07-03 20:33:24 +12:00
tbMP3_qscale.Value = FFmpegHelper.libmp3lame_qscale_end - Options.FFmpeg.MP3_qscale;
#if WindowsStore
btnTest.Visible = false;
#endif
2014-06-04 11:18:41 +12:00
cbCustomCommands.Checked = Options.FFmpeg.UseCustomCommands;
if (Options.FFmpeg.UseCustomCommands)
{
txtCommandLinePreview.Text = Options.FFmpeg.CustomCommands;
}
2014-07-03 20:33:24 +12:00
settingsLoaded = true;
UpdateUI();
2014-05-09 15:14:53 +12:00
}
private async Task RefreshSourcesAsync(bool selectDevices = false)
2014-05-13 18:57:09 +12:00
{
btnRefreshSources.Enabled = false;
2014-05-13 18:57:09 +12:00
DirectShowDevices devices = null;
await Task.Run(() =>
2014-05-13 18:57:09 +12:00
{
using (FFmpegHelper ffmpeg = new FFmpegHelper(Options))
{
devices = ffmpeg.GetDirectShowDevices();
}
});
2015-06-26 02:06:26 +12:00
cboVideoSource.Items.Clear();
cboVideoSource.Items.Add(FFmpegHelper.SourceNone);
cboVideoSource.Items.Add(FFmpegHelper.SourceGDIGrab);
cboAudioSource.Items.Clear();
cboAudioSource.Items.Add(FFmpegHelper.SourceNone);
if (devices != null)
{
cboVideoSource.Items.AddRange(devices.VideoDevices.ToArray());
cboAudioSource.Items.AddRange(devices.AudioDevices.ToArray());
}
if (selectDevices && cboVideoSource.Items.Contains(FFmpegHelper.SourceVideoDevice))
{
Options.FFmpeg.VideoSource = FFmpegHelper.SourceVideoDevice;
}
cboVideoSource.Text = Options.FFmpeg.VideoSource;
if (selectDevices && cboAudioSource.Items.Contains(FFmpegHelper.SourceAudioDevice))
{
Options.FFmpeg.AudioSource = FFmpegHelper.SourceAudioDevice;
}
cboAudioSource.Text = Options.FFmpeg.AudioSource;
btnRefreshSources.Enabled = true;
2014-05-13 18:57:09 +12:00
}
2014-07-03 20:33:24 +12:00
private void UpdateUI()
2014-05-12 20:40:56 +12:00
{
2014-07-03 20:33:24 +12:00
if (settingsLoaded)
2014-05-12 11:19:38 +12:00
{
lblAACQuality.Text = string.Format(Resources.FFmpegOptionsForm_UpdateUI_Bitrate___0_k, Options.FFmpeg.AAC_bitrate);
lblVorbisQuality.Text = Resources.FFmpegOptionsForm_UpdateUI_Quality_ + " " + Options.FFmpeg.Vorbis_qscale;
lblMP3Quality.Text = Resources.FFmpegOptionsForm_UpdateUI_Quality_ + " " + Options.FFmpeg.MP3_qscale;
2015-10-21 11:18:28 +13:00
bool isValidAudioCodec = true;
FFmpegVideoCodec videoCodec = (FFmpegVideoCodec)cboVideoCodec.SelectedIndex;
if (videoCodec == FFmpegVideoCodec.libvpx)
{
FFmpegAudioCodec audioCodec = (FFmpegAudioCodec)cboAudioCodec.SelectedIndex;
if (audioCodec != FFmpegAudioCodec.libvorbis)
{
isValidAudioCodec = false;
}
}
2015-10-29 20:03:16 +13:00
pbAudioCodecWarning.Visible = !isValidAudioCodec;
pbx264PresetWarning.Visible = (FFmpegPreset)cbx264Preset.SelectedIndex > FFmpegPreset.fast;
2015-10-21 11:18:28 +13:00
2014-07-03 20:33:24 +12:00
if (!Options.FFmpeg.UseCustomCommands)
{
txtCommandLinePreview.Text = Options.GetFFmpegArgs();
}
2014-05-14 23:18:18 +12:00
2016-02-13 11:51:56 +13:00
UpdateFFmpegPathUI();
}
}
2016-02-13 11:51:56 +13:00
private void UpdateFFmpegPathUI()
{
2015-09-16 11:37:42 +12:00
#if !STEAM
2016-01-08 07:30:35 +13:00
Color backColor = Color.FromArgb(255, 200, 200);
try
{
if (File.Exists(Options.FFmpeg.FFmpegPath))
{
backColor = Color.FromArgb(200, 255, 200);
}
}
2018-05-16 20:09:01 +12:00
catch
{
}
2016-01-08 07:30:35 +13:00
txtFFmpegPath.BackColor = backColor;
2015-09-16 11:37:42 +12:00
#endif
}
2016-02-13 11:51:56 +13:00
private void cbOverrideFFmpegPath_CheckedChanged(object sender, EventArgs e)
{
#if STEAM || WindowsStore
2016-02-13 11:51:56 +13:00
Options.FFmpeg.OverrideCLIPath = cbOverrideFFmpegPath.Checked;
gbFFmpegExe.Enabled = Options.FFmpeg.OverrideCLIPath;
#endif
}
private void txtFFmpegPath_TextChanged(object sender, EventArgs e)
{
Options.FFmpeg.CLIPath = txtFFmpegPath.Text;
UpdateFFmpegPathUI();
}
private async void buttonFFmpegBrowse_Click(object sender, EventArgs e)
{
2016-02-13 11:51:56 +13:00
if (Helpers.BrowseFile(Resources.FFmpegOptionsForm_buttonFFmpegBrowse_Click_Browse_for_ffmpeg_exe, txtFFmpegPath, Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), true))
{
await RefreshSourcesAsync();
}
}
private async void btnRefreshSources_Click(object sender, EventArgs e)
{
await RefreshSourcesAsync();
}
private void cboVideoSource_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.VideoSource = cboVideoSource.Text;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private void cboAudioSource_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.AudioSource = cboAudioSource.Text;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private async void btnInstallHelperDevices_Click(object sender, EventArgs e)
{
2015-06-26 02:06:26 +12:00
string filepath = Helpers.GetAbsolutePath(FFmpegHelper.DeviceSetupPath);
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
{
bool result = false;
await Task.Run(() =>
{
try
{
Process process = Process.Start(filepath);
result = process.WaitForExit(1000 * 60 * 5) && process.ExitCode == 0;
}
catch { }
});
if (result)
{
await RefreshSourcesAsync(true);
}
}
2015-07-04 13:33:13 +12:00
else
{
MessageBox.Show("File not exists: \"" + filepath + "\"", "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnHelperDevicesHelp_Click(object sender, EventArgs e)
{
URLHelpers.OpenURL("https://github.com/rdp/screen-capture-recorder-to-video-windows-free");
}
private void cboVideoCodec_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.VideoCodec = (FFmpegVideoCodec)cboVideoCodec.SelectedIndex;
2014-07-03 20:33:24 +12:00
if (cboVideoCodec.SelectedIndex >= 0)
{
switch (Options.FFmpeg.VideoCodec)
{
default:
case FFmpegVideoCodec.libx264:
case FFmpegVideoCodec.libx265:
tcFFmpegVideoCodecs.SelectedIndex = 0;
break;
case FFmpegVideoCodec.libvpx:
tcFFmpegVideoCodecs.SelectedIndex = 1;
break;
case FFmpegVideoCodec.libxvid:
tcFFmpegVideoCodecs.SelectedIndex = 2;
break;
2016-12-03 05:35:34 +13:00
case FFmpegVideoCodec.h264_nvenc:
case FFmpegVideoCodec.hevc_nvenc:
2015-06-09 10:55:45 +12:00
tcFFmpegVideoCodecs.SelectedIndex = 3;
break;
2016-12-03 05:35:34 +13:00
case FFmpegVideoCodec.gif:
tcFFmpegVideoCodecs.SelectedIndex = 4;
break;
}
2014-07-03 20:33:24 +12:00
}
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private void cboAudioCodec_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.AudioCodec = (FFmpegAudioCodec)cboAudioCodec.SelectedIndex;
2014-07-03 20:33:24 +12:00
if (cboAudioCodec.SelectedIndex >= 0)
{
switch (Options.FFmpeg.AudioCodec)
{
default:
case FFmpegAudioCodec.libvoaacenc:
tcFFmpegAudioCodecs.SelectedIndex = 0;
break;
case FFmpegAudioCodec.libvorbis:
tcFFmpegAudioCodecs.SelectedIndex = 1;
break;
case FFmpegAudioCodec.libmp3lame:
tcFFmpegAudioCodecs.SelectedIndex = 2;
break;
}
2014-07-03 20:33:24 +12:00
}
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private void nudx264CRF_ValueChanged(object sender, EventArgs e)
{
Options.FFmpeg.x264_CRF = (int)nudx264CRF.Value;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private void cbPreset_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.x264_Preset = (FFmpegPreset)cbx264Preset.SelectedIndex;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private void nudVP8Bitrate_ValueChanged(object sender, EventArgs e)
{
Options.FFmpeg.VPx_bitrate = (int)nudVP8Bitrate.Value;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private void nudQscale_ValueChanged(object sender, EventArgs e)
{
Options.FFmpeg.XviD_qscale = (int)nudXvidQscale.Value;
UpdateUI();
}
2016-12-03 05:35:34 +13:00
private void cbNVENCPreset_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.NVENC_preset = (FFmpegNVENCPreset)cbNVENCPreset.SelectedIndex;
UpdateUI();
}
private void nudNVENCBitrate_ValueChanged(object sender, EventArgs e)
{
Options.FFmpeg.NVENC_bitrate = (int)nudNVENCBitrate.Value;
UpdateUI();
}
private void cbGIFStatsMode_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.GIFStatsMode = (FFmpegPaletteGenStatsMode)cbGIFStatsMode.SelectedIndex;
UpdateUI();
}
private void cbGIFDither_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.GIFDither = (FFmpegPaletteUseDither)cbGIFDither.SelectedIndex;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
2014-07-03 20:33:24 +12:00
private void tbAACBitrate_ValueChanged(object sender, EventArgs e)
2014-05-15 18:13:49 +12:00
{
Options.FFmpeg.AAC_bitrate = tbAACBitrate.Value * 32;
UpdateUI();
}
2014-07-03 20:33:24 +12:00
private void tbVorbis_qscale_ValueChanged(object sender, EventArgs e)
2014-05-15 18:13:49 +12:00
{
Options.FFmpeg.Vorbis_qscale = tbVorbis_qscale.Value;
UpdateUI();
}
2014-07-03 20:33:24 +12:00
private void tbMP3_qscale_ValueChanged(object sender, EventArgs e)
2014-05-15 18:13:49 +12:00
{
2014-07-03 20:33:24 +12:00
Options.FFmpeg.MP3_qscale = FFmpegHelper.libmp3lame_qscale_end - tbMP3_qscale.Value;
2014-05-15 18:13:49 +12:00
UpdateUI();
}
private void tbUserArgs_TextChanged(object sender, EventArgs e)
{
Options.FFmpeg.UserArgs = tbUserArgs.Text;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
2014-05-10 12:23:47 +12:00
private void btnDownload_Click(object sender, EventArgs e)
{
2014-12-09 05:15:18 +13:00
FFmpegDownloader.DownloadFFmpeg(true, DownloaderForm_InstallRequested);
}
private void DownloaderForm_InstallRequested(string filePath)
2014-05-10 12:23:47 +12:00
{
2018-01-29 01:29:39 +13:00
bool result = FFmpegDownloader.ExtractFFmpeg(filePath, DefaultToolsFolder);
2014-05-10 12:23:47 +12:00
if (result)
{
this.InvokeSafe(async () =>
2014-05-13 18:57:09 +12:00
{
2018-01-29 01:29:39 +13:00
txtFFmpegPath.Text = Helpers.GetVariableFolderPath(Path.Combine(DefaultToolsFolder, "ffmpeg.exe"));
await RefreshSourcesAsync();
2014-05-15 18:13:49 +12:00
UpdateUI();
2014-05-13 18:57:09 +12:00
});
2014-06-05 02:08:07 +12:00
MessageBox.Show(Resources.FFmpegOptionsForm_DownloaderForm_InstallRequested_Successfully_downloaded_FFmpeg_, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
2014-05-10 12:23:47 +12:00
}
else
{
MessageBox.Show(Resources.FFmpegOptionsForm_DownloaderForm_InstallRequested_Download_of_FFmpeg_failed_, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
2014-05-10 12:23:47 +12:00
}
}
private void btnTest_Click(object sender, EventArgs e)
{
if (File.Exists(Options.FFmpeg.FFmpegPath))
{
try
{
using (Process process = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
2015-09-16 08:11:43 +12:00
psi.Arguments = $"/k {Path.GetFileName(Options.FFmpeg.FFmpegPath)} {Options.GetFFmpegCommands()}";
psi.WorkingDirectory = Path.GetDirectoryName(Options.FFmpeg.FFmpegPath);
process.StartInfo = psi;
process.Start();
}
}
catch (Exception ex)
{
DebugHelper.WriteException(ex);
}
}
}
private void btnCopyPreview_Click(object sender, EventArgs e)
{
2015-09-16 08:11:43 +12:00
ClipboardHelpers.CopyText($"{Path.GetFileName(Options.FFmpeg.FFmpegPath)} {Options.GetFFmpegCommands()}");
2014-05-13 18:57:09 +12:00
}
2014-06-04 11:18:41 +12:00
private void cbCustomCommands_CheckedChanged(object sender, EventArgs e)
{
Options.FFmpeg.UseCustomCommands = cbCustomCommands.Checked;
txtCommandLinePreview.ReadOnly = !Options.FFmpeg.UseCustomCommands;
2014-07-19 22:14:53 +12:00
if (settingsLoaded)
{
2014-07-19 22:14:53 +12:00
if (Options.FFmpeg.UseCustomCommands)
{
txtCommandLinePreview.Text = Options.GetFFmpegArgs(true);
}
else
{
txtCommandLinePreview.Text = Options.GetFFmpegArgs();
}
}
2014-06-04 11:18:41 +12:00
}
private void txtCommandLinePreview_TextChanged(object sender, EventArgs e)
{
Options.FFmpeg.CustomCommands = txtCommandLinePreview.Text;
2014-06-04 11:18:41 +12:00
}
private object eiFFmpeg_ExportRequested()
{
return Options.FFmpeg;
}
private void eiFFmpeg_ImportRequested(object obj)
{
FFmpegOptions ffmpegOptions = obj as FFmpegOptions;
if (ffmpegOptions != null)
{
string tempFFmpegPath = Options.FFmpeg.CLIPath;
Options.FFmpeg = ffmpegOptions;
Options.FFmpeg.CLIPath = tempFFmpegPath;
SettingsLoad();
}
}
2014-05-09 15:14:53 +12:00
}
}