private static string ProcessTwoPassEncoding(string input, TaskMetadata metadata, TaskSettings taskSettings, bool deleteInputFile = true) { string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(taskSettings, metadata); string fileName = TaskHelpers.GetFileName(taskSettings, taskSettings.CaptureSettings.FFmpegOptions.Extension, metadata); string output = Path.Combine(screenshotsFolder, 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); }
private void UpdateScreenshotsFolderPathPreview() { try { lblSaveImageSubFolderPatternPreview.Text = TaskHelpers.GetScreenshotsFolder(); } catch (Exception e) { lblSaveImageSubFolderPatternPreview.Text = "Error: " + e.Message; } }
private void DoTextJobs() { if (Info.TaskSettings.AdvancedSettings.TextTaskSaveAsFile) { string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings); string filePath = TaskHelpers.HandleExistsFile(screenshotsFolder, Info.FileName, Info.TaskSettings); if (!string.IsNullOrEmpty(filePath)) { Info.FilePath = filePath; Helpers.CreateDirectoryFromFilePath(Info.FilePath); File.WriteAllText(Info.FilePath, Text, Encoding.UTF8); DebugHelper.WriteLine("Text saved to file: " + Info.FilePath); } } byte[] byteArray = Encoding.UTF8.GetBytes(Text); Data = new MemoryStream(byteArray); }
private bool DownloadFromURL(bool upload) { string url = Info.Result.URL.Trim(); Info.Result.URL = ""; string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings); Info.FilePath = TaskHelpers.HandleExistsFile(screenshotsFolder, Info.FileName, Info.TaskSettings); if (!string.IsNullOrEmpty(Info.FilePath)) { Info.Status = Resources.UploadTask_DownloadAndUpload_Downloading; OnStatusChanged(); try { Helpers.CreateDirectoryFromFilePath(Info.FilePath); using (WebClient wc = new WebClient()) { wc.Headers.Add(HttpRequestHeader.UserAgent, ShareXResources.UserAgent); wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy(); wc.DownloadFile(url, Info.FilePath); } if (upload) { LoadFileStream(); } return(true); } catch (Exception e) { DebugHelper.WriteException(e); MessageBox.Show(string.Format(Resources.UploadManager_DownloadAndUploadFile_Download_failed, e), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(false); }
public void AddWatchFolder(WatchFolderSettings watchFolderSetting, TaskSettings taskSettings) { if (!IsExist(watchFolderSetting)) { if (!taskSettings.WatchFolderList.Contains(watchFolderSetting)) { taskSettings.WatchFolderList.Add(watchFolderSetting); } WatchFolder watchFolder = new WatchFolder(); watchFolder.Settings = watchFolderSetting; watchFolder.TaskSettings = taskSettings; watchFolder.FileWatcherTrigger += origPath => { TaskSettings taskSettingsCopy = TaskSettings.GetSafeTaskSettings(taskSettings); string destPath = origPath; if (watchFolderSetting.MoveFilesToScreenshotsFolder) { string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(taskSettingsCopy); string fileName = Path.GetFileName(origPath); destPath = TaskHelpers.HandleExistsFile(screenshotsFolder, fileName, taskSettingsCopy); FileHelpers.CreateDirectoryFromFilePath(destPath); File.Move(origPath, destPath); } UploadManager.UploadFile(destPath, taskSettingsCopy); }; WatchFolders.Add(watchFolder); if (taskSettings.WatchFolderEnabled) { watchFolder.Enable(); } } }
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; TaskMetadata metadata = new TaskMetadata(); switch (startMethod) { case ScreenRecordStartMethod.Region: if (taskSettings.CaptureSettings.ScreenRecordTransparentRegion) { RegionCaptureTasks.GetRectangleRegionTransparent(out captureRectangle); } else { RegionCaptureTasks.GetRectangleRegion(out captureRectangle, out WindowInfo windowInfo, taskSettings.CaptureSettings.SurfaceOptions); metadata.UpdateInfo(windowInfo); } 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 screenshotsFolder = TaskHelpers.GetScreenshotsFolder(taskSettings, metadata); string fileName = TaskHelpers.GetFileName(taskSettings, extension, metadata); path = TaskHelpers.HandleExistsFile(screenshotsFolder, 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, metadata, 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(() => { if (!abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path) && TaskHelpers.ShowAfterCaptureForm(taskSettings, out string 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, metadata, taskSettings, customFileName); TaskManager.Start(task); } abortRequested = false; IsRecording = false; }); }
private bool DoAfterCaptureJobs() { if (Image == null) { return(true); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AddImageEffects)) { Image = TaskHelpers.ApplyImageEffects(Image, Info.TaskSettings.ImageSettingsReference); if (Image == null) { DebugHelper.WriteLine("Error: Applying image effects resulted empty image."); return(false); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AnnotateImage)) { Image = TaskHelpers.AnnotateImage(Image, null, Info.TaskSettings, true); if (Image == null) { return(false); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.CopyImageToClipboard)) { ClipboardHelpers.CopyImage(Image, Info.FileName); DebugHelper.WriteLine("Image copied to clipboard."); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SendImageToPrinter)) { TaskHelpers.PrintImage(Image); } Info.Metadata.Image = Image; if (Info.TaskSettings.AfterCaptureJob.HasFlagAny(AfterCaptureTasks.SaveImageToFile, AfterCaptureTasks.SaveImageToFileWithDialog, AfterCaptureTasks.DoOCR, AfterCaptureTasks.UploadImageToHost)) { ImageData imageData = TaskHelpers.PrepareImage(Image, Info.TaskSettings); Data = imageData.ImageStream; Info.FileName = Path.ChangeExtension(Info.FileName, imageData.ImageFormat.GetDescription()); if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveImageToFile)) { string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings, Info.Metadata); string filePath = TaskHelpers.HandleExistsFile(screenshotsFolder, Info.FileName, Info.TaskSettings); if (!string.IsNullOrEmpty(filePath)) { Info.FilePath = filePath; imageData.Write(Info.FilePath); DebugHelper.WriteLine("Image saved to file: " + Info.FilePath); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveImageToFileWithDialog)) { using (SaveFileDialog sfd = new SaveFileDialog()) { string initialDirectory = null; if (!string.IsNullOrEmpty(HelpersOptions.LastSaveDirectory) && Directory.Exists(HelpersOptions.LastSaveDirectory)) { initialDirectory = HelpersOptions.LastSaveDirectory; } else { initialDirectory = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings, Info.Metadata); } bool imageSaved; do { sfd.InitialDirectory = initialDirectory; sfd.FileName = Info.FileName; sfd.DefaultExt = Path.GetExtension(Info.FileName).Substring(1); sfd.Filter = string.Format("*{0}|*{0}|All files (*.*)|*.*", Path.GetExtension(Info.FileName)); sfd.Title = Resources.UploadTask_DoAfterCaptureJobs_Choose_a_folder_to_save + " " + Path.GetFileName(Info.FileName); if (sfd.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(sfd.FileName)) { Info.FilePath = sfd.FileName; HelpersOptions.LastSaveDirectory = Path.GetDirectoryName(Info.FilePath); imageSaved = imageData.Write(Info.FilePath); if (imageSaved) { DebugHelper.WriteLine("Image saved to file with dialog: " + Info.FilePath); } } else { break; } } while (!imageSaved); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveThumbnailImageToFile)) { string thumbnailFileName, thumbnailFolder; if (!string.IsNullOrEmpty(Info.FilePath)) { thumbnailFileName = Path.GetFileName(Info.FilePath); thumbnailFolder = Path.GetDirectoryName(Info.FilePath); } else { thumbnailFileName = Info.FileName; thumbnailFolder = TaskHelpers.GetScreenshotsFolder(Info.TaskSettings, Info.Metadata); } Info.ThumbnailFilePath = TaskHelpers.CreateThumbnail(Image, thumbnailFolder, thumbnailFileName, Info.TaskSettings); if (!string.IsNullOrEmpty(Info.ThumbnailFilePath)) { DebugHelper.WriteLine("Thumbnail saved to file: " + Info.ThumbnailFilePath); } } } return(true); }