Overload for 'Snapshot' with Input Stream & Output

This commit is contained in:
la 2023-06-09 05:58:52 -07:00
parent 6df9495e9f
commit b4deb22b53
2 changed files with 74 additions and 6 deletions

View file

@ -2,6 +2,7 @@
using FFMpegCore.Enums;
using FFMpegCore.Exceptions;
using FFMpegCore.Helpers;
using FFMpegCore.Pipes;
using Instances;
namespace FFMpegCore
@ -32,6 +33,27 @@ public static bool Snapshot(string input, string output, Size? size = null, Time
.OutputToFile(output, true, outputOptions)
.ProcessSynchronously();
}
/// <summary>
/// Saves a PNG thumbnail from the input video to the specified output stream.
/// </summary>
/// <param name="input">Input video stream</param>
/// <param name="output">Output stream to save the thumbnail</param>
/// <param name="size">Thumbnail size. If width or height is set to 0, the other dimension will be computed automatically.</param>
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
/// <param name="streamIndex">Index of the selected video stream. If not specified, the primary video stream or the first video stream will be selected.</param>
/// <param name="inputFileIndex">Index of the input file</param>
/// <returns>True if the snapshot was successfully created and saved; otherwise, false.</returns>
public static bool Snapshot(Stream input, Stream output, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
{
var source = FFProbe.Analyse(input);
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
return arguments
.OutputToPipe(new StreamPipeSink(output), outputOptions)
.ProcessSynchronously();
}
/// <summary>
/// Saves a 'png' thumbnail from the input video to drive
/// </summary>
@ -57,6 +79,26 @@ public static async Task<bool> SnapshotAsync(string input, string output, Size?
.ProcessAsynchronously();
}
/// <summary>
/// Saves a PNG thumbnail from the input video to the specified output stream.
/// </summary>
/// <param name="input">Input video stream</param>
/// <param name="output">Output stream to save the thumbnail</param>
/// <param name="size">Thumbnail size. If width or height is set to 0, the other dimension will be computed automatically.</param>
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
/// <param name="streamIndex">Index of the selected video stream. If not specified, the primary video stream or the first video stream will be selected.</param>
/// <param name="inputFileIndex">Index of the input file</param>
/// <returns>True if the snapshot was successfully created and saved; otherwise, false.</returns>
public static async Task<bool> SnapshotAsync(Stream input, Stream output, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
{
var source = await FFProbe.AnalyseAsync(input).ConfigureAwait(false);
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
return await arguments
.OutputToPipe(new StreamPipeSink(output), outputOptions)
.ProcessAsynchronously();
}
public static bool GifSnapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null, TimeSpan? duration = null, int? streamIndex = null)
{
if (Path.GetExtension(output)?.ToLower() != FileExtension.Gif)

View file

@ -2,6 +2,7 @@
using System.Drawing;
using FFMpegCore.Enums;
using FFMpegCore.Pipes;
namespace FFMpegCore;
@ -31,13 +32,38 @@ public static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) Bui
.Resize(size));
}
public static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(
Stream inputStream,
IMediaAnalysis source,
Size? size = null,
TimeSpan? captureTime = null,
int? streamIndex = null,
int inputFileIndex = 0)
{
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
size = PrepareSnapshotSize(source, size);
streamIndex ??= source.PrimaryVideoStream?.Index
?? source.VideoStreams.FirstOrDefault()?.Index
?? 0;
return (FFMpegArguments
.FromPipeInput(new StreamPipeSource(inputStream), options => options
.Seek(captureTime)),
options => options
.SelectStream((int)streamIndex, inputFileIndex)
.WithVideoCodec(VideoCodec.Png)
.WithFrameOutputCount(1)
.Resize(size)
);
}
public static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildGifSnapshotArguments(
string input,
IMediaAnalysis source,
Size? size = null,
TimeSpan? captureTime = null,
TimeSpan? duration = null,
int? streamIndex = null,
string input,
IMediaAnalysis source,
Size? size = null,
TimeSpan? captureTime = null,
TimeSpan? duration = null,
int? streamIndex = null,
int fps = 12)
{
var defaultGifOutputSize = new Size(480, -1);