Use map filtering

Use map filtering  doing snapshot
This commit is contained in:
Fedor Zhilkin 2020-09-17 20:56:28 +03:00
parent e60fb71ff8
commit 981b3294c5

View file

@ -20,13 +20,14 @@ public static class FFMpeg
/// <param name="output">Output video file path</param> /// <param name="output">Output video file path</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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static bool Snapshot(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null) public static bool Snapshot(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0)
{ {
if (Path.GetExtension(output) != FileExtension.Png) if (Path.GetExtension(output) != FileExtension.Png)
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png; output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
var arguments = BuildSnapshotArguments(source, size, captureTime); var arguments = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber);
return arguments return arguments
.OutputToFile(output) .OutputToFile(output)
@ -39,13 +40,14 @@ public static bool Snapshot(IMediaAnalysis source, string output, Size? size = n
/// <param name="output">Output video file path</param> /// <param name="output">Output video file path</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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static Task<bool> SnapshotAsync(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null) public static Task<bool> SnapshotAsync(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0)
{ {
if (Path.GetExtension(output) != FileExtension.Png) if (Path.GetExtension(output) != FileExtension.Png)
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png; output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
var arguments = BuildSnapshotArguments(source, size, captureTime); var arguments = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber);
return arguments return arguments
.OutputToFile(output) .OutputToFile(output)
@ -57,10 +59,11 @@ public static Task<bool> SnapshotAsync(IMediaAnalysis source, string output, Siz
/// <param name="source">Source video file.</param> /// <param name="source">Source 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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static Bitmap Snapshot(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null) public static Bitmap Snapshot(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0)
{ {
var arguments = BuildSnapshotArguments(source, size, captureTime); var arguments = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber);
using var ms = new MemoryStream(); using var ms = new MemoryStream();
arguments arguments
@ -77,10 +80,11 @@ public static Bitmap Snapshot(IMediaAnalysis source, Size? size = null, TimeSpan
/// <param name="source">Source video file.</param> /// <param name="source">Source 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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static async Task<Bitmap> SnapshotAsync(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null) public static async Task<Bitmap> SnapshotAsync(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0)
{ {
var arguments = BuildSnapshotArguments(source, size, captureTime); var arguments = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber);
using var ms = new MemoryStream(); using var ms = new MemoryStream();
await arguments await arguments
@ -92,13 +96,14 @@ await arguments
return new Bitmap(ms); return new Bitmap(ms);
} }
private static FFMpegArguments BuildSnapshotArguments(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null) private static FFMpegArguments BuildSnapshotArguments(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0)
{ {
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3); captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
size = PrepareSnapshotSize(source, size); size = PrepareSnapshotSize(source, size);
return FFMpegArguments return FFMpegArguments
.FromSeekedFiles((source.Path, captureTime ?? TimeSpan.Zero)) .FromSeekedFiles((source.Path, captureTime ?? TimeSpan.Zero))
.WithVideoStream(videoStreamNumber)
.WithVideoCodec(VideoCodec.Png) .WithVideoCodec(VideoCodec.Png)
.WithFrameOutputCount(1) .WithFrameOutputCount(1)
.Resize(size); .Resize(size);