ShareX/ShareX/Forms/ScreenRecordForm.cs

345 lines
14 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.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;
private ScreenRegionForm regionForm;
private DWMManager dwmManager;
private bool abortRequested;
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;
}
public void StartStopRecording()
{
if (regionForm != null && !regionForm.IsDisposed)
{
regionForm.StartStop();
}
}
private void StopRecording()
{
if (IsRecording && screenRecorder != null)
{
screenRecorder.StopRecording();
}
}
2013-11-03 23:53:49 +13:00
private void TrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
StartStopRecording();
2013-11-03 23:53:49 +13:00
}
}
2014-07-17 18:58:17 +12:00
public void StartRecording(TaskSettings taskSettings)
2013-11-03 23:53:49 +13:00
{
2014-07-17 18:58:17 +12:00
if (taskSettings.CaptureSettings.RunScreencastCLI)
2014-04-20 03:39:17 +12:00
{
2014-07-17 18:58:17 +12:00
if (!Program.Settings.VideoEncoders.IsValidIndex(taskSettings.CaptureSettings.VideoEncoderSelected))
2014-04-20 03:39:17 +12:00
{
MessageBox.Show(Resources.ScreenRecordForm_StartRecording_There_is_no_valid_CLI_video_encoder_selected_,
"ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning);
2014-04-20 03:39:17 +12:00
return;
}
2014-10-19 10:48:47 +13:00
if (!Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].IsValid())
2014-04-20 03:39:17 +12:00
{
MessageBox.Show(Resources.ScreenRecordForm_StartRecording_CLI_video_encoder_file_does_not_exist__ +
Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].Path,
"ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning);
2014-04-20 03:39:17 +12:00
return;
}
}
if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.FFmpeg)
2014-05-10 12:23:47 +12:00
{
if (!File.Exists(taskSettings.CaptureSettings.FFmpegOptions.CLIPath))
{
string ffmpegText = string.IsNullOrEmpty(taskSettings.CaptureSettings.FFmpegOptions.CLIPath) ? "ffmpeg.exe" : taskSettings.CaptureSettings.FFmpegOptions.CLIPath;
if (MessageBox.Show(string.Format(Resources.ScreenRecordForm_StartRecording_does_not_exist, ffmpegText),
"ShareX - " + Resources.ScreenRecordForm_StartRecording_Missing + " ffmpeg.exe", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
2014-12-09 05:15:18 +13:00
if (FFmpegDownloader.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;
}
}
if (!taskSettings.CaptureSettings.FFmpegOptions.IsSourceSelected)
{
MessageBox.Show(Resources.ScreenRecordForm_StartRecording_FFmpeg_video_and_audio_source_both_can_t_be__None__,
"ShareX - " + Resources.ScreenRecordForm_StartRecording_FFmpeg_error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
2014-05-10 12:23:47 +12:00
}
Rectangle captureRectangle;
TaskHelpers.SelectRegion(out captureRectangle, taskSettings);
captureRectangle = CaptureHelpers.EvenRectangleSize(captureRectangle);
if (IsRecording || !captureRectangle.IsValid() || screenRecorder != null)
2013-11-03 23:53:49 +13:00
{
return;
}
IsRecording = true;
2014-07-17 18:58:17 +12:00
Screenshot.CaptureCursor = taskSettings.CaptureSettings.ShowCursor;
2013-11-03 23:53:49 +13:00
TrayIcon.Text = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Waiting___;
2013-11-03 23:53:49 +13:00
TrayIcon.Icon = Resources.control_record_yellow.ToIcon();
TrayIcon.Visible = true;
string path = "";
2014-07-17 18:58:17 +12:00
float duration = taskSettings.CaptureSettings.ScreenRecordFixedDuration ? taskSettings.CaptureSettings.ScreenRecordDuration : 0;
regionForm = ScreenRegionForm.Show(captureRectangle, StopRecording, duration);
regionForm.RecordResetEvent = new ManualResetEvent(false);
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
{
2014-07-17 18:58:17 +12:00
if (taskSettings.CaptureSettings.ScreenRecordAutoDisableAero)
{
dwmManager = new DWMManager();
dwmManager.AutoDisable();
}
2014-07-17 18:58:17 +12:00
if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.AVI)
2014-05-22 16:19:32 +12:00
{
2014-07-17 18:58:17 +12:00
path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, "avi"));
2014-05-22 16:19:32 +12:00
}
2014-07-17 18:58:17 +12:00
else if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.FFmpeg)
2014-05-22 16:19:32 +12:00
{
2014-07-17 18:58:17 +12:00
path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, taskSettings.CaptureSettings.FFmpegOptions.Extension));
2014-05-22 16:19:32 +12:00
}
else
{
path = Program.ScreenRecorderCacheFilePath;
}
2014-05-09 01:28:46 +12:00
2014-05-22 16:19:32 +12:00
ScreencastOptions options = new ScreencastOptions()
{
2014-07-17 18:58:17 +12:00
FFmpeg = taskSettings.CaptureSettings.FFmpegOptions,
AVI = taskSettings.CaptureSettings.AVIOptions,
ScreenRecordFPS = taskSettings.CaptureSettings.ScreenRecordFPS,
GIFFPS = taskSettings.CaptureSettings.GIFFPS,
Duration = duration,
OutputPath = path,
CaptureArea = captureRectangle,
2014-07-17 18:58:17 +12:00
DrawCursor = taskSettings.CaptureSettings.ShowCursor
2014-05-22 16:19:32 +12:00
};
2013-11-03 23:53:49 +13:00
2014-07-17 18:58:17 +12:00
screenRecorder = new ScreenRecorder(options, captureRectangle, taskSettings.CaptureSettings.ScreenRecordOutput);
2013-11-03 23:53:49 +13:00
if (regionForm != null && regionForm.RecordResetEvent != null)
2014-05-22 16:19:32 +12:00
{
TrayIcon.Text = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_start_recording_;
if (taskSettings.CaptureSettings.ScreenRecordAutoStart)
{
int delay = (int)(taskSettings.CaptureSettings.ScreenRecordStartDelay * 1000);
if (delay > 0)
{
regionForm.RecordResetEvent.WaitOne(delay);
}
}
else
{
regionForm.RecordResetEvent.WaitOne();
}
2013-11-03 23:53:49 +13:00
if (regionForm.AbortRequested)
{
abortRequested = true;
}
}
if (!abortRequested)
2014-05-22 16:19:32 +12:00
{
TrayIcon.Text = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_stop_recording_;
TrayIcon.Icon = Resources.control_record.ToIcon();
2013-11-03 23:53:49 +13:00
if (regionForm != null)
{
this.InvokeSafe(() => regionForm.StartTimer());
}
screenRecorder.StartRecording();
if (regionForm != null && regionForm.AbortRequested)
{
abortRequested = true;
}
}
2014-05-22 16:19:32 +12:00
}
finally
{
if (dwmManager != null)
{
dwmManager.Dispose();
dwmManager = null;
}
if (regionForm != null)
2014-05-22 16:19:32 +12:00
{
if (regionForm.RecordResetEvent != null)
{
regionForm.RecordResetEvent.Dispose();
}
this.InvokeSafe(() => regionForm.Close());
regionForm = null;
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
{
if (!abortRequested && screenRecorder != null)
2014-05-22 16:19:32 +12:00
{
TrayIcon.Text = "ShareX - " + Resources.ScreenRecordForm_StartRecording_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-07-17 18:58:17 +12:00
if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.GIF)
{
2014-07-17 18:58:17 +12:00
if (taskSettings.CaptureSettings.RunScreencastCLI)
2014-05-22 16:19:32 +12:00
{
sourceFilePath = Path.ChangeExtension(Program.ScreenRecorderCacheFilePath, "gif");
}
else
{
2014-07-17 18:58:17 +12:00
sourceFilePath = path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, "gif"));
2014-05-22 16:19:32 +12:00
}
2014-07-17 18:58:17 +12:00
screenRecorder.SaveAsGIF(sourceFilePath, taskSettings.ImageSettings.ImageGIFQuality);
}
2014-05-22 16:19:32 +12:00
2014-07-17 18:58:17 +12:00
if (taskSettings.CaptureSettings.RunScreencastCLI)
2013-11-03 23:53:49 +13:00
{
2014-07-17 18:58:17 +12:00
VideoEncoder encoder = Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected];
path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, encoder.OutputExtension));
2014-05-22 16:19:32 +12:00
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-07-17 18:58:17 +12:00
if (taskSettings.CaptureSettings.RunScreencastCLI && !string.IsNullOrEmpty(screenRecorder.CachePath) && File.Exists(screenRecorder.CachePath))
2014-05-22 16:19:32 +12:00
{
File.Delete(screenRecorder.CachePath);
}
screenRecorder.Dispose();
screenRecorder = null;
if (abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path))
{
File.Delete(path);
}
}
2013-11-03 23:53:49 +13:00
}
},
() =>
2013-11-03 23:53:49 +13:00
{
if (TrayIcon.Visible)
{
TrayIcon.Visible = false;
}
if (!abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path) && TaskHelpers.ShowAfterCaptureForm(taskSettings))
2013-11-03 23:53:49 +13:00
{
2014-07-17 18:58:17 +12:00
UploadTask task = UploadTask.CreateFileJobTask(path, taskSettings);
TaskManager.Start(task);
2013-11-03 23:53:49 +13:00
}
abortRequested = false;
IsRecording = false;
});
2013-11-03 23:53:49 +13:00
}
private void DownloaderForm_InstallRequested(string filePath)
{
string extractPath = Path.Combine(Program.ToolsFolder, "ffmpeg.exe");
2014-12-09 05:15:18 +13:00
bool result = FFmpegDownloader.ExtractFFmpeg(filePath, extractPath);
if (result)
{
MessageBox.Show(Resources.ScreenRecordForm_DownloaderForm_InstallRequested_FFmpeg_successfully_downloaded_, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(Resources.ScreenRecordForm_DownloaderForm_InstallRequested_Download_of_FFmpeg_failed_, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
2013-11-03 23:53:49 +13:00
}
}