ShareX/ScreenCaptureLib/Screencast/FFmpegOptionsForm.cs

320 lines
11 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-05-13 21:06:40 +12:00
Copyright (C) 2007-2014 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)
using HelpersLib;
2014-05-12 09:40:49 +12:00
using ScreenCaptureLib;
2014-05-10 12:23:47 +12:00
using SevenZip;
2014-05-09 15:14:53 +12:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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.Linq;
using System.Text;
2014-05-10 12:23:47 +12:00
using System.Text.RegularExpressions;
2014-05-09 15:14:53 +12:00
using System.Windows.Forms;
namespace 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
private readonly int libmp3lame_qscale_end = 9;
2014-05-13 20:57:05 +12:00
public FFmpegOptionsForm(ScreencastOptions options)
2014-05-09 15:14:53 +12:00
{
InitializeComponent();
Icon = ShareXResources.Icon;
Text = string.Format("{0} - FFmpeg Options", Application.ProductName);
2014-05-09 15:14:53 +12:00
Options = options;
2014-05-13 20:57:05 +12:00
if (Options != null)
{
2014-05-12 13:13:42 +12:00
SettingsLoad();
BindUpdatePreview(Controls);
2014-05-12 20:40:56 +12:00
UpdateExtensions();
}
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-05-12 13:13:42 +12:00
// General
RefreshSourcesAsync();
cboVideoCodec.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegVideoCodec>());
cboVideoCodec.SelectedIndex = (int)Options.FFmpeg.VideoCodec;
cboAudioCodec.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegAudioCodec>());
cboAudioCodec.SelectedIndex = (int)Options.FFmpeg.AudioCodec;
2014-05-12 11:19:38 +12:00
cbExtension.Text = Options.FFmpeg.Extension;
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);
2014-05-12 13:13:42 +12:00
cbPreset.Items.AddRange(Helpers.GetEnumDescriptions<FFmpegPreset>());
cbPreset.SelectedIndex = (int)Options.FFmpeg.Preset;
// VPx
nudVPxCRF.Value = Options.FFmpeg.VPx_CRF.Between((int)nudVPxCRF.Minimum, (int)nudVPxCRF.Maximum);
2014-05-12 13:13:42 +12:00
// XviD
nudQscale.Value = Options.FFmpeg.XviD_qscale.Between((int)nudQscale.Minimum, (int)nudQscale.Maximum);
// Audio - Vorbis
tbVorbis_qscale.Value = Options.FFmpeg.Vorbis_qscale;
// Audio - MP3
tbMP3_qscale.Value = libmp3lame_qscale_end - Options.FFmpeg.MP3_qscale; // 0-9 where a lower value is a higher quality
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;
Helpers.AsyncJob(() =>
{
using (FFmpegHelper ffmpeg = new FFmpegHelper(Options))
{
devices = ffmpeg.GetDirectShowDevices();
}
},
() =>
{
cboVideoSource.Items.Clear();
cboVideoSource.Items.Add("GDI grab");
cboAudioSource.Items.Clear();
cboAudioSource.Items.Add("None");
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
});
}
private void BindUpdatePreview(System.Windows.Forms.Control.ControlCollection controls)
{
controls.Cast<Control>().ForEach(x => { if (x.HasChildren) BindUpdatePreview(x.Controls); });
foreach (Control ctl in controls)
{
if (ctl is NumericUpDown)
{
((NumericUpDown)ctl).ValueChanged += (sender, e) => UpdatePreview();
}
else if (ctl is TrackBar)
{
((TrackBar)ctl).ValueChanged += (sender, e) => UpdatePreview();
}
2014-05-14 14:33:04 +12:00
else if (ctl is TextBox && ctl != txtCommandLinePreview)
{
((TextBox)ctl).TextChanged += (sender, e) => UpdatePreview();
}
else if (ctl is ComboBox)
{
((ComboBox)ctl).SelectedIndexChanged += (sender, e) => UpdatePreview();
}
}
}
public void UpdatePreview()
2014-05-09 15:14:53 +12:00
{
txtCommandLinePreview.Text = "ffmpeg " + Options.GetFFmpegArgs();
2014-05-12 20:40:56 +12:00
}
2014-05-12 20:40:56 +12:00
public void UpdateExtensions()
{
cbExtension.Items.Clear();
if (Options.FFmpeg.VideoCodec == FFmpegVideoCodec.libx264 || Options.FFmpeg.VideoCodec == FFmpegVideoCodec.libxvid)
2014-05-12 11:19:38 +12:00
{
2014-05-12 20:40:56 +12:00
cbExtension.Items.Add("mp4");
2014-05-12 11:19:38 +12:00
}
2014-05-12 20:40:56 +12:00
else if (Options.FFmpeg.VideoCodec == FFmpegVideoCodec.libvpx)
2014-05-12 11:19:38 +12:00
{
2014-05-12 20:40:56 +12:00
cbExtension.Items.Add("webm");
2014-05-12 11:19:38 +12:00
}
2014-05-12 20:40:56 +12:00
cbExtension.Items.Add("avi");
cbExtension.SelectedIndex = 0;
2014-05-12 14:47:03 +12:00
tcFFmpegVideoCodecs.SelectedIndex = (int)Options.FFmpeg.VideoCodec;
2014-05-09 15:14:53 +12:00
}
2014-05-09 18:41:16 +12:00
private void btnRefreshSources_Click(object sender, EventArgs e)
{
RefreshSourcesAsync();
}
private void cboVideoSource_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.VideoSource = cboVideoSource.Text;
}
private void cboAudioSource_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.AudioSource = cboAudioSource.Text;
}
private void cboVideoCodec_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.VideoCodec = (FFmpegVideoCodec)cboVideoCodec.SelectedIndex;
UpdateExtensions();
}
private void cboAudioCodec_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.AudioCodec = (FFmpegAudioCodec)cboAudioCodec.SelectedIndex;
}
private void cbExtension_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.Extension = cbExtension.Text;
}
private void nudx264CRF_ValueChanged(object sender, EventArgs e)
{
Options.FFmpeg.x264_CRF = (int)nudx264CRF.Value;
}
private void cbPreset_SelectedIndexChanged(object sender, EventArgs e)
{
Options.FFmpeg.Preset = (FFmpegPreset)cbPreset.SelectedIndex;
}
private void nudVPxCRF_ValueChanged(object sender, EventArgs e)
{
Options.FFmpeg.VPx_CRF = (int)nudVPxCRF.Value;
}
private void nudQscale_ValueChanged(object sender, EventArgs e)
{
Options.FFmpeg.XviD_qscale = (int)nudQscale.Value;
}
private void tbFFmpegPath_TextChanged(object sender, EventArgs e)
{
Options.FFmpeg.CLIPath = txtFFmpegPath.Text;
txtFFmpegPath.BackColor = File.Exists(txtFFmpegPath.Text) ? Color.LightGreen : Color.LightPink;
}
2014-05-09 20:12:44 +12:00
private void buttonFFmpegBrowse_Click(object sender, EventArgs e)
{
if (Helpers.BrowseFile("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;
}
private void buttonFFmpegHelp_Click(object sender, EventArgs e)
{
Helpers.OpenURL("https://www.ffmpeg.org/ffmpeg.html");
2014-05-09 20:12:44 +12:00
}
2014-05-10 12:23:47 +12:00
private void btnDownload_Click(object sender, EventArgs e)
{
FFmpegHelper.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";
bool result = FFmpegHelper.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();
UpdatePreview();
2014-05-13 18:57:09 +12:00
});
2014-05-10 12:23:47 +12:00
}
else
{
MessageBox.Show("Download of FFmpeg failed.", Application.ProductName, 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");
psi.Arguments = "/k ffmpeg " + Options.GetFFmpegArgs();
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)
{
ClipboardHelpers.CopyText(txtCommandLinePreview.Text);
2014-05-13 18:57:09 +12:00
}
private void tbVorbis_qscale_Scroll(object sender, EventArgs e)
{
Options.FFmpeg.Vorbis_qscale = tbVorbis_qscale.Value;
}
private void tbMP3_qscale_Scroll(object sender, EventArgs e)
{
Options.FFmpeg.MP3_qscale = libmp3lame_qscale_end - tbMP3_qscale.Value; // 0-9 where a lower value is a higher quality
}
2014-05-09 15:14:53 +12:00
}
}