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: c2a0c3b942
This commit is contained in:
Kyriakos Ioannou 2019-03-28 00:51:41 +02:00 committed by Vlad Jerca
parent 7547c48927
commit b3c08dd890
2 changed files with 30 additions and 5 deletions

View file

@ -1,7 +1,6 @@
using FFMpegCore.Enums; using FFMpegCore.Enums;
using FFMpegCore.FFMPEG.Argument; using FFMpegCore.FFMPEG.Argument;
using FFMpegCore.FFMPEG.Enums; using FFMpegCore.FFMPEG.Enums;
using FFMpegCore.Test;
using FFMpegCore.Test.Resources; using FFMpegCore.Test.Resources;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic; using System.Collections.Generic;
@ -32,7 +31,8 @@ namespace FFMpegCore.Test
{ {
Assert.AreEqual(outputVideo.Width, input.Width); Assert.AreEqual(outputVideo.Width, input.Width);
Assert.AreEqual(outputVideo.Height, input.Height); Assert.AreEqual(outputVideo.Height, input.Height);
} else }
else
{ {
Assert.AreNotEqual(outputVideo.Width, input.Width); Assert.AreNotEqual(outputVideo.Width, input.Width);
Assert.AreNotEqual(outputVideo.Height, input.Height); Assert.AreNotEqual(outputVideo.Height, input.Height);
@ -90,7 +90,8 @@ namespace FFMpegCore.Test
{ {
Assert.AreEqual(outputVideo.Width, input.Width); Assert.AreEqual(outputVideo.Width, input.Width);
Assert.AreEqual(outputVideo.Height, input.Height); Assert.AreEqual(outputVideo.Height, input.Height);
} else }
else
{ {
if (scaling.Value.Width != -1) if (scaling.Value.Width != -1)
{ {
@ -221,6 +222,29 @@ namespace FFMpegCore.Test
} }
} }
[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] [TestMethod]
public void Video_Join() public void Video_Join()
{ {

View file

@ -48,8 +48,9 @@ namespace FFMpegCore.FFMPEG
/// <param name="output">Output video file</param> /// <param name="output">Output video file</param>
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param> /// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param> /// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
/// <param name="persistSnapshotOnFileSystem">By default, it deletes the created image on disk. If set to true, it won't delete the image</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
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) if (captureTime == null)
captureTime = TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3); captureTime = TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
@ -106,7 +107,7 @@ namespace FFMpegCore.FFMPEG
} }
} }
if (output.Exists) if (output.Exists && !persistSnapshotOnFileSystem)
{ {
output.Delete(); output.Delete();
} }