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

View file

@ -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()
{

View file

@ -48,8 +48,9 @@ public FFMpeg(): base()
/// <param name="output">Output video file</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="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>
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();
}