ShareX/ShareX/Forms/ScreenRecordForm.cs

320 lines
12 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13: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
2013-11-03 23:53:49 +13: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;
using ScreenCaptureLib;
2013-11-03 23:53:49 +13:00
using ShareX.Properties;
using System;
2013-11-03 23:53:49 +13:00
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace ShareX
{
public class ScreenRecordForm : TrayForm
{
public bool IsRecording { get; private set; }
private static ScreenRecordForm instance;
public static ScreenRecordForm Instance
{
get
{
if (instance == null || instance.IsDisposed)
{
instance = new ScreenRecordForm();
instance.Show();
}
return instance;
}
}
private ScreenRecorder screenRecorder;
2014-05-15 01:52:10 +12:00
private Rectangle captureRectangle;
private ScreenRegionForm regionForm;
private DWMManager dwmManager;
2013-11-03 23:53:49 +13:00
private ScreenRecordForm()
{
2014-05-28 00:58:59 +12:00
TrayIcon.Text = "ShareX";
2013-11-03 23:53:49 +13:00
TrayIcon.MouseClick += TrayIcon_MouseClick;
}
private void TrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
StopRecording();
}
}
private void SelectRegion()
{
2014-05-15 01:52:10 +12:00
if (TaskHelpers.SelectRegion(out captureRectangle) && !captureRectangle.IsEmpty)
2013-11-03 23:53:49 +13:00
{
2014-05-15 01:52:10 +12:00
captureRectangle = CaptureHelpers.EvenRectangleSize(captureRectangle);
2013-11-03 23:53:49 +13:00
}
}
private void ActiveWindowRegion(TaskSettings taskSettings)
{
IntPtr handle = NativeMethods.GetForegroundWindow();
if (handle.ToInt32() > 0)
{
if (taskSettings.CaptureSettings.CaptureClientArea)
{
captureRectangle = NativeMethods.GetClientRect(handle);
}
else
{
captureRectangle = CaptureHelpers.GetWindowRectangle(handle);
}
}
else
{
SelectRegion();
}
}
public void StartRecording(TaskSettings TaskSettings)
2013-11-03 23:53:49 +13:00
{
if (TaskSettings.CaptureSettings.RunScreencastCLI)
2014-04-20 03:39:17 +12:00
{
if (!Program.Settings.VideoEncoders.IsValidIndex(TaskSettings.CaptureSettings.VideoEncoderSelected))
2014-04-20 03:39:17 +12:00
{
2014-04-20 16:24:03 +12:00
MessageBox.Show("There is no valid CLI video encoder selected.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2014-04-20 03:39:17 +12:00
return;
}
else if (!Program.Settings.VideoEncoders[TaskSettings.CaptureSettings.VideoEncoderSelected].IsValid())
2014-04-20 03:39:17 +12:00
{
MessageBox.Show("CLI video encoder file does not exist: " + Program.Settings.VideoEncoders[TaskSettings.CaptureSettings.VideoEncoderSelected].Path,
2014-05-05 04:09:53 +12:00
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2014-04-20 03:39:17 +12:00
return;
}
}
if (TaskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.FFmpeg && !File.Exists(TaskSettings.CaptureSettings.FFmpegOptions.CLIPath))
2014-05-10 12:23:47 +12:00
{
string ffmpegText = string.IsNullOrEmpty(TaskSettings.CaptureSettings.FFmpegOptions.CLIPath) ? "ffmpeg.exe" : TaskSettings.CaptureSettings.FFmpegOptions.CLIPath;
if (MessageBox.Show(ffmpegText + " does not exist." + Environment.NewLine + Environment.NewLine + "Would you like to automatically download it?",
Application.ProductName + " - Missing ffmpeg.exe", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
if (FFmpegHelper.DownloadFFmpeg(false, DownloaderForm_InstallRequested) == DialogResult.OK)
{
Program.DefaultTaskSettings.CaptureSettings.FFmpegOptions.CLIPath = TaskSettings.TaskSettingsReference.CaptureSettings.FFmpegOptions.CLIPath =
TaskSettings.CaptureSettings.FFmpegOptions.CLIPath = Path.Combine(Program.ToolsFolder, "ffmpeg.exe");
}
}
else
{
return;
}
2014-05-10 12:23:47 +12:00
}
if (TaskSettings.AdvancedSettings.ScreenRecorderUseActiveWindow)
{
ActiveWindowRegion(TaskSettings);
}
else
{
SelectRegion();
}
2013-11-03 23:53:49 +13:00
2014-05-15 01:52:10 +12:00
if (IsRecording || captureRectangle.IsEmpty || screenRecorder != null)
2013-11-03 23:53:49 +13:00
{
return;
}
IsRecording = true;
2014-05-15 01:52:10 +12:00
Screenshot.CaptureCursor = TaskSettings.CaptureSettings.ShowCursor;
2013-11-03 23:53:49 +13:00
2014-05-28 00:58:59 +12:00
TrayIcon.Text = "ShareX - Waiting...";
2013-11-03 23:53:49 +13:00
TrayIcon.Icon = Resources.control_record_yellow.ToIcon();
TrayIcon.Visible = true;
string path = "";
float duration = TaskSettings.CaptureSettings.ScreenRecordFixedDuration ? TaskSettings.CaptureSettings.ScreenRecordDuration : 0;
regionForm = ScreenRegionForm.Show(captureRectangle, StopRecording, duration);
2014-05-24 03:39:14 +12:00
TaskEx.Run(() =>
2013-11-03 23:53:49 +13:00
{
2014-05-22 16:19:32 +12:00
try
2013-11-03 23:53:49 +13:00
{
if (TaskSettings.CaptureSettings.ScreenRecordAutoDisableAero)
{
dwmManager = new DWMManager();
dwmManager.AutoDisable();
}
2014-05-22 16:19:32 +12:00
if (TaskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.AVI)
{
path = Path.Combine(TaskSettings.CaptureFolder, TaskHelpers.GetFilename(TaskSettings, "avi"));
}
else if (TaskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.FFmpeg)
{
path = Path.Combine(TaskSettings.CaptureFolder, TaskHelpers.GetFilename(TaskSettings, TaskSettings.CaptureSettings.FFmpegOptions.Extension));
}
else
{
path = Program.ScreenRecorderCacheFilePath;
}
2014-05-09 01:28:46 +12:00
2014-05-22 16:19:32 +12:00
ScreencastOptions options = new ScreencastOptions()
{
FFmpeg = TaskSettings.CaptureSettings.FFmpegOptions,
AVI = TaskSettings.CaptureSettings.AVIOptions,
2014-05-22 16:19:32 +12:00
ScreenRecordFPS = TaskSettings.CaptureSettings.ScreenRecordFPS,
GIFFPS = TaskSettings.CaptureSettings.GIFFPS,
Duration = duration,
OutputPath = path,
CaptureArea = captureRectangle,
2014-05-22 16:19:32 +12:00
DrawCursor = TaskSettings.CaptureSettings.ShowCursor
};
2013-11-03 23:53:49 +13:00
2014-05-22 16:19:32 +12:00
screenRecorder = new ScreenRecorder(options, captureRectangle, TaskSettings.CaptureSettings.ScreenRecordOutput);
2013-11-03 23:53:49 +13:00
2014-05-22 16:19:32 +12:00
int delay = (int)(TaskSettings.CaptureSettings.ScreenRecordStartDelay * 1000);
2013-11-03 23:53:49 +13:00
2014-05-22 16:19:32 +12:00
if (delay > 0)
{
Thread.Sleep(delay);
}
2013-11-03 23:53:49 +13:00
TrayIcon.Text = "ShareX - Click tray icon to stop recording.";
TrayIcon.Icon = Resources.control_record.ToIcon();
if (regionForm != null)
2014-05-22 16:19:32 +12:00
{
this.InvokeSafe(() => regionForm.StartTimer());
}
2013-11-03 23:53:49 +13:00
2014-05-22 16:19:32 +12:00
screenRecorder.StartRecording();
}
finally
{
if (dwmManager != null)
{
dwmManager.Dispose();
dwmManager = null;
}
if (regionForm != null)
2014-05-22 16:19:32 +12:00
{
this.InvokeSafe(() => regionForm.Close());
2014-05-22 16:19:32 +12:00
}
2013-11-03 23:53:49 +13:00
}
2014-05-22 16:19:32 +12:00
try
2013-11-03 23:53:49 +13:00
{
2014-05-22 16:19:32 +12:00
if (screenRecorder != null)
{
2014-05-28 00:58:59 +12:00
TrayIcon.Text = "ShareX - Encoding...";
TrayIcon.Icon = Resources.camcorder__pencil.ToIcon();
2013-11-03 23:53:49 +13:00
2014-05-22 16:19:32 +12:00
string sourceFilePath = path;
2014-05-22 16:19:32 +12:00
if (TaskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.GIF)
{
2014-05-22 16:19:32 +12:00
if (TaskSettings.CaptureSettings.RunScreencastCLI)
{
sourceFilePath = Path.ChangeExtension(Program.ScreenRecorderCacheFilePath, "gif");
}
else
{
sourceFilePath = path = Path.Combine(TaskSettings.CaptureFolder, TaskHelpers.GetFilename(TaskSettings, "gif"));
}
screenRecorder.SaveAsGIF(sourceFilePath, TaskSettings.ImageSettings.ImageGIFQuality);
}
2014-05-22 16:19:32 +12:00
if (TaskSettings.CaptureSettings.RunScreencastCLI)
2013-11-03 23:53:49 +13:00
{
2014-05-22 16:19:32 +12:00
VideoEncoder encoder = Program.Settings.VideoEncoders[TaskSettings.CaptureSettings.VideoEncoderSelected];
path = Path.Combine(TaskSettings.CaptureFolder, TaskHelpers.GetFilename(TaskSettings, encoder.OutputExtension));
screenRecorder.EncodeUsingCommandLine(encoder, sourceFilePath, path);
2013-11-03 23:53:49 +13:00
}
}
2014-05-22 16:19:32 +12:00
}
finally
{
if (screenRecorder != null)
{
2014-05-22 16:19:32 +12:00
if (TaskSettings.CaptureSettings.RunScreencastCLI && !string.IsNullOrEmpty(screenRecorder.CachePath) && File.Exists(screenRecorder.CachePath))
{
File.Delete(screenRecorder.CachePath);
}
screenRecorder.Dispose();
screenRecorder = null;
}
2013-11-03 23:53:49 +13:00
}
},
() =>
2013-11-03 23:53:49 +13:00
{
if (TrayIcon.Visible)
{
TrayIcon.Visible = false;
}
2014-05-22 16:19:32 +12:00
IsRecording = false;
if (!string.IsNullOrEmpty(path) && File.Exists(path))
2013-11-03 23:53:49 +13:00
{
UploadTask task = UploadTask.CreateFileJobTask(path, TaskSettings);
TaskManager.Start(task);
2013-11-03 23:53:49 +13:00
}
});
2013-11-03 23:53:49 +13:00
}
private void DownloaderForm_InstallRequested(string filePath)
{
string extractPath = Path.Combine(Program.ToolsFolder, "ffmpeg.exe");
bool result = FFmpegHelper.ExtractFFmpeg(filePath, extractPath);
if (result)
{
MessageBox.Show("FFmpeg successfully downloaded.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Download of FFmpeg failed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2013-11-03 23:53:49 +13:00
public void StopRecording()
{
if (IsRecording && screenRecorder != null)
{
screenRecorder.StopRecording();
}
}
}
}