From b3c08dd890d1adfc77a9083b4e4c270f484c7d24 Mon Sep 17 00:00:00 2001 From: Kyriakos Ioannou Date: Thu, 28 Mar 2019 00:51:41 +0200 Subject: [PATCH] Modify snapshot method (#4) * When a snapshot is needeed, it's overkill to create the image on the filesystem, then delete it, and then create the image on the filesystem again. With this feature we can avoid this scenario and actually persist the created image on disk * Apply requested changes by author. Added unit test for persist snapshot Former-commit-id: c2a0c3b942ec65f0dda92a16fedf5a6e4715ed3f --- FFMpegCore.Test/VideoTest.cs | 30 +++++++++++++++++++++++++++--- FFMpegCore/FFMPEG/FFMpeg.cs | 5 +++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 9ed832a..242aacf 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -1,7 +1,6 @@ using FFMpegCore.Enums; using FFMpegCore.FFMPEG.Argument; using FFMpegCore.FFMPEG.Enums; -using FFMpegCore.Test; using FFMpegCore.Test.Resources; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; @@ -32,7 +31,8 @@ public bool Convert(VideoType type, bool multithreaded = false, VideoSize size = { Assert.AreEqual(outputVideo.Width, input.Width); Assert.AreEqual(outputVideo.Height, input.Height); - } else + } + else { Assert.AreNotEqual(outputVideo.Width, input.Width); Assert.AreNotEqual(outputVideo.Height, input.Height); @@ -90,7 +90,8 @@ public void Convert(VideoType type, ArgumentContainer container) { Assert.AreEqual(outputVideo.Width, input.Width); Assert.AreEqual(outputVideo.Height, input.Height); - } else + } + else { if (scaling.Value.Width != -1) { @@ -221,6 +222,29 @@ public void Video_Snapshot() } } + [TestMethod] + public void Video_Snapshot_PersistSnapshot() + { + var output = Input.OutputLocation(ImageType.Png); + try + { + var input = VideoInfo.FromFileInfo(Input); + + using (var bitmap = Encoder.Snapshot(input, output, persistSnapshotOnFileSystem: true)) + { + Assert.AreEqual(input.Width, bitmap.Width); + Assert.AreEqual(input.Height, bitmap.Height); + Assert.AreEqual(bitmap.RawFormat, ImageFormat.Png); + Assert.IsTrue(File.Exists(output.FullName)); + } + } + finally + { + if (File.Exists(output.FullName)) + File.Delete(output.FullName); + } + } + [TestMethod] public void Video_Join() { diff --git a/FFMpegCore/FFMPEG/FFMpeg.cs b/FFMpegCore/FFMPEG/FFMpeg.cs index ae428a4..0f12c3b 100644 --- a/FFMpegCore/FFMPEG/FFMpeg.cs +++ b/FFMpegCore/FFMPEG/FFMpeg.cs @@ -48,8 +48,9 @@ public FFMpeg(): base() /// Output video file /// Seek position where the thumbnail should be taken. /// Thumbnail size. If width or height equal 0, the other will be computed automatically. + /// By default, it deletes the created image on disk. If set to true, it won't delete the image /// Bitmap with the requested snapshot. - public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, TimeSpan? captureTime = null) + public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, TimeSpan? captureTime = null, bool persistSnapshotOnFileSystem = false) { if (captureTime == null) captureTime = TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3); @@ -106,7 +107,7 @@ public Bitmap Snapshot(VideoInfo source, FileInfo output, Size? size = null, Tim } } - if (output.Exists) + if (output.Exists && !persistSnapshotOnFileSystem) { output.Delete(); }