private static string ProcessTwoPassEncoding(string input, TaskSettings taskSettings, bool deleteInputFile = true) { string filename = TaskHelpers.GetFilename(taskSettings, taskSettings.CaptureSettings.FFmpegOptions.Extension); string output = Path.Combine(taskSettings.GetScreenshotsFolder(), filename); try { if (taskSettings.CaptureSettings.FFmpegOptions.VideoCodec == FFmpegVideoCodec.gif) { screenRecorder.FFmpegEncodeAsGIF(input, output); } else { screenRecorder.FFmpegEncodeVideo(input, output); } } finally { if (deleteInputFile && !input.Equals(output, StringComparison.InvariantCultureIgnoreCase) && File.Exists(input)) { File.Delete(input); } } return(output); }
public static void SaveImageAsFile(Bitmap bmp, TaskSettings taskSettings, bool overwriteFile = false) { using (ImageData imageData = PrepareImage(bmp, taskSettings)) { string fileName = GetFilename(taskSettings, imageData.ImageFormat.GetDescription(), bmp); string filePath = Path.Combine(taskSettings.GetScreenshotsFolder(), fileName); if (!overwriteFile) { filePath = HandleExistsFile(filePath, taskSettings); } if (!string.IsNullOrEmpty(filePath)) { imageData.Write(filePath); DebugHelper.WriteLine("Image saved to file: " + filePath); } } }
private static void StartRecording(ScreenRecordOutput outputType, TaskSettings taskSettings, ScreenRecordStartMethod startMethod = ScreenRecordStartMethod.Region) { if (outputType == ScreenRecordOutput.GIF) { taskSettings.CaptureSettings.FFmpegOptions.VideoCodec = FFmpegVideoCodec.gif; } if (taskSettings.CaptureSettings.FFmpegOptions.IsAnimatedImage) { taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding = true; } int fps; if (taskSettings.CaptureSettings.FFmpegOptions.VideoCodec == FFmpegVideoCodec.gif) { fps = taskSettings.CaptureSettings.GIFFPS; } else { fps = taskSettings.CaptureSettings.ScreenRecordFPS; } DebugHelper.WriteLine("Starting screen recording. Video encoder: \"{0}\", Audio encoder: \"{1}\", FPS: {2}", taskSettings.CaptureSettings.FFmpegOptions.VideoCodec.GetDescription(), taskSettings.CaptureSettings.FFmpegOptions.AudioCodec.GetDescription(), fps); if (!TaskHelpers.CheckFFmpeg(taskSettings)) { return; } if (!taskSettings.CaptureSettings.FFmpegOptions.IsSourceSelected) { MessageBox.Show(Resources.FFmpeg_FFmpeg_video_and_audio_source_both_can_t_be__None__, "ShareX - " + Resources.FFmpeg_FFmpeg_error, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } Rectangle captureRectangle = Rectangle.Empty; switch (startMethod) { case ScreenRecordStartMethod.Region: if (taskSettings.CaptureSettings.ScreenRecordTransparentRegion) { RegionCaptureTasks.GetRectangleRegionTransparent(out captureRectangle); } else { RegionCaptureTasks.GetRectangleRegion(out captureRectangle, taskSettings.CaptureSettings.SurfaceOptions); } break; case ScreenRecordStartMethod.ActiveWindow: if (taskSettings.CaptureSettings.CaptureClientArea) { captureRectangle = CaptureHelpers.GetActiveWindowClientRectangle(); } else { captureRectangle = CaptureHelpers.GetActiveWindowRectangle(); } break; case ScreenRecordStartMethod.CustomRegion: captureRectangle = taskSettings.CaptureSettings.CaptureCustomRegion; break; case ScreenRecordStartMethod.LastRegion: captureRectangle = Program.Settings.ScreenRecordRegion; break; } Rectangle screenRectangle = CaptureHelpers.GetScreenBounds(); captureRectangle = Rectangle.Intersect(captureRectangle, screenRectangle); if (taskSettings.CaptureSettings.FFmpegOptions.IsEvenSizeRequired) { captureRectangle = CaptureHelpers.EvenRectangleSize(captureRectangle); } if (IsRecording || !captureRectangle.IsValid() || screenRecorder != null) { return; } Program.Settings.ScreenRecordRegion = captureRectangle; IsRecording = true; string path = ""; bool abortRequested = false; float duration = taskSettings.CaptureSettings.ScreenRecordFixedDuration ? taskSettings.CaptureSettings.ScreenRecordDuration : 0; recordForm = new ScreenRecordForm(captureRectangle) { ActivateWindow = startMethod == ScreenRecordStartMethod.Region, Duration = duration, AskConfirmationOnAbort = taskSettings.CaptureSettings.ScreenRecordAskConfirmationOnAbort }; recordForm.StopRequested += StopRecording; recordForm.Show(); Task.Run(() => { try { string extension; if (taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding) { extension = "mp4"; } else { extension = taskSettings.CaptureSettings.FFmpegOptions.Extension; } string filename = TaskHelpers.GetFilename(taskSettings, extension); path = TaskHelpers.HandleExistsFile(taskSettings.GetScreenshotsFolder(), filename, taskSettings); if (string.IsNullOrEmpty(path)) { abortRequested = true; } if (!abortRequested) { recordForm.ChangeState(ScreenRecordState.BeforeStart); if (taskSettings.CaptureSettings.ScreenRecordAutoStart) { int delay = (int)(taskSettings.CaptureSettings.ScreenRecordStartDelay * 1000); if (delay > 0) { recordForm.InvokeSafe(() => recordForm.StartCountdown(delay)); recordForm.RecordResetEvent.WaitOne(delay); } } else { recordForm.RecordResetEvent.WaitOne(); } if (recordForm.IsAbortRequested) { abortRequested = true; } if (!abortRequested) { ScreencastOptions options = new ScreencastOptions() { IsRecording = true, IsLossless = taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding, FFmpeg = taskSettings.CaptureSettings.FFmpegOptions, FPS = fps, Duration = duration, OutputPath = path, CaptureArea = captureRectangle, DrawCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor }; Screenshot screenshot = TaskHelpers.GetScreenshot(taskSettings); screenshot.CaptureCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor; screenRecorder = new ScreenRecorder(ScreenRecordOutput.FFmpeg, options, screenshot, captureRectangle); screenRecorder.RecordingStarted += ScreenRecorder_RecordingStarted; screenRecorder.EncodingProgressChanged += ScreenRecorder_EncodingProgressChanged; recordForm.ChangeState(ScreenRecordState.AfterStart); screenRecorder.StartRecording(); if (recordForm.IsAbortRequested) { abortRequested = true; } } } } catch (Exception e) { DebugHelper.WriteException(e); } if (taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding && !abortRequested && screenRecorder != null && File.Exists(path)) { recordForm.ChangeState(ScreenRecordState.Encoding); path = ProcessTwoPassEncoding(path, taskSettings); } if (recordForm != null) { recordForm.InvokeSafe(() => { recordForm.Close(); recordForm.Dispose(); recordForm = null; }); } if (screenRecorder != null) { screenRecorder.Dispose(); screenRecorder = null; if (abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path)) { File.Delete(path); } } }).ContinueInCurrentContext(() => { string customFileName; if (!abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path) && TaskHelpers.ShowAfterCaptureForm(taskSettings, out customFileName, null, path)) { if (!string.IsNullOrEmpty(customFileName)) { string currentFilename = Path.GetFileNameWithoutExtension(path); string ext = Path.GetExtension(path); if (!currentFilename.Equals(customFileName, StringComparison.InvariantCultureIgnoreCase)) { path = Helpers.RenameFile(path, customFileName + ext); } } WorkerTask task = WorkerTask.CreateFileJobTask(path, taskSettings, customFileName); TaskManager.Start(task); } abortRequested = false; IsRecording = false; }); }
public static Bitmap AnnotateImage(Bitmap bmp, string filePath, TaskSettings taskSettings, bool taskMode = false) { if (bmp != null) { using (bmp) { RegionCaptureMode mode = taskMode ? RegionCaptureMode.TaskEditor : RegionCaptureMode.Editor; RegionCaptureOptions options = taskSettings.CaptureSettingsReference.SurfaceOptions; using (RegionCaptureForm form = new RegionCaptureForm(mode, options, bmp)) { form.ImageFilePath = filePath; form.SaveImageRequested += (output, newFilePath) => { using (output) { if (string.IsNullOrEmpty(newFilePath)) { string fileName = GetFilename(taskSettings, taskSettings.ImageSettings.ImageFormat.GetDescription(), output); newFilePath = Path.Combine(taskSettings.GetScreenshotsFolder(), fileName); } ImageHelpers.SaveImage(output, newFilePath); } return(newFilePath); }; form.SaveImageAsRequested += (output, newFilePath) => { using (output) { if (string.IsNullOrEmpty(newFilePath)) { string fileName = GetFilename(taskSettings, taskSettings.ImageSettings.ImageFormat.GetDescription(), output); newFilePath = Path.Combine(taskSettings.GetScreenshotsFolder(), fileName); } newFilePath = ImageHelpers.SaveImageFileDialog(output, newFilePath); } return(newFilePath); }; form.CopyImageRequested += output => { Option.MainForm.Invoke(() => { using (output) { ClipboardHelpers.CopyImage(output); } }); }; form.UploadImageRequested += output => { Option.MainForm.Invoke(() => { //UploadManager.UploadImage(output); }); }; form.PrintImageRequested += output => { Option.MainForm.Invoke(() => { using (output) { PrintImage(output); } }); }; form.ShowDialog(); switch (form.Result) { case RegionResult.Close: // Esc case RegionResult.AnnotateCancelTask: return(null); case RegionResult.Region: // Enter case RegionResult.AnnotateRunAfterCaptureTasks: return(form.GetResultImage()); case RegionResult.Fullscreen: // Space or right click case RegionResult.AnnotateContinueTask: return((Bitmap)form.Canvas.Clone()); } } } } return(null); }