From c30cf4c7ad87da71dcf3820ee35f5dabdc3d98ec Mon Sep 17 00:00:00 2001 From: Fedor Zhilkin Date: Tue, 3 Aug 2021 21:58:40 +0300 Subject: [PATCH] Style guide fixes and minor refactoring --- FFMpegCore/FFMpeg/FFMpeg.cs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/FFMpegCore/FFMpeg/FFMpeg.cs b/FFMpegCore/FFMpeg/FFMpeg.cs index 42c344b..14556b4 100644 --- a/FFMpegCore/FFMpeg/FFMpeg.cs +++ b/FFMpegCore/FFMpeg/FFMpeg.cs @@ -20,14 +20,15 @@ public static class FFMpeg /// Output video file path /// Seek position where the thumbnail should be taken. /// Thumbnail size. If width or height equal 0, the other will be computed automatically. + /// Selected video stream index. /// Bitmap with the requested snapshot. - public static bool Snapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null) + public static bool Snapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0) { if (Path.GetExtension(output) != FileExtension.Png) output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png; var source = FFProbe.Analyse(input); - var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime); + var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex); return arguments .OutputToFile(output, true, outputOptions) @@ -40,14 +41,15 @@ public static bool Snapshot(string input, string output, Size? size = null, Time /// Output video file path /// Seek position where the thumbnail should be taken. /// Thumbnail size. If width or height equal 0, the other will be computed automatically. + /// Selected video stream index. /// Bitmap with the requested snapshot. - public static async Task SnapshotAsync(string input, string output, Size? size = null, TimeSpan? captureTime = null) + public static async Task SnapshotAsync(string input, string output, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0) { if (Path.GetExtension(output) != FileExtension.Png) output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png; var source = await FFProbe.AnalyseAsync(input); - var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime); + var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex); return await arguments .OutputToFile(output, true, outputOptions) @@ -60,11 +62,12 @@ public static async Task SnapshotAsync(string input, string output, Size? /// Source video file. /// Seek position where the thumbnail should be taken. /// Thumbnail size. If width or height equal 0, the other will be computed automatically. + /// Selected video stream index. /// Bitmap with the requested snapshot. - public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null) + public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0) { var source = FFProbe.Analyse(input); - var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime); + var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex); using var ms = new MemoryStream(); arguments @@ -82,11 +85,12 @@ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? capture /// Source video file. /// Seek position where the thumbnail should be taken. /// Thumbnail size. If width or height equal 0, the other will be computed automatically. + /// Selected video stream index. /// Bitmap with the requested snapshot. - public static async Task SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null) + public static async Task SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0) { var source = await FFProbe.AnalyseAsync(input); - var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime); + var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex); using var ms = new MemoryStream(); await arguments @@ -98,15 +102,17 @@ await arguments return new Bitmap(ms); } - private static (FFMpegArguments, Action outputOptions) BuildSnapshotArguments(string input, IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null) + private static (FFMpegArguments, Action outputOptions) BuildSnapshotArguments(string input, IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0) { captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3); size = PrepareSnapshotSize(source, size); + var index = source.VideoStreams.FirstOrDefault(videoStream => videoStream.Index == streamIndex)?.Index; return (FFMpegArguments .FromFileInput(input, false, options => options .Seek(captureTime)), options => options + .SelectStream(index ?? 0) .WithVideoCodec(VideoCodec.Png) .WithFrameOutputCount(1) .Resize(size));