2023-02-02 21:19:45 +01:00
|
|
|
|
using System.Drawing;
|
2022-03-24 20:19:37 +01:00
|
|
|
|
using FFMpegCore.Pipes;
|
|
|
|
|
|
|
|
|
|
namespace FFMpegCore.Extensions.System.Drawing.Common
|
|
|
|
|
{
|
|
|
|
|
public static class FFMpegImage
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves a 'png' thumbnail to an in-memory bitmap
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Source 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="streamIndex">Selected video stream index.</param>
|
|
|
|
|
/// <param name="inputFileIndex">Input file index</param>
|
|
|
|
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
2023-02-16 09:37:01 +01:00
|
|
|
|
public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
|
2022-03-24 20:19:37 +01:00
|
|
|
|
{
|
|
|
|
|
var source = FFProbe.Analyse(input);
|
2023-01-29 22:17:40 +01:00
|
|
|
|
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
|
2022-03-24 20:19:37 +01:00
|
|
|
|
using var ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
arguments
|
|
|
|
|
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
|
|
|
|
|
.ForceFormat("rawvideo")))
|
|
|
|
|
.ProcessSynchronously();
|
|
|
|
|
|
|
|
|
|
ms.Position = 0;
|
2023-02-16 09:37:01 +01:00
|
|
|
|
using var bitmap = new Bitmap(ms);
|
|
|
|
|
return bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat);
|
2022-03-24 20:19:37 +01:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves a 'png' thumbnail to an in-memory bitmap
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="input">Source 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="streamIndex">Selected video stream index.</param>
|
|
|
|
|
/// <param name="inputFileIndex">Input file index</param>
|
|
|
|
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
2023-02-16 09:37:01 +01:00
|
|
|
|
public static async Task<Bitmap> SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
|
2022-03-24 20:19:37 +01:00
|
|
|
|
{
|
|
|
|
|
var source = await FFProbe.AnalyseAsync(input).ConfigureAwait(false);
|
2023-01-29 22:17:40 +01:00
|
|
|
|
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
|
2022-03-24 20:19:37 +01:00
|
|
|
|
using var ms = new MemoryStream();
|
|
|
|
|
|
|
|
|
|
await arguments
|
|
|
|
|
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
|
|
|
|
|
.ForceFormat("rawvideo")))
|
|
|
|
|
.ProcessAsynchronously();
|
|
|
|
|
|
|
|
|
|
ms.Position = 0;
|
2023-02-16 09:37:01 +01:00
|
|
|
|
return new Bitmap(ms);
|
2022-03-24 20:19:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-02 21:19:45 +01:00
|
|
|
|
}
|