ShareX/ShareX.ScreenCaptureLib/Screencast/FFmpegOptionsForm.cs

414 lines
14 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
2014-12-31 22:41:32 +13:00
Copyright © 2007-2015 ShareX Developers
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)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
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;
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
{
2014-05-10 20:06:43 +12:00
public partial class FFmpegOptionsForm : Form
2014-05-09 15:14:53 +12:00
{
public ScreencastOptions Options = new ScreencastOptions();
public string DefaultToolsPath;
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;
eiFFmpeg.ObjectType = typeof(FFmpegOptions);
cboVideoCodec.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegVideoCodec>());
cboAudioCodec.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegAudioCodec>());
cbPreset.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegPreset>());
2014-07-03 20:33:24 +12:00
SettingsLoad();
2014-05-09 15:14:53 +12:00
}
2014-05-12 13:13:42 +12:00
private void 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
RefreshSourcesAsync();
cboVideoCodec.SelectedIndex = (int)Options.FFmpeg.VideoCodec;
cboAudioCodec.SelectedIndex = (int)Options.FFmpeg.AudioCodec;
2014-05-09 20:12:44 +12:00
2014-05-10 12:23:47 +12:00
string cli = "ffmpeg.exe";
if (string.IsNullOrEmpty(Options.FFmpeg.CLIPath) && File.Exists(cli))
2014-05-10 12:23:47 +12:00
{
Options.FFmpeg.CLIPath = cli;
2014-05-10 12:23:47 +12:00
}
txtFFmpegPath.Text = Options.FFmpeg.CLIPath;
tbUserArgs.Text = Options.FFmpeg.UserArgs;
2014-05-12 13:13:42 +12:00
// x264
nudx264CRF.Value = Options.FFmpeg.x264_CRF.Between((int)nudx264CRF.Minimum, (int)nudx264CRF.Maximum);
2015-06-01 22:57:37 +12:00
cbPreset.SelectedIndex = (int)Options.FFmpeg.x264_Preset;
2014-05-12 13:13:42 +12:00
// VPx
nudVP8Bitrate.Value = Options.FFmpeg.VPx_bitrate.Between((int)nudVP8Bitrate.Minimum, (int)nudVP8Bitrate.Maximum);
2014-05-12 13:13:42 +12:00
// Xvid
nudQscale.Value = Options.FFmpeg.XviD_qscale.Between((int)nudQscale.Minimum, (int)nudQscale.Maximum);
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;
txtExtension.Text = Options.FFmpeg.Extension;
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 void RefreshSourcesAsync()
2014-05-13 18:57:09 +12:00
{
btnRefreshSources.Enabled = false;
2014-05-13 18:57:09 +12:00
DirectShowDevices devices = null;
2014-05-24 03:39:14 +12:00
TaskEx.Run(() =>
2014-05-13 18:57:09 +12:00
{
using (FFmpegHelper ffmpeg = new FFmpegHelper(Options))
{
devices = ffmpeg.GetDirectShowDevices();
}
},
() =>
{
cboVideoSource.Items.Clear();
cboVideoSource.Items.Add(FFmpegHelper.SourceNone);
cboVideoSource.Items.Add(FFmpegHelper.SourceGDIGrab);
2014-05-13 18:57:09 +12:00
cboAudioSource.Items.Clear();
cboAudioSource.Items.Add(FFmpegHelper.SourceNone);
2014-05-13 18:57:09 +12:00
if (devices != null)
{
cboVideoSource.Items.AddRange(devices.VideoDevices.ToArray());
cboAudioSource.Items.AddRange(devices.AudioDevices.ToArray());
}
cboVideoSource.Text = Options.FFmpeg.VideoSource;
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;
2014-07-03 20:33:24 +12:00
if (!Options.FFmpeg.UseCustomCommands)
{
txtCommandLinePreview.Text = Options.GetFFmpegArgs();
}
2014-05-15 18:13:49 +12:00
}
2014-05-14 23:18:18 +12:00
}
private void btnRefreshSources_Click(object sender, EventArgs e)
{
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 void cboVideoCodec_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.VideoCodec = (FFmpegVideoCodec)cboVideoCodec.SelectedIndex;
2014-07-03 20:33:24 +12:00
if (settingsLoaded)
{
switch (Options.FFmpeg.VideoCodec)
{
case FFmpegVideoCodec.libx264:
2015-06-02 00:05:46 +12:00
case FFmpegVideoCodec.libx265:
2014-07-03 20:33:24 +12:00
txtExtension.Text = "mp4";
break;
case FFmpegVideoCodec.libvpx:
txtExtension.Text = "webm";
break;
case FFmpegVideoCodec.libxvid:
txtExtension.Text = "avi";
break;
2015-06-05 19:48:02 +12:00
case FFmpegVideoCodec.gif:
txtExtension.Text = "gif";
break;
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;
}
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();
}
2014-07-03 20:33:24 +12:00
private void txtExtension_TextChanged(object sender, EventArgs e)
{
2014-07-03 20:33:24 +12:00
Options.FFmpeg.Extension = txtExtension.Text;
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)
{
2015-06-01 22:57:37 +12:00
Options.FFmpeg.x264_Preset = (FFmpegPreset)cbPreset.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)nudQscale.Value;
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 tbFFmpegPath_TextChanged(object sender, EventArgs e)
{
Options.FFmpeg.CLIPath = txtFFmpegPath.Text;
2014-05-14 23:18:18 +12:00
txtFFmpegPath.BackColor = File.Exists(txtFFmpegPath.Text) ? Color.FromArgb(200, 255, 200) : Color.FromArgb(255, 200, 200);
}
2014-05-09 20:12:44 +12:00
private void buttonFFmpegBrowse_Click(object sender, EventArgs e)
{
if (Helpers.BrowseFile(Resources.FFmpegOptionsForm_buttonFFmpegBrowse_Click_Browse_for_ffmpeg_exe, txtFFmpegPath, Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)))
2014-05-13 18:57:09 +12:00
{
RefreshSourcesAsync();
2014-05-13 18:57:09 +12:00
}
}
private void tbUserArgs_TextChanged(object sender, EventArgs e)
{
Options.FFmpeg.UserArgs = tbUserArgs.Text;
2014-06-04 11:18:41 +12:00
UpdateUI();
}
private void buttonFFmpegHelp_Click(object sender, EventArgs e)
{
2014-07-03 20:33:24 +12:00
URLHelpers.OpenURL("https://github.com/ShareX/ShareX/wiki/FFmpeg-options#additional-commands");
2014-05-09 20:12:44 +12:00
}
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
{
2014-05-12 11:19:38 +12:00
string extractPath = DefaultToolsPath ?? "ffmpeg.exe";
2014-12-09 05:15:18 +13:00
bool result = FFmpegDownloader.ExtractFFmpeg(filePath, extractPath);
2014-05-10 12:23:47 +12:00
if (result)
{
2014-05-13 18:57:09 +12:00
this.InvokeSafe(() =>
{
txtFFmpegPath.Text = extractPath;
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.CLIPath))
{
try
{
using (Process process = new Process())
{
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe");
2014-06-04 11:18:41 +12:00
psi.Arguments = "/k ffmpeg " + Options.GetFFmpegCommands();
psi.WorkingDirectory = Path.GetDirectoryName(Options.FFmpeg.CLIPath);
process.StartInfo = psi;
process.Start();
}
}
catch (Exception ex)
{
DebugHelper.WriteException(ex);
}
}
}
private void btnCopyPreview_Click(object sender, EventArgs e)
{
2014-06-04 11:18:41 +12:00
ClipboardHelpers.CopyText("ffmpeg " + 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 void btnHelp_Click(object sender, EventArgs e)
{
URLHelpers.OpenURL("https://github.com/ShareX/ShareX/wiki/FFmpeg-options");
}
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
}
}