From d4242771584a33027e055a45890a8e0d9fd5a892 Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 5 Aug 2015 14:44:04 +0300 Subject: [PATCH] Video thumbnailer auto upload support --- ShareX.MediaLib/Forms/VideoThumbnailerForm.cs | 35 ++++++++++++++----- ShareX.MediaLib/VideoThumbnailOptions.cs | 24 ++++++------- ShareX.MediaLib/VideoThumbnailer.cs | 25 +++++-------- ShareX/TaskHelpers.cs | 4 ++- 4 files changed, 51 insertions(+), 37 deletions(-) diff --git a/ShareX.MediaLib/Forms/VideoThumbnailerForm.cs b/ShareX.MediaLib/Forms/VideoThumbnailerForm.cs index b7fef3761..fc04d79da 100644 --- a/ShareX.MediaLib/Forms/VideoThumbnailerForm.cs +++ b/ShareX.MediaLib/Forms/VideoThumbnailerForm.cs @@ -32,12 +32,15 @@ You should have received a copy of the GNU General Public License using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Windows.Forms; namespace ShareX.MediaLib { public partial class VideoThumbnailerForm : Form { + public event Action UploadRequested; + public string FFmpegPath { get; set; } public VideoThumbnailOptions Options { get; set; } @@ -62,17 +65,25 @@ private void btnStart_Click(object sender, EventArgs e) pbProgress.Maximum = Options.ScreenshotCount; pbProgress.Visible = true; btnStart.Visible = false; - BackgroundWorker bw = new BackgroundWorker(); - bw.DoWork += (sender2, e2) => thumbnailer.TakeScreenshots(); - bw.RunWorkerCompleted += (sender3, e3) => + + new Thread(() => { - if (!IsDisposed) + List screenshots = thumbnailer.TakeScreenshots(); + + if (screenshots != null && screenshots.Count > 0) { - btnStart.Visible = true; - pbProgress.Visible = false; + this.InvokeSafe(() => + { + if (Options.UploadScreenshots) + { + screenshots.ForEach(x => OnUploadRequested(x.Filepath)); + } + + btnStart.Visible = true; + pbProgress.Visible = false; + }); } - }; - bw.RunWorkerAsync(); + }).Start(); } } @@ -81,6 +92,14 @@ private void Thumbnailer_ProgressChanged(int current, int length) this.InvokeSafe(() => pbProgress.Value = current); } + protected void OnUploadRequested(string filepath) + { + if (UploadRequested != null) + { + UploadRequested(filepath); + } + } + private void btnBrowse_Click(object sender, EventArgs e) { // TODO: Translate diff --git a/ShareX.MediaLib/VideoThumbnailOptions.cs b/ShareX.MediaLib/VideoThumbnailOptions.cs index d1b654a90..fa404a363 100644 --- a/ShareX.MediaLib/VideoThumbnailOptions.cs +++ b/ShareX.MediaLib/VideoThumbnailOptions.cs @@ -30,16 +30,16 @@ namespace ShareX.MediaLib { public class VideoThumbnailOptions { - [Category("Screenshots"), DefaultValue(ThumbnailLocationType.ParentFolder), Description("Create screenshots in the same folders as the media file, default torrent folder or in a custom folder.")] + [Category("Screenshots"), DefaultValue(ThumbnailLocationType.ParentFolder), Description("Create screenshots in the same folders as the media file, default screenshot folder or in a custom folder.")] public ThumbnailLocationType OutputLocation { get; set; } [Category("Screenshots"), DefaultValue(""), Description("Output folder where screenshots will get saved.")] - public string OutputDirectory { get; set; } + public string CustomOutputDirectory { get; set; } - [Category("Screenshots"), DefaultValue(EImageFormat.PNG), Description("FFmpeg thumbnail extension e.g. png or jpg.")] - public EImageFormat FFmpegThumbnailExtension { get; set; } + [Category("Screenshots"), DefaultValue(EImageFormat.PNG), Description("Thumbnail image format to save.")] + public EImageFormat ImageFormat { get; set; } - [Category("Screenshots"), DefaultValue(3), Description("Total number of screenshots to take.")] + [Category("Screenshots"), DefaultValue(9), Description("Total number of screenshots to take.")] public int ScreenshotCount { get; set; } [Category("Screenshots"), DefaultValue(false), Description("Choose random frame each time a media file is processed.")] @@ -48,16 +48,16 @@ public class VideoThumbnailOptions [Category("Screenshots"), DefaultValue(true), Description("Upload screenshots.")] public bool UploadScreenshots { get; set; } - [Category("Screenshots"), DefaultValue(true), Description("Keep or delete screenshots after processing files.")] + [Category("Screenshots"), DefaultValue(false), Description("After combine screenshots keep single image files.")] public bool KeepScreenshots { get; set; } - [Category("Screenshots"), DefaultValue(true), Description("After all screenshots taken open output directory automatically.")] + [Category("Screenshots"), DefaultValue(false), Description("After all screenshots taken open output directory automatically.")] public bool OpenDirectory { get; set; } - [Category("Screenshots"), DefaultValue(0), Description("Maximum thumbnail width size, 0 means don't resize.")] + [Category("Screenshots"), DefaultValue(512), Description("Maximum thumbnail width size, 0 means don't resize.")] public int MaxThumbnailWidth { get; set; } - [Category("Screenshots / Combined"), DefaultValue(false), Description("Combine all screenshots to one large screenshot.")] + [Category("Screenshots / Combined"), DefaultValue(true), Description("Combine all screenshots to one large screenshot.")] public bool CombineScreenshots { get; set; } [Category("Screenshots / Combined"), DefaultValue(20), Description("Space between border and content as pixel.")] @@ -66,11 +66,11 @@ public class VideoThumbnailOptions [Category("Screenshots / Combined"), DefaultValue(10), Description("Space between screenshots as pixel.")] public int Spacing { get; set; } - [Category("Screenshots / Combined"), DefaultValue(1), Description("Number of screenshots per row.")] + [Category("Screenshots / Combined"), DefaultValue(3), Description("Number of screenshots per row.")] public int ColumnCount { get; set; } - [Category("Screenshots / Combined"), DefaultValue(true), Description("Add movie information to the combined screenshot.")] - public bool AddMovieInfo { get; set; } + [Category("Screenshots / Combined"), DefaultValue(true), Description("Add video information to the combined screenshot.")] + public bool AddVideoInfo { get; set; } [Category("Screenshots / Combined"), DefaultValue(true), Description("Add timestamp of screenshot at corner of image.")] public bool AddTimestamp { get; set; } diff --git a/ShareX.MediaLib/VideoThumbnailer.cs b/ShareX.MediaLib/VideoThumbnailer.cs index 961c9d113..778bbdc34 100644 --- a/ShareX.MediaLib/VideoThumbnailer.cs +++ b/ShareX.MediaLib/VideoThumbnailer.cs @@ -74,7 +74,7 @@ public List TakeScreenshots() timeSliceElapsed = GetTimeSlice(Options.ScreenshotCount) * (i + 1); } - string filename = string.Format("{0}-{1}.{2}", mediaFileName, timeSliceElapsed, Options.FFmpegThumbnailExtension.GetDescription()); + string filename = string.Format("{0}-{1}.{2}", mediaFileName, timeSliceElapsed, Options.ImageFormat.GetDescription()); string tempScreenshotPath = Path.Combine(GetOutputDirectory(), filename); using (Process p = new Process()) @@ -113,22 +113,15 @@ private List Finish(List tempScreenshots { using (Image img = CombineScreenshots(tempScreenshots)) { - string tempFilepath = Path.Combine(GetOutputDirectory(), Path.GetFileNameWithoutExtension(MediaPath) + "_Thumbnail." + Options.FFmpegThumbnailExtension.GetDescription()); - - switch (Options.FFmpegThumbnailExtension) - { - case EImageFormat.PNG: - img.Save(tempFilepath, ImageFormat.Png); - break; - case EImageFormat.JPEG: - img.Save(tempFilepath, ImageFormat.Jpeg); - break; - } - + string tempFilepath = Path.Combine(GetOutputDirectory(), Path.GetFileNameWithoutExtension(MediaPath) + "_Thumbnail." + Options.ImageFormat.GetDescription()); + ImageHelpers.SaveImage(img, tempFilepath); screenshots.Add(new VideoThumbnailInfo(tempFilepath)); } - tempScreenshots.ForEach(x => File.Delete(x.Filepath)); + if (!Options.KeepScreenshots) + { + tempScreenshots.ForEach(x => File.Delete(x.Filepath)); + } } else { @@ -160,7 +153,7 @@ private string GetOutputDirectory() case ThumbnailLocationType.ParentFolder: return Path.GetDirectoryName(MediaPath); case ThumbnailLocationType.CustomFolder: - return Options.OutputDirectory; + return Options.CustomOutputDirectory; case ThumbnailLocationType.DefaultFolder: // TODO return ""; } @@ -194,7 +187,7 @@ private Image CombineScreenshots(List screenshots) string infoString = ""; int infoStringHeight = 0; - if (Options.AddMovieInfo) + if (Options.AddVideoInfo) { infoString = VideoInfo.ToString(); diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index ce0c112ba..77802b573 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -626,7 +626,9 @@ public static void OpenVideoThumbnailer(TaskSettings taskSettings = null) { if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings(); - new VideoThumbnailerForm(taskSettings.CaptureSettings.FFmpegOptions.CLIPath, Program.Settings.VideoThumbnailOptions).Show(); + VideoThumbnailerForm thumbnailerForm = new VideoThumbnailerForm(taskSettings.CaptureSettings.FFmpegOptions.CLIPath, Program.Settings.VideoThumbnailOptions); + thumbnailerForm.UploadRequested += filepath => UploadManager.UploadFile(filepath, taskSettings); + thumbnailerForm.Show(); } public static void OpenImageEditor(string filePath = null)