public void StartRecording(TaskSettings taskSettings) { if (taskSettings.CaptureSettings.RunScreencastCLI) { if (!Program.Settings.VideoEncoders.IsValidIndex(taskSettings.CaptureSettings.VideoEncoderSelected)) { MessageBox.Show(Resources.ScreenRecordForm_StartRecording_There_is_no_valid_CLI_video_encoder_selected_, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].IsValid()) { MessageBox.Show(Resources.ScreenRecordForm_StartRecording_CLI_video_encoder_file_does_not_exist__ + Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].Path, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.FFmpeg) { 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) { 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; } } 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; } } Rectangle captureRectangle; TaskHelpers.SelectRegion(out captureRectangle); captureRectangle = CaptureHelpers.EvenRectangleSize(captureRectangle); if (IsRecording || !captureRectangle.IsValid() || screenRecorder != null) { return; } IsRecording = true; Screenshot.CaptureCursor = taskSettings.CaptureSettings.ShowCursor; TrayIcon.Text = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Waiting___; 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); TaskEx.Run(() => { try { if (taskSettings.CaptureSettings.ScreenRecordAutoDisableAero) { dwmManager = new DWMManager(); dwmManager.AutoDisable(); } 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; } ScreencastOptions options = new ScreencastOptions() { FFmpeg = taskSettings.CaptureSettings.FFmpegOptions, AVI = taskSettings.CaptureSettings.AVIOptions, ScreenRecordFPS = taskSettings.CaptureSettings.ScreenRecordFPS, GIFFPS = taskSettings.CaptureSettings.GIFFPS, Duration = duration, OutputPath = path, CaptureArea = captureRectangle, DrawCursor = taskSettings.CaptureSettings.ShowCursor }; screenRecorder = new ScreenRecorder(options, captureRectangle, taskSettings.CaptureSettings.ScreenRecordOutput); int delay = (int)(taskSettings.CaptureSettings.ScreenRecordStartDelay * 1000); if (delay > 0) { Thread.Sleep(delay); } TrayIcon.Text = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Click_tray_icon_to_stop_recording_; TrayIcon.Icon = Resources.control_record.ToIcon(); if (regionForm != null) { this.InvokeSafe(() => regionForm.StartTimer()); } screenRecorder.StartRecording(); } finally { if (dwmManager != null) { dwmManager.Dispose(); dwmManager = null; } if (regionForm != null) { this.InvokeSafe(() => regionForm.Close()); } } try { if (screenRecorder != null) { TrayIcon.Text = "ShareX - " + Resources.ScreenRecordForm_StartRecording_Encoding___; TrayIcon.Icon = Resources.camcorder_pencil.ToIcon(); string sourceFilePath = path; if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.GIF) { 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); } if (taskSettings.CaptureSettings.RunScreencastCLI) { VideoEncoder encoder = Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected]; path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, encoder.OutputExtension)); screenRecorder.EncodeUsingCommandLine(encoder, sourceFilePath, path); } } } finally { if (screenRecorder != null) { if (taskSettings.CaptureSettings.RunScreencastCLI && !string.IsNullOrEmpty(screenRecorder.CachePath) && File.Exists(screenRecorder.CachePath)) { File.Delete(screenRecorder.CachePath); } screenRecorder.Dispose(); screenRecorder = null; } } }, () => { if (TrayIcon.Visible) { TrayIcon.Visible = false; } IsRecording = false; if (!string.IsNullOrEmpty(path) && File.Exists(path) && TaskHelpers.ShowAfterCaptureForm(taskSettings)) { UploadTask task = UploadTask.CreateFileJobTask(path, taskSettings); TaskManager.Start(task); } }); }
public void StartRecording(TaskSettings taskSettings) { if (taskSettings.CaptureSettings.RunScreencastCLI) { if (!Program.Settings.VideoEncoders.IsValidIndex(taskSettings.CaptureSettings.VideoEncoderSelected)) { MessageBox.Show("There is no valid CLI video encoder selected.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if (!Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].IsValid()) { MessageBox.Show("CLI video encoder file does not exist: " + Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].Path, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.FFmpeg && !File.Exists(taskSettings.CaptureSettings.FFmpegOptions.CLIPath)) { 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; } } if (taskSettings.AdvancedSettings.ScreenRecorderUseActiveWindow) { ActiveWindowRegion(taskSettings); } else { SelectRegion(); } captureRectangle = CaptureHelpers.EvenRectangleSize(captureRectangle); if (IsRecording || !captureRectangle.IsValid() || screenRecorder != null) { return; } IsRecording = true; Screenshot.CaptureCursor = taskSettings.CaptureSettings.ShowCursor; TrayIcon.Text = "ShareX - Waiting..."; 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); TaskEx.Run(() => { try { if (taskSettings.CaptureSettings.ScreenRecordAutoDisableAero) { dwmManager = new DWMManager(); dwmManager.AutoDisable(); } 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; } ScreencastOptions options = new ScreencastOptions() { FFmpeg = taskSettings.CaptureSettings.FFmpegOptions, AVI = taskSettings.CaptureSettings.AVIOptions, ScreenRecordFPS = taskSettings.CaptureSettings.ScreenRecordFPS, GIFFPS = taskSettings.CaptureSettings.GIFFPS, Duration = duration, OutputPath = path, CaptureArea = captureRectangle, DrawCursor = taskSettings.CaptureSettings.ShowCursor }; screenRecorder = new ScreenRecorder(options, captureRectangle, taskSettings.CaptureSettings.ScreenRecordOutput); int delay = (int)(taskSettings.CaptureSettings.ScreenRecordStartDelay * 1000); if (delay > 0) { Thread.Sleep(delay); } TrayIcon.Text = "ShareX - Click tray icon to stop recording."; TrayIcon.Icon = Resources.control_record.ToIcon(); if (regionForm != null) { this.InvokeSafe(() => regionForm.StartTimer()); } screenRecorder.StartRecording(); } finally { if (dwmManager != null) { dwmManager.Dispose(); dwmManager = null; } if (regionForm != null) { this.InvokeSafe(() => regionForm.Close()); } } try { if (screenRecorder != null) { TrayIcon.Text = "ShareX - Encoding..."; TrayIcon.Icon = Resources.camcorder_pencil.ToIcon(); string sourceFilePath = path; if (taskSettings.CaptureSettings.ScreenRecordOutput == ScreenRecordOutput.GIF) { 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); } if (taskSettings.CaptureSettings.RunScreencastCLI) { VideoEncoder encoder = Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected]; path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, encoder.OutputExtension)); screenRecorder.EncodeUsingCommandLine(encoder, sourceFilePath, path); } } } finally { if (screenRecorder != null) { if (taskSettings.CaptureSettings.RunScreencastCLI && !string.IsNullOrEmpty(screenRecorder.CachePath) && File.Exists(screenRecorder.CachePath)) { File.Delete(screenRecorder.CachePath); } screenRecorder.Dispose(); screenRecorder = null; } } }, () => { if (TrayIcon.Visible) { TrayIcon.Visible = false; } IsRecording = false; if (!string.IsNullOrEmpty(path) && File.Exists(path) && TaskHelpers.ShowAfterCaptureForm(taskSettings)) { UploadTask task = UploadTask.CreateFileJobTask(path, taskSettings); TaskManager.Start(task); } }); }