From c96fdc490a66e087ead22383e762a081365e5c8e Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 11:07:06 +0100 Subject: [PATCH 01/25] Updated package description for SkiaSharp instead of Aspose.Drawing --- FFMpegCore/FFMpegCore.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index 7c3f7bb..b08f00b 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -2,12 +2,12 @@ true - A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications + A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications. Uses SkiaSharp instead of System.Drawing.Common. 5.0.0 ffmpeg ffprobe convert video audio mediafile resize analyze muxing - Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev + Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev, Dimitri Vranken README.md From f464be430bd05ec70ca884e0b53dc5cbb97e96a1 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 11:25:45 +0100 Subject: [PATCH 02/25] Replaced System.Drawing.Common with SkiaSharp --- FFMpegCore.Examples/Program.cs | 3 +- .../BitmapExtensions.cs | 10 ++- .../BitmapVideoFrameWrapper.cs | 65 +++++-------------- ...re.Extensions.System.Drawing.Common.csproj | 2 +- .../FFMpegImage.cs | 11 ++-- 5 files changed, 34 insertions(+), 57 deletions(-) diff --git a/FFMpegCore.Examples/Program.cs b/FFMpegCore.Examples/Program.cs index ac4bce5..f926d94 100644 --- a/FFMpegCore.Examples/Program.cs +++ b/FFMpegCore.Examples/Program.cs @@ -3,6 +3,7 @@ using FFMpegCore.Enums; using FFMpegCore.Extensions.System.Drawing.Common; using FFMpegCore.Pipes; +using SkiaSharp; var inputPath = "/path/to/input"; var outputPath = "/path/to/output"; @@ -79,7 +80,7 @@ await FFMpegArguments FFMpeg.PosterWithAudio(inputPath, inputAudioPath, outputPath); // or #pragma warning disable CA1416 - using var image = Image.FromFile(inputImagePath); + using var image = SKBitmap.Decode(inputImagePath); image.AddAudio(inputAudioPath, outputPath); #pragma warning restore CA1416 } diff --git a/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs b/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs index 14cecaa..b8a0c83 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs +++ b/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs @@ -1,13 +1,17 @@ -using System.Drawing; +using SkiaSharp; namespace FFMpegCore.Extensions.System.Drawing.Common { public static class BitmapExtensions { - public static bool AddAudio(this Image poster, string audio, string output) + public static bool AddAudio(this SKBitmap poster, string audio, string output) { var destination = $"{Environment.TickCount}.png"; - poster.Save(destination); + using (var fileStream = File.OpenWrite(destination)) + { + poster.Encode(fileStream, SKEncodedImageFormat.Png, default); // PNG does not respect the quality parameter + } + try { return FFMpeg.PosterWithAudio(destination, audio, output); diff --git a/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs b/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs index 5462ca2..0439721 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs +++ b/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs @@ -1,7 +1,5 @@ -using System.Drawing; -using System.Drawing.Imaging; -using System.Runtime.InteropServices; -using FFMpegCore.Pipes; +using FFMpegCore.Pipes; +using SkiaSharp; namespace FFMpegCore.Extensions.System.Drawing.Common { @@ -13,44 +11,24 @@ public class BitmapVideoFrameWrapper : IVideoFrame, IDisposable public string Format { get; private set; } - public Bitmap Source { get; private set; } + public SKBitmap Source { get; private set; } - public BitmapVideoFrameWrapper(Bitmap bitmap) + public BitmapVideoFrameWrapper(SKBitmap bitmap) { Source = bitmap ?? throw new ArgumentNullException(nameof(bitmap)); - Format = ConvertStreamFormat(bitmap.PixelFormat); + Format = ConvertStreamFormat(bitmap.ColorType); } public void Serialize(Stream stream) { - var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat); - - try - { - var buffer = new byte[data.Stride * data.Height]; - Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); - stream.Write(buffer, 0, buffer.Length); - } - finally - { - Source.UnlockBits(data); - } + var data = Source.Bytes; + stream.Write(data, 0, data.Length); } public async Task SerializeAsync(Stream stream, CancellationToken token) { - var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat); - - try - { - var buffer = new byte[data.Stride * data.Height]; - Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); - await stream.WriteAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false); - } - finally - { - Source.UnlockBits(data); - } + var data = Source.Bytes; + await stream.WriteAsync(data, 0, data.Length, token).ConfigureAwait(false); } public void Dispose() @@ -58,27 +36,20 @@ public void Dispose() Source.Dispose(); } - private static string ConvertStreamFormat(PixelFormat fmt) + private static string ConvertStreamFormat(SKColorType fmt) { switch (fmt) { - case PixelFormat.Format16bppGrayScale: - return "gray16le"; - case PixelFormat.Format16bppRgb555: - return "bgr555le"; - case PixelFormat.Format16bppRgb565: - return "bgr565le"; - case PixelFormat.Format24bppRgb: - return "bgr24"; - case PixelFormat.Format32bppArgb: + case SKColorType.Gray8: + return "gray8"; + case SKColorType.Bgra8888: return "bgra"; - case PixelFormat.Format32bppPArgb: - //This is not really same as argb32 - return "argb"; - case PixelFormat.Format32bppRgb: + case SKColorType.Rgb888x: + return "rgb"; + case SKColorType.Rgba8888: return "rgba"; - case PixelFormat.Format48bppRgb: - return "rgb48le"; + case SKColorType.Rgb565: + return "rgb565"; default: throw new NotSupportedException($"Not supported pixel format {fmt}"); } diff --git a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj index aafb577..b0d6349 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj +++ b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj @@ -11,7 +11,7 @@ - + diff --git a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs index f36f83d..8cd0f26 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs +++ b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs @@ -1,5 +1,6 @@ using System.Drawing; using FFMpegCore.Pipes; +using SkiaSharp; namespace FFMpegCore.Extensions.System.Drawing.Common { @@ -14,7 +15,7 @@ public static class FFMpegImage /// Selected video stream index. /// Input file index /// Bitmap with the requested snapshot. - public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0) + public static SKBitmap Snapshot(string input, 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); @@ -26,8 +27,8 @@ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? capture .ProcessSynchronously(); ms.Position = 0; - using var bitmap = new Bitmap(ms); - return bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat); + using var bitmap = SKBitmap.Decode(ms); + return bitmap.Copy(); } /// /// Saves a 'png' thumbnail to an in-memory bitmap @@ -38,7 +39,7 @@ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? capture /// Selected video stream index. /// Input file index /// Bitmap with the requested snapshot. - public static async Task SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0) + public static async Task SnapshotAsync(string input, 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); @@ -50,7 +51,7 @@ await arguments .ProcessAsynchronously(); ms.Position = 0; - return new Bitmap(ms); + return SKBitmap.Decode(ms); } } } From 7f17d68a52fcc08863ff8f0c709fff72cf6f9c8f Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 11:39:03 +0100 Subject: [PATCH 03/25] Updated tests for SkiaSharp --- FFMpegCore.Test/Utilities/BitmapSources.cs | 11 ++--- FFMpegCore.Test/VideoTest.cs | 49 +++++++++++----------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/FFMpegCore.Test/Utilities/BitmapSources.cs b/FFMpegCore.Test/Utilities/BitmapSources.cs index b7ecb45..7e8d1a9 100644 --- a/FFMpegCore.Test/Utilities/BitmapSources.cs +++ b/FFMpegCore.Test/Utilities/BitmapSources.cs @@ -4,13 +4,14 @@ using System.Runtime.Versioning; using FFMpegCore.Extensions.System.Drawing.Common; using FFMpegCore.Pipes; +using SkiaSharp; namespace FFMpegCore.Test.Utilities { [SupportedOSPlatform("windows")] internal static class BitmapSource { - public static IEnumerable CreateBitmaps(int count, PixelFormat fmt, int w, int h) + public static IEnumerable CreateBitmaps(int count, SKColorType fmt, int w, int h) { for (var i = 0; i < count; i++) { @@ -21,9 +22,9 @@ public static IEnumerable CreateBitmaps(int count, PixelFormat fmt, } } - public static BitmapVideoFrameWrapper CreateVideoFrame(int index, PixelFormat fmt, int w, int h, float scaleNoise, float offset) + public static BitmapVideoFrameWrapper CreateVideoFrame(int index, SKColorType fmt, int w, int h, float scaleNoise, float offset) { - var bitmap = new Bitmap(w, h, fmt); + var bitmap = new SKBitmap(w, h, fmt, SKAlphaType.Opaque); offset = offset * index; @@ -36,9 +37,9 @@ public static BitmapVideoFrameWrapper CreateVideoFrame(int index, PixelFormat fm var nx = x * scaleNoise + offset; var ny = y * scaleNoise + offset; - var value = (int)((Perlin.Noise(nx, ny) + 1.0f) / 2.0f * 255); + var value = (byte)((Perlin.Noise(nx, ny) + 1.0f) / 2.0f * 255); - var color = Color.FromArgb((int)(value * xf), (int)(value * yf), value); + var color = new SKColor((byte)(value * xf), (byte)(value * yf), value); bitmap.SetPixel(x, y, color); } diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index e3e4b6b..23cc1b5 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -1,5 +1,4 @@ -using System.Drawing.Imaging; -using System.Runtime.Versioning; +using System.Runtime.Versioning; using System.Text; using FFMpegCore.Arguments; using FFMpegCore.Enums; @@ -9,6 +8,7 @@ using FFMpegCore.Test.Resources; using FFMpegCore.Test.Utilities; using Microsoft.VisualStudio.TestTools.UnitTesting; +using SkiaSharp; namespace FFMpegCore.Test { @@ -83,9 +83,9 @@ public void Video_ToH265_MKV_Args() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(10000)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] - public void Video_ToMP4_Args_Pipe(System.Drawing.Imaging.PixelFormat pixelFormat) + [DataRow(SKColorType.Rgb565)] + [DataRow(SKColorType.Bgra8888)] + public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -106,8 +106,8 @@ public void Video_ToMP4_Args_Pipe_DifferentImageSizes() var frames = new List { - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 256, 256, 1, 0) + BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 256, 256, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -126,8 +126,8 @@ public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() var frames = new List { - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 256, 256, 1, 0) + BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 256, 256, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -146,8 +146,8 @@ public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() var frames = new List { - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format32bppRgb, 255, 255, 1, 0) + BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, SKColorType.Bgra8888, 255, 255, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -166,8 +166,8 @@ public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async() var frames = new List { - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, System.Drawing.Imaging.PixelFormat.Format32bppRgb, 255, 255, 1, 0) + BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, SKColorType.Bgra8888, 255, 255, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -313,9 +313,9 @@ public void Video_ToTS_Args() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(10000)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] - public async Task Video_ToTS_Args_Pipe(System.Drawing.Imaging.PixelFormat pixelFormat) + [DataRow(SKColorType.Rgb565)] + [DataRow(SKColorType.Bgra8888)] + public async Task Video_ToTS_Args_Pipe(SKColorType pixelFormat) { using var output = new TemporaryFile($"out{VideoType.Ts.Extension}"); var input = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); @@ -346,10 +346,9 @@ public async Task Video_ToOGV_Resize() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(10000)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format48bppRgb)] - public void RawVideoPipeSource_Ogv_Scale(System.Drawing.Imaging.PixelFormat pixelFormat) + [DataRow(SKColorType.Rgb565)] + [DataRow(SKColorType.Bgra8888)] + public void RawVideoPipeSource_Ogv_Scale(SKColorType pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Ogv.Extension}"); var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); @@ -382,10 +381,10 @@ public void Scale_Mp4_Multithreaded() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(10000)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] - [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] + [DataRow(SKColorType.Rgb565)] + [DataRow(SKColorType.Bgra8888)] // [DataRow(PixelFormat.Format48bppRgb)] - public void Video_ToMP4_Resize_Args_Pipe(System.Drawing.Imaging.PixelFormat pixelFormat) + public void Video_ToMP4_Resize_Args_Pipe(SKColorType pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); @@ -407,7 +406,7 @@ public void Video_Snapshot_InMemory() var input = FFProbe.Analyse(TestResources.Mp4Video); Assert.AreEqual(input.PrimaryVideoStream!.Width, bitmap.Width); Assert.AreEqual(input.PrimaryVideoStream.Height, bitmap.Height); - Assert.AreEqual(bitmap.RawFormat, ImageFormat.Png); + Assert.AreEqual(bitmap.ColorType, SKColorType.Bgra8888); } [TestMethod, Timeout(10000)] @@ -568,7 +567,7 @@ public void Video_TranscodeInMemory() { using var resStream = new MemoryStream(); var reader = new StreamPipeSink(resStream); - var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, System.Drawing.Imaging.PixelFormat.Format24bppRgb, 128, 128)); + var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, SKColorType.Rgb565, 128, 128)); FFMpegArguments .FromPipeInput(writer) From 72c76c20f0a7e8aafbdf008cc782b784e91f4461 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 11:39:34 +0100 Subject: [PATCH 04/25] refactor: Centralized test timeout duration Changes can be made in a single place if neeeded --- FFMpegCore.Test/VideoTest.cs | 84 ++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 23cc1b5..dd205e0 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -15,7 +15,9 @@ namespace FFMpegCore.Test [TestClass] public class VideoTest { - [TestMethod, Timeout(10000)] + private const int BaseTimeoutMilliseconds = 10_000; + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToOGV() { using var outputFile = new TemporaryFile($"out{VideoType.Ogv.Extension}"); @@ -27,7 +29,7 @@ public void Video_ToOGV() Assert.IsTrue(success); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToMP4() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -39,7 +41,7 @@ public void Video_ToMP4() Assert.IsTrue(success); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToMP4_YUV444p() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -55,7 +57,7 @@ public void Video_ToMP4_YUV444p() Assert.IsTrue(analysis.VideoStreams.First().PixelFormat == "yuv444p"); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToMP4_Args() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -68,7 +70,7 @@ public void Video_ToMP4_Args() Assert.IsTrue(success); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToH265_MKV_Args() { using var outputFile = new TemporaryFile($"out.mkv"); @@ -82,7 +84,7 @@ public void Video_ToH265_MKV_Args() } [SupportedOSPlatform("windows")] - [WindowsOnlyDataTestMethod, Timeout(10000)] + [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] [DataRow(SKColorType.Rgb565)] [DataRow(SKColorType.Bgra8888)] public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat) @@ -99,7 +101,7 @@ public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat) } [SupportedOSPlatform("windows")] - [WindowsOnlyTestMethod, Timeout(10000)] + [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToMP4_Args_Pipe_DifferentImageSizes() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -119,7 +121,7 @@ public void Video_ToMP4_Args_Pipe_DifferentImageSizes() } [SupportedOSPlatform("windows")] - [WindowsOnlyTestMethod, Timeout(10000)] + [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -139,7 +141,7 @@ public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() } [SupportedOSPlatform("windows")] - [WindowsOnlyTestMethod, Timeout(10000)] + [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -159,7 +161,7 @@ public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() } [SupportedOSPlatform("windows")] - [WindowsOnlyTestMethod, Timeout(10000)] + [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -178,7 +180,7 @@ public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async() .ProcessAsynchronously()); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToMP4_Args_StreamPipe() { using var input = File.OpenRead(TestResources.WebmVideo); @@ -192,7 +194,7 @@ public void Video_ToMP4_Args_StreamPipe() Assert.IsTrue(success); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_ToMP4_Args_StreamOutputPipe_Async_Failure() { await Assert.ThrowsExceptionAsync(async () => @@ -206,7 +208,7 @@ await FFMpegArguments }); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_StreamFile_OutputToMemoryStream() { var output = new MemoryStream(); @@ -223,7 +225,7 @@ public void Video_StreamFile_OutputToMemoryStream() Console.WriteLine(result.Duration); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToMP4_Args_StreamOutputPipe_Failure() { Assert.ThrowsException(() => @@ -237,7 +239,7 @@ public void Video_ToMP4_Args_StreamOutputPipe_Failure() }); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_ToMP4_Args_StreamOutputPipe_Async() { await using var ms = new MemoryStream(); @@ -250,7 +252,7 @@ await FFMpegArguments .ProcessAsynchronously(); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task TestDuplicateRun() { FFMpegArguments @@ -266,7 +268,7 @@ await FFMpegArguments File.Delete("temporary.mp4"); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void TranscodeToMemoryStream_Success() { using var output = new MemoryStream(); @@ -284,7 +286,7 @@ public void TranscodeToMemoryStream_Success() Assert.AreEqual(inputAnalysis.Duration.TotalSeconds, outputAnalysis.Duration.TotalSeconds, 0.3); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToTS() { using var outputFile = new TemporaryFile($"out{VideoType.MpegTs.Extension}"); @@ -296,7 +298,7 @@ public void Video_ToTS() Assert.IsTrue(success); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToTS_Args() { using var outputFile = new TemporaryFile($"out{VideoType.MpegTs.Extension}"); @@ -312,7 +314,7 @@ public void Video_ToTS_Args() } [SupportedOSPlatform("windows")] - [WindowsOnlyDataTestMethod, Timeout(10000)] + [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] [DataRow(SKColorType.Rgb565)] [DataRow(SKColorType.Bgra8888)] public async Task Video_ToTS_Args_Pipe(SKColorType pixelFormat) @@ -331,7 +333,7 @@ public async Task Video_ToTS_Args_Pipe(SKColorType pixelFormat) Assert.AreEqual(VideoType.Ts.Name, analysis.Format.FormatName); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_ToOGV_Resize() { using var outputFile = new TemporaryFile($"out{VideoType.Ogv.Extension}"); @@ -345,7 +347,7 @@ public async Task Video_ToOGV_Resize() } [SupportedOSPlatform("windows")] - [WindowsOnlyDataTestMethod, Timeout(10000)] + [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] [DataRow(SKColorType.Rgb565)] [DataRow(SKColorType.Bgra8888)] public void RawVideoPipeSource_Ogv_Scale(SKColorType pixelFormat) @@ -365,7 +367,7 @@ public void RawVideoPipeSource_Ogv_Scale(SKColorType pixelFormat) Assert.AreEqual((int)VideoSize.Ed, analysis.PrimaryVideoStream!.Width); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Scale_Mp4_Multithreaded() { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -380,7 +382,7 @@ public void Scale_Mp4_Multithreaded() } [SupportedOSPlatform("windows")] - [WindowsOnlyDataTestMethod, Timeout(10000)] + [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] [DataRow(SKColorType.Rgb565)] [DataRow(SKColorType.Bgra8888)] // [DataRow(PixelFormat.Format48bppRgb)] @@ -398,7 +400,7 @@ public void Video_ToMP4_Resize_Args_Pipe(SKColorType pixelFormat) } [SupportedOSPlatform("windows")] - [WindowsOnlyTestMethod, Timeout(10000)] + [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_Snapshot_InMemory() { using var bitmap = FFMpegImage.Snapshot(TestResources.Mp4Video); @@ -409,7 +411,7 @@ public void Video_Snapshot_InMemory() Assert.AreEqual(bitmap.ColorType, SKColorType.Bgra8888); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_Snapshot_PersistSnapshot() { var outputPath = new TemporaryFile("out.png"); @@ -423,7 +425,7 @@ public void Video_Snapshot_PersistSnapshot() Assert.AreEqual("png", analysis.PrimaryVideoStream!.CodecName); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_Join() { var inputCopy = new TemporaryFile("copy-input.mp4"); @@ -445,7 +447,7 @@ public void Video_Join() Assert.AreEqual(input.PrimaryVideoStream.Width, result.PrimaryVideoStream.Width); } - [TestMethod, Timeout(20000)] + [TestMethod, Timeout(2 * BaseTimeoutMilliseconds)] public void Video_Join_Image_Sequence() { var imageSet = new List(); @@ -470,7 +472,7 @@ public void Video_Join_Image_Sequence() Assert.AreEqual(imageAnalysis.PrimaryVideoStream!.Height, result.PrimaryVideoStream.Height); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_With_Only_Audio_Should_Extract_Metadata() { var video = FFProbe.Analyse(TestResources.Mp4WithoutVideo); @@ -479,7 +481,7 @@ public void Video_With_Only_Audio_Should_Extract_Metadata() Assert.AreEqual(10, video.Duration.TotalSeconds, 0.5); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_Duration() { var video = FFProbe.Analyse(TestResources.Mp4Video); @@ -499,7 +501,7 @@ public void Video_Duration() Assert.AreEqual(video.Duration.Seconds - 2, outputVideo.Duration.Seconds); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_UpdatesProgress() { var outputFile = new TemporaryFile("out.mp4"); @@ -540,7 +542,7 @@ void OnTimeProgess(TimeSpan time) Assert.AreNotEqual(analysis.Duration, timeDone); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_OutputsData() { var outputFile = new TemporaryFile("out.mp4"); @@ -562,7 +564,7 @@ public void Video_OutputsData() } [SupportedOSPlatform("windows")] - [WindowsOnlyTestMethod, Timeout(10000)] + [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_TranscodeInMemory() { using var resStream = new MemoryStream(); @@ -582,7 +584,7 @@ public void Video_TranscodeInMemory() Assert.AreEqual(vi.PrimaryVideoStream.Height, 128); } - [TestMethod, Timeout(20000)] + [TestMethod, Timeout(2 * BaseTimeoutMilliseconds)] public void Video_TranscodeToMemory() { using var memoryStream = new MemoryStream(); @@ -600,7 +602,7 @@ public void Video_TranscodeToMemory() Assert.AreEqual(vi.PrimaryVideoStream.Height, 360); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_Cancel_Async() { var outputFile = new TemporaryFile("out.mp4"); @@ -624,7 +626,7 @@ public async Task Video_Cancel_Async() Assert.IsFalse(result); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_Cancel() { var outputFile = new TemporaryFile("out.mp4"); @@ -645,7 +647,7 @@ public void Video_Cancel() Assert.IsFalse(result); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_Cancel_Async_With_Timeout() { var outputFile = new TemporaryFile("out.mp4"); @@ -675,7 +677,7 @@ public async Task Video_Cancel_Async_With_Timeout() Assert.AreEqual("aac", outputInfo.PrimaryAudioStream!.CodecName); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_Cancel_CancellationToken_Async() { var outputFile = new TemporaryFile("out.mp4"); @@ -700,7 +702,7 @@ public async Task Video_Cancel_CancellationToken_Async() Assert.IsFalse(result); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_Cancel_CancellationToken_Async_Throws() { var outputFile = new TemporaryFile("out.mp4"); @@ -723,7 +725,7 @@ public async Task Video_Cancel_CancellationToken_Async_Throws() await Assert.ThrowsExceptionAsync(() => task); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_Cancel_CancellationToken_Throws() { var outputFile = new TemporaryFile("out.mp4"); @@ -745,7 +747,7 @@ public void Video_Cancel_CancellationToken_Throws() Assert.ThrowsException(() => task.ProcessSynchronously()); } - [TestMethod, Timeout(10000)] + [TestMethod, Timeout(BaseTimeoutMilliseconds)] public async Task Video_Cancel_CancellationToken_Async_With_Timeout() { var outputFile = new TemporaryFile("out.mp4"); From 6e38b45445847ce272c1253e52722992affe7569 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 11:40:11 +0100 Subject: [PATCH 05/25] chore: Increased test timeout duration Required for successful test execution on my rather slow machine --- FFMpegCore.Test/VideoTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index dd205e0..930cd99 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -15,7 +15,7 @@ namespace FFMpegCore.Test [TestClass] public class VideoTest { - private const int BaseTimeoutMilliseconds = 10_000; + private const int BaseTimeoutMilliseconds = 60_000; [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToOGV() From f95bba5aa2aef925455b045e8dec4a603c991e35 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 11:54:41 +0100 Subject: [PATCH 06/25] Added configuration for running tests in WSL on windows See https://learn.microsoft.com/en-us/visualstudio/test/remote-testing?view=vs-2022 --- testenvironments.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 testenvironments.json diff --git a/testenvironments.json b/testenvironments.json new file mode 100644 index 0000000..14b2763 --- /dev/null +++ b/testenvironments.json @@ -0,0 +1,10 @@ +{ + "version": "1", + "environments": [ + { + "name": "Ubuntu", + "type": "wsl", + "wslDistribution": "Ubuntu" + } + ] +} \ No newline at end of file From bdfe87be163032d346117f4836e824da3a53d72e Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 12:09:35 +0100 Subject: [PATCH 07/25] Enabled windows-only tests on all plattforms They are now also supported on Linux because we are using SkiaSharp instead of System.Drawing.Common --- .../Utilities/WindowsOnlyDataTestMethod.cs | 24 +++++++++---------- .../Utilities/WindowsOnlyTestMethod.cs | 24 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs b/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs index 84a779a..e39921a 100644 --- a/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs +++ b/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs @@ -1,5 +1,4 @@ -using System.Runtime.InteropServices; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FFMpegCore.Test.Utilities; @@ -7,16 +6,17 @@ public class WindowsOnlyDataTestMethod : DataTestMethodAttribute { public override TestResult[] Execute(ITestMethod testMethod) { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - var message = $"Test not executed on other platforms than Windows"; - { - return new[] - { - new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } - }; - } - } + // Commented out because this edition of FFMpegCore fully supports Linux + //if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + //{ + // var message = $"Test not executed on other platforms than Windows"; + // { + // return new[] + // { + // new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } + // }; + // } + //} return base.Execute(testMethod); } diff --git a/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs b/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs index 7e817bf..5143194 100644 --- a/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs +++ b/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs @@ -1,5 +1,4 @@ -using System.Runtime.InteropServices; -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FFMpegCore.Test.Utilities; @@ -7,16 +6,17 @@ public class WindowsOnlyTestMethod : TestMethodAttribute { public override TestResult[] Execute(ITestMethod testMethod) { - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - var message = $"Test not executed on other platforms than Windows"; - { - return new[] - { - new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } - }; - } - } + // Commented out because this edition of FFMpegCore fully supports Linux + //if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + //{ + // var message = $"Test not executed on other platforms than Windows"; + // { + // return new[] + // { + // new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } + // }; + // } + //} return base.Execute(testMethod); } From 29fc9246d11fb0b570296825d51e69f4b517ce1c Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 12:11:03 +0100 Subject: [PATCH 08/25] Added dependency SkiaSharp.NativeAssets.Linux.NoDependencies Required for execution on Linux, otherwise we get the following error: Unable to load shared library 'libSkiaSharp' or one of its dependencies See https://github.com/mono/SkiaSharp/issues/1341 for more information --- .../FFMpegCore.Extensions.System.Drawing.Common.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj index b0d6349..25e820a 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj +++ b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj @@ -12,6 +12,7 @@ + From fb19f453318df7bf0d442536b05c1387389ad0ec Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 12:22:36 +0100 Subject: [PATCH 09/25] Updated package properties for fork --- FFMpegCore/FFMpegCore.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index b08f00b..a30bf76 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -9,6 +9,9 @@ ffmpeg ffprobe convert video audio mediafile resize analyze muxing Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev, Dimitri Vranken README.md + FFMpegCore.SkiaSharp + https://github.com/drasive/FFMpegCore + https://github.com/drasive/FFMpegCore From db215ff4c7c70b51d58fbd25684572de4415c8d9 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 12:23:07 +0100 Subject: [PATCH 10/25] Revert "Updated package description for SkiaSharp instead of Aspose.Drawing" This reverts commit c96fdc490a66e087ead22383e762a081365e5c8e. --- FFMpegCore/FFMpegCore.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index a30bf76..1bb2305 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -2,12 +2,12 @@ true - A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications. Uses SkiaSharp instead of System.Drawing.Common. + A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications 5.0.0 ffmpeg ffprobe convert video audio mediafile resize analyze muxing - Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev, Dimitri Vranken + Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev README.md FFMpegCore.SkiaSharp https://github.com/drasive/FFMpegCore From 8afe1e0c9eca7fdf17196d53e796c5e254ca0493 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 13 Feb 2023 12:23:12 +0100 Subject: [PATCH 11/25] Revert "Updated package properties for fork" This reverts commit fb19f453318df7bf0d442536b05c1387389ad0ec. --- FFMpegCore/FFMpegCore.csproj | 3 --- 1 file changed, 3 deletions(-) diff --git a/FFMpegCore/FFMpegCore.csproj b/FFMpegCore/FFMpegCore.csproj index 1bb2305..7c3f7bb 100644 --- a/FFMpegCore/FFMpegCore.csproj +++ b/FFMpegCore/FFMpegCore.csproj @@ -9,9 +9,6 @@ ffmpeg ffprobe convert video audio mediafile resize analyze muxing Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev README.md - FFMpegCore.SkiaSharp - https://github.com/drasive/FFMpegCore - https://github.com/drasive/FFMpegCore From cc22d15061ac62922ccb120729864aa5a4f1619a Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Thu, 16 Feb 2023 09:37:01 +0100 Subject: [PATCH 12/25] Moved SkiaSharp implemented to its own extension --- .../BitmapExtensions.cs | 28 ++++++++ .../BitmapVideoFrameWrapper.cs | 58 +++++++++++++++++ .../FFMpegCore.Extensions.SkiaSharp.csproj | 22 +++++++ .../FFMpegImage.cs | 57 ++++++++++++++++ .../BitmapExtensions.cs | 10 +-- .../BitmapVideoFrameWrapper.cs | 65 ++++++++++++++----- ...re.Extensions.System.Drawing.Common.csproj | 3 +- .../FFMpegImage.cs | 11 ++-- FFMpegCore.sln | 8 ++- 9 files changed, 228 insertions(+), 34 deletions(-) create mode 100644 FFMpegCore.Extensions.SkiaSharp/BitmapExtensions.cs create mode 100644 FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs create mode 100644 FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj create mode 100644 FFMpegCore.Extensions.SkiaSharp/FFMpegImage.cs diff --git a/FFMpegCore.Extensions.SkiaSharp/BitmapExtensions.cs b/FFMpegCore.Extensions.SkiaSharp/BitmapExtensions.cs new file mode 100644 index 0000000..34e303a --- /dev/null +++ b/FFMpegCore.Extensions.SkiaSharp/BitmapExtensions.cs @@ -0,0 +1,28 @@ +using SkiaSharp; + +namespace FFMpegCore.Extensions.SkiaSharp +{ + public static class BitmapExtensions + { + public static bool AddAudio(this SKBitmap poster, string audio, string output) + { + var destination = $"{Environment.TickCount}.png"; + using (var fileStream = File.OpenWrite(destination)) + { + poster.Encode(fileStream, SKEncodedImageFormat.Png, default); // PNG does not respect the quality parameter + } + + try + { + return FFMpeg.PosterWithAudio(destination, audio, output); + } + finally + { + if (File.Exists(destination)) + { + File.Delete(destination); + } + } + } + } +} diff --git a/FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs b/FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs new file mode 100644 index 0000000..2556883 --- /dev/null +++ b/FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs @@ -0,0 +1,58 @@ +using FFMpegCore.Pipes; +using SkiaSharp; + +namespace FFMpegCore.Extensions.SkiaSharp +{ + public class BitmapVideoFrameWrapper : IVideoFrame, IDisposable + { + public int Width => Source.Width; + + public int Height => Source.Height; + + public string Format { get; private set; } + + public SKBitmap Source { get; private set; } + + public BitmapVideoFrameWrapper(SKBitmap bitmap) + { + Source = bitmap ?? throw new ArgumentNullException(nameof(bitmap)); + Format = ConvertStreamFormat(bitmap.ColorType); + } + + public void Serialize(Stream stream) + { + var data = Source.Bytes; + stream.Write(data, 0, data.Length); + } + + public async Task SerializeAsync(Stream stream, CancellationToken token) + { + var data = Source.Bytes; + await stream.WriteAsync(data, 0, data.Length, token).ConfigureAwait(false); + } + + public void Dispose() + { + Source.Dispose(); + } + + private static string ConvertStreamFormat(SKColorType fmt) + { + switch (fmt) + { + case SKColorType.Gray8: + return "gray8"; + case SKColorType.Bgra8888: + return "bgra"; + case SKColorType.Rgb888x: + return "rgb"; + case SKColorType.Rgba8888: + return "rgba"; + case SKColorType.Rgb565: + return "rgb565"; + default: + throw new NotSupportedException($"Not supported pixel format {fmt}"); + } + } + } +} diff --git a/FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj b/FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj new file mode 100644 index 0000000..25e820a --- /dev/null +++ b/FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj @@ -0,0 +1,22 @@ + + + + true + Image extension for FFMpegCore using System.Common.Drawing + 5.0.0 + + + ffmpeg ffprobe convert video audio mediafile resize analyze muxing + Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev + + + + + + + + + + + + diff --git a/FFMpegCore.Extensions.SkiaSharp/FFMpegImage.cs b/FFMpegCore.Extensions.SkiaSharp/FFMpegImage.cs new file mode 100644 index 0000000..69929d3 --- /dev/null +++ b/FFMpegCore.Extensions.SkiaSharp/FFMpegImage.cs @@ -0,0 +1,57 @@ +using System.Drawing; +using FFMpegCore.Pipes; +using SkiaSharp; + +namespace FFMpegCore.Extensions.SkiaSharp +{ + public static class FFMpegImage + { + /// + /// Saves a 'png' thumbnail to an in-memory bitmap + /// + /// 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. + /// Input file index + /// Bitmap with the requested snapshot. + public static SKBitmap Snapshot(string input, 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); + using var ms = new MemoryStream(); + + arguments + .OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options + .ForceFormat("rawvideo"))) + .ProcessSynchronously(); + + ms.Position = 0; + using var bitmap = SKBitmap.Decode(ms); + return bitmap.Copy(); + } + /// + /// Saves a 'png' thumbnail to an in-memory bitmap + /// + /// 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. + /// Input file index + /// Bitmap with the requested snapshot. + public static async Task SnapshotAsync(string input, 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); + using var ms = new MemoryStream(); + + await arguments + .OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options + .ForceFormat("rawvideo"))) + .ProcessAsynchronously(); + + ms.Position = 0; + return SKBitmap.Decode(ms); + } + } +} diff --git a/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs b/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs index b8a0c83..14cecaa 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs +++ b/FFMpegCore.Extensions.System.Drawing.Common/BitmapExtensions.cs @@ -1,17 +1,13 @@ -using SkiaSharp; +using System.Drawing; namespace FFMpegCore.Extensions.System.Drawing.Common { public static class BitmapExtensions { - public static bool AddAudio(this SKBitmap poster, string audio, string output) + public static bool AddAudio(this Image poster, string audio, string output) { var destination = $"{Environment.TickCount}.png"; - using (var fileStream = File.OpenWrite(destination)) - { - poster.Encode(fileStream, SKEncodedImageFormat.Png, default); // PNG does not respect the quality parameter - } - + poster.Save(destination); try { return FFMpeg.PosterWithAudio(destination, audio, output); diff --git a/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs b/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs index 0439721..5462ca2 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs +++ b/FFMpegCore.Extensions.System.Drawing.Common/BitmapVideoFrameWrapper.cs @@ -1,5 +1,7 @@ -using FFMpegCore.Pipes; -using SkiaSharp; +using System.Drawing; +using System.Drawing.Imaging; +using System.Runtime.InteropServices; +using FFMpegCore.Pipes; namespace FFMpegCore.Extensions.System.Drawing.Common { @@ -11,24 +13,44 @@ public class BitmapVideoFrameWrapper : IVideoFrame, IDisposable public string Format { get; private set; } - public SKBitmap Source { get; private set; } + public Bitmap Source { get; private set; } - public BitmapVideoFrameWrapper(SKBitmap bitmap) + public BitmapVideoFrameWrapper(Bitmap bitmap) { Source = bitmap ?? throw new ArgumentNullException(nameof(bitmap)); - Format = ConvertStreamFormat(bitmap.ColorType); + Format = ConvertStreamFormat(bitmap.PixelFormat); } public void Serialize(Stream stream) { - var data = Source.Bytes; - stream.Write(data, 0, data.Length); + var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat); + + try + { + var buffer = new byte[data.Stride * data.Height]; + Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); + stream.Write(buffer, 0, buffer.Length); + } + finally + { + Source.UnlockBits(data); + } } public async Task SerializeAsync(Stream stream, CancellationToken token) { - var data = Source.Bytes; - await stream.WriteAsync(data, 0, data.Length, token).ConfigureAwait(false); + var data = Source.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, Source.PixelFormat); + + try + { + var buffer = new byte[data.Stride * data.Height]; + Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); + await stream.WriteAsync(buffer, 0, buffer.Length, token).ConfigureAwait(false); + } + finally + { + Source.UnlockBits(data); + } } public void Dispose() @@ -36,20 +58,27 @@ public void Dispose() Source.Dispose(); } - private static string ConvertStreamFormat(SKColorType fmt) + private static string ConvertStreamFormat(PixelFormat fmt) { switch (fmt) { - case SKColorType.Gray8: - return "gray8"; - case SKColorType.Bgra8888: + case PixelFormat.Format16bppGrayScale: + return "gray16le"; + case PixelFormat.Format16bppRgb555: + return "bgr555le"; + case PixelFormat.Format16bppRgb565: + return "bgr565le"; + case PixelFormat.Format24bppRgb: + return "bgr24"; + case PixelFormat.Format32bppArgb: return "bgra"; - case SKColorType.Rgb888x: - return "rgb"; - case SKColorType.Rgba8888: + case PixelFormat.Format32bppPArgb: + //This is not really same as argb32 + return "argb"; + case PixelFormat.Format32bppRgb: return "rgba"; - case SKColorType.Rgb565: - return "rgb565"; + case PixelFormat.Format48bppRgb: + return "rgb48le"; default: throw new NotSupportedException($"Not supported pixel format {fmt}"); } diff --git a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj index 25e820a..aafb577 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj +++ b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegCore.Extensions.System.Drawing.Common.csproj @@ -11,8 +11,7 @@ - - + diff --git a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs index 8cd0f26..f36f83d 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs +++ b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs @@ -1,6 +1,5 @@ using System.Drawing; using FFMpegCore.Pipes; -using SkiaSharp; namespace FFMpegCore.Extensions.System.Drawing.Common { @@ -15,7 +14,7 @@ public static class FFMpegImage /// Selected video stream index. /// Input file index /// Bitmap with the requested snapshot. - public static SKBitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0) + public static Bitmap Snapshot(string input, 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); @@ -27,8 +26,8 @@ public static SKBitmap Snapshot(string input, Size? size = null, TimeSpan? captu .ProcessSynchronously(); ms.Position = 0; - using var bitmap = SKBitmap.Decode(ms); - return bitmap.Copy(); + using var bitmap = new Bitmap(ms); + return bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat); } /// /// Saves a 'png' thumbnail to an in-memory bitmap @@ -39,7 +38,7 @@ public static SKBitmap Snapshot(string input, Size? size = null, TimeSpan? captu /// Selected video stream index. /// Input file index /// Bitmap with the requested snapshot. - public static async Task SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0) + public static async Task SnapshotAsync(string input, 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); @@ -51,7 +50,7 @@ await arguments .ProcessAsynchronously(); ms.Position = 0; - return SKBitmap.Decode(ms); + return new Bitmap(ms); } } } diff --git a/FFMpegCore.sln b/FFMpegCore.sln index 5a9faa8..7ab0929 100644 --- a/FFMpegCore.sln +++ b/FFMpegCore.sln @@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FFMpegCore.Test", "FFMpegCo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FFMpegCore.Examples", "FFMpegCore.Examples\FFMpegCore.Examples.csproj", "{3125CF91-FFBD-4E4E-8930-247116AFE772}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FFMpegCore.Extensions.System.Drawing.Common", "FFMpegCore.Extensions.System.Drawing.Common\FFMpegCore.Extensions.System.Drawing.Common.csproj", "{9C1A4930-9369-4A18-AD98-929A2A510D80}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FFMpegCore.Extensions.System.Drawing.Common", "FFMpegCore.Extensions.System.Drawing.Common\FFMpegCore.Extensions.System.Drawing.Common.csproj", "{9C1A4930-9369-4A18-AD98-929A2A510D80}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FFMpegCore.Extensions.SkiaSharp", "FFMpegCore.Extensions.SkiaSharp\FFMpegCore.Extensions.SkiaSharp.csproj", "{5A76F9B7-3681-4551-A9B6-8D3AC5DA1090}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {9C1A4930-9369-4A18-AD98-929A2A510D80}.Debug|Any CPU.Build.0 = Debug|Any CPU {9C1A4930-9369-4A18-AD98-929A2A510D80}.Release|Any CPU.ActiveCfg = Release|Any CPU {9C1A4930-9369-4A18-AD98-929A2A510D80}.Release|Any CPU.Build.0 = Release|Any CPU + {5A76F9B7-3681-4551-A9B6-8D3AC5DA1090}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5A76F9B7-3681-4551-A9B6-8D3AC5DA1090}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5A76F9B7-3681-4551-A9B6-8D3AC5DA1090}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5A76F9B7-3681-4551-A9B6-8D3AC5DA1090}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 315c1e1a914000e835e9b6a2b0e2fbae24458efc Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Thu, 16 Feb 2023 10:25:42 +0100 Subject: [PATCH 13/25] Adjusted FFMpegCore.Examples for separate FFMpegCore.Extensions.SkiaSharp project --- FFMpegCore.Examples/FFMpegCore.Examples.csproj | 1 + FFMpegCore.Examples/Program.cs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/FFMpegCore.Examples/FFMpegCore.Examples.csproj b/FFMpegCore.Examples/FFMpegCore.Examples.csproj index db3c66e..a1193a8 100644 --- a/FFMpegCore.Examples/FFMpegCore.Examples.csproj +++ b/FFMpegCore.Examples/FFMpegCore.Examples.csproj @@ -7,6 +7,7 @@ + diff --git a/FFMpegCore.Examples/Program.cs b/FFMpegCore.Examples/Program.cs index f926d94..24d29d5 100644 --- a/FFMpegCore.Examples/Program.cs +++ b/FFMpegCore.Examples/Program.cs @@ -3,7 +3,6 @@ using FFMpegCore.Enums; using FFMpegCore.Extensions.System.Drawing.Common; using FFMpegCore.Pipes; -using SkiaSharp; var inputPath = "/path/to/input"; var outputPath = "/path/to/output"; @@ -80,7 +79,8 @@ await FFMpegArguments FFMpeg.PosterWithAudio(inputPath, inputAudioPath, outputPath); // or #pragma warning disable CA1416 - using var image = SKBitmap.Decode(inputImagePath); + using var image = Image.FromFile(inputImagePath); // Using FFMpegCore.Extensions.System.Drawing.Common + //using var image = SKBitmap.Decode(inputImagePath); // Using FFMpegCore.Extensions.SkiaSharp image.AddAudio(inputAudioPath, outputPath); #pragma warning restore CA1416 } From 2458b4ae9c54732afeabc35a40229c72d277a2d5 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Thu, 16 Feb 2023 14:34:35 +0100 Subject: [PATCH 14/25] Adjusted FFMpegCore.Test for separate FFMpegCore.Extensions.SkiaSharp project --- FFMpegCore.Test/FFMpegCore.Test.csproj | 2 + FFMpegCore.Test/Utilities/BitmapSources.cs | 53 ++++++-- .../Utilities/WindowsOnlyDataTestMethod.cs | 24 ++-- .../Utilities/WindowsOnlyTestMethod.cs | 24 ++-- FFMpegCore.Test/VideoTest.cs | 124 +++++++++++++----- 5 files changed, 162 insertions(+), 65 deletions(-) diff --git a/FFMpegCore.Test/FFMpegCore.Test.csproj b/FFMpegCore.Test/FFMpegCore.Test.csproj index def07d2..67096f5 100644 --- a/FFMpegCore.Test/FFMpegCore.Test.csproj +++ b/FFMpegCore.Test/FFMpegCore.Test.csproj @@ -20,9 +20,11 @@ + + diff --git a/FFMpegCore.Test/Utilities/BitmapSources.cs b/FFMpegCore.Test/Utilities/BitmapSources.cs index 7e8d1a9..755f781 100644 --- a/FFMpegCore.Test/Utilities/BitmapSources.cs +++ b/FFMpegCore.Test/Utilities/BitmapSources.cs @@ -2,15 +2,25 @@ using System.Drawing.Imaging; using System.Numerics; using System.Runtime.Versioning; -using FFMpegCore.Extensions.System.Drawing.Common; using FFMpegCore.Pipes; using SkiaSharp; namespace FFMpegCore.Test.Utilities { - [SupportedOSPlatform("windows")] internal static class BitmapSource { + [SupportedOSPlatform("windows")] + public static IEnumerable CreateBitmaps(int count, PixelFormat fmt, int w, int h) + { + for (var i = 0; i < count; i++) + { + using (var frame = CreateVideoFrame(i, fmt, w, h, 0.025f, 0.025f * w * 0.03f)) + { + yield return frame; + } + } + } + public static IEnumerable CreateBitmaps(int count, SKColorType fmt, int w, int h) { for (var i = 0; i < count; i++) @@ -22,10 +32,41 @@ public static IEnumerable CreateBitmaps(int count, SKColorType fmt, } } - public static BitmapVideoFrameWrapper CreateVideoFrame(int index, SKColorType fmt, int w, int h, float scaleNoise, float offset) + [SupportedOSPlatform("windows")] + public static Extensions.System.Drawing.Common.BitmapVideoFrameWrapper CreateVideoFrame(int index, PixelFormat fmt, int w, int h, float scaleNoise, float offset) + { + var bitmap = new Bitmap(w, h, fmt); + + SetVideoFramePixels(index, w, h, scaleNoise, offset, ((int x, int y, byte red, byte green, byte blue) args) => + { + var color = Color.FromArgb(args.red, args.blue, args.green); + bitmap.SetPixel(args.x, args.y, color); + }); + + return new Extensions.System.Drawing.Common.BitmapVideoFrameWrapper(bitmap); + } + + public static Extensions.SkiaSharp.BitmapVideoFrameWrapper CreateVideoFrame(int index, SKColorType fmt, int w, int h, float scaleNoise, float offset) { var bitmap = new SKBitmap(w, h, fmt, SKAlphaType.Opaque); + SetVideoFramePixels(index, w, h, scaleNoise, offset, ((int x, int y, byte red, byte green, byte blue) args) => + { + var color = new SKColor(args.red, args.blue, args.green); + bitmap.SetPixel(args.x, args.y, color); + }); + + return new Extensions.SkiaSharp.BitmapVideoFrameWrapper(bitmap); + } + + private static void SetVideoFramePixels( + int index, + int w, + int h, + float scaleNoise, + float offset, + Action<(int x, int y, byte red, byte green, byte blue)> setPixel) + { offset = offset * index; for (var y = 0; y < h; y++) @@ -39,13 +80,9 @@ public static BitmapVideoFrameWrapper CreateVideoFrame(int index, SKColorType fm var value = (byte)((Perlin.Noise(nx, ny) + 1.0f) / 2.0f * 255); - var color = new SKColor((byte)(value * xf), (byte)(value * yf), value); - - bitmap.SetPixel(x, y, color); + setPixel((x, y, (byte)(value * xf), (byte)(value * yf), value)); } } - - return new BitmapVideoFrameWrapper(bitmap); } // diff --git a/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs b/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs index e39921a..84a779a 100644 --- a/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs +++ b/FFMpegCore.Test/Utilities/WindowsOnlyDataTestMethod.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FFMpegCore.Test.Utilities; @@ -6,17 +7,16 @@ public class WindowsOnlyDataTestMethod : DataTestMethodAttribute { public override TestResult[] Execute(ITestMethod testMethod) { - // Commented out because this edition of FFMpegCore fully supports Linux - //if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - //{ - // var message = $"Test not executed on other platforms than Windows"; - // { - // return new[] - // { - // new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } - // }; - // } - //} + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + var message = $"Test not executed on other platforms than Windows"; + { + return new[] + { + new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } + }; + } + } return base.Execute(testMethod); } diff --git a/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs b/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs index 5143194..7e817bf 100644 --- a/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs +++ b/FFMpegCore.Test/Utilities/WindowsOnlyTestMethod.cs @@ -1,4 +1,5 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Runtime.InteropServices; +using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FFMpegCore.Test.Utilities; @@ -6,17 +7,16 @@ public class WindowsOnlyTestMethod : TestMethodAttribute { public override TestResult[] Execute(ITestMethod testMethod) { - // Commented out because this edition of FFMpegCore fully supports Linux - //if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - //{ - // var message = $"Test not executed on other platforms than Windows"; - // { - // return new[] - // { - // new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } - // }; - // } - //} + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + var message = $"Test not executed on other platforms than Windows"; + { + return new[] + { + new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(message) } + }; + } + } return base.Execute(testMethod); } diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 930cd99..90a4b89 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -1,21 +1,20 @@ -using System.Runtime.Versioning; +using System.Drawing.Imaging; +using System.Runtime.Versioning; using System.Text; using FFMpegCore.Arguments; using FFMpegCore.Enums; using FFMpegCore.Exceptions; -using FFMpegCore.Extensions.System.Drawing.Common; using FFMpegCore.Pipes; using FFMpegCore.Test.Resources; using FFMpegCore.Test.Utilities; using Microsoft.VisualStudio.TestTools.UnitTesting; -using SkiaSharp; namespace FFMpegCore.Test { [TestClass] public class VideoTest { - private const int BaseTimeoutMilliseconds = 60_000; + private const int BaseTimeoutMilliseconds = 10_000; [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToOGV() @@ -85,9 +84,16 @@ public void Video_ToH265_MKV_Args() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] - [DataRow(SKColorType.Rgb565)] - [DataRow(SKColorType.Bgra8888)] - public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat) + [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] + [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] + public void Video_ToMP4_Args_Pipe_WindowsOnly(System.Drawing.Imaging.PixelFormat pixelFormat) => Video_ToMP4_Args_Pipe_Internal(pixelFormat); + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + [DataRow(SkiaSharp.SKColorType.Rgb565)] + [DataRow(SkiaSharp.SKColorType.Bgra8888)] + public void Video_ToMP4_Args_Pipe(SkiaSharp.SKColorType pixelFormat) => Video_ToMP4_Args_Pipe_Internal(pixelFormat); + + private static void Video_ToMP4_Args_Pipe_Internal(dynamic pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); @@ -102,14 +108,19 @@ public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat) [SupportedOSPlatform("windows")] [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] - public void Video_ToMP4_Args_Pipe_DifferentImageSizes() + public void Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly() => Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(System.Drawing.Imaging.PixelFormat.Format24bppRgb); + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + public void Video_ToMP4_Args_Pipe_DifferentImageSizes() => Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(SkiaSharp.SKColorType.Rgb565); + + private static void Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(dynamic pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); var frames = new List { - BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 256, 256, 1, 0) + BitmapSource.CreateVideoFrame(0, pixelFormat, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, pixelFormat, 256, 256, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -122,14 +133,19 @@ public void Video_ToMP4_Args_Pipe_DifferentImageSizes() [SupportedOSPlatform("windows")] [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] - public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() + public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly_Async() => await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(System.Drawing.Imaging.PixelFormat.Format24bppRgb); + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() => await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(SkiaSharp.SKColorType.Rgb565); + + private static async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(dynamic pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); var frames = new List { - BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 256, 256, 1, 0) + BitmapSource.CreateVideoFrame(0, pixelFormat, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, pixelFormat, 256, 256, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -142,14 +158,20 @@ public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async() [SupportedOSPlatform("windows")] [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] - public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() + public void Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly() => + Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(System.Drawing.Imaging.PixelFormat.Format24bppRgb, System.Drawing.Imaging.PixelFormat.Format32bppRgb); + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() => Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(SkiaSharp.SKColorType.Rgb565, SkiaSharp.SKColorType.Bgra8888); + + private static void Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); var frames = new List { - BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, SKColorType.Bgra8888, 255, 255, 1, 0) + BitmapSource.CreateVideoFrame(0, pixelFormatFrame1, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, pixelFormatFrame2, 255, 255, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -162,14 +184,20 @@ public void Video_ToMP4_Args_Pipe_DifferentPixelFormats() [SupportedOSPlatform("windows")] [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] - public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async() + public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly_Async() => + await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(System.Drawing.Imaging.PixelFormat.Format24bppRgb, System.Drawing.Imaging.PixelFormat.Format32bppRgb); + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async() => await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(SkiaSharp.SKColorType.Rgb565, SkiaSharp.SKColorType.Bgra8888); + + private static async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); var frames = new List { - BitmapSource.CreateVideoFrame(0, SKColorType.Rgb565, 255, 255, 1, 0), - BitmapSource.CreateVideoFrame(0, SKColorType.Bgra8888, 255, 255, 1, 0) + BitmapSource.CreateVideoFrame(0, pixelFormatFrame1, 255, 255, 1, 0), + BitmapSource.CreateVideoFrame(0, pixelFormatFrame2, 255, 255, 1, 0) }; var videoFramesSource = new RawVideoPipeSource(frames); @@ -315,9 +343,16 @@ public void Video_ToTS_Args() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] - [DataRow(SKColorType.Rgb565)] - [DataRow(SKColorType.Bgra8888)] - public async Task Video_ToTS_Args_Pipe(SKColorType pixelFormat) + [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] + [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] + public async Task Video_ToTS_Args_Pipe_WindowsOnly(System.Drawing.Imaging.PixelFormat pixelFormat) => await Video_ToTS_Args_Pipe_Internal(pixelFormat); + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + [DataRow(SkiaSharp.SKColorType.Rgb565)] + [DataRow(SkiaSharp.SKColorType.Bgra8888)] + public async Task Video_ToTS_Args_Pipe(SkiaSharp.SKColorType pixelFormat) => await Video_ToTS_Args_Pipe_Internal(pixelFormat); + + private static async Task Video_ToTS_Args_Pipe_Internal(dynamic pixelFormat) { using var output = new TemporaryFile($"out{VideoType.Ts.Extension}"); var input = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); @@ -348,9 +383,9 @@ public async Task Video_ToOGV_Resize() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] - [DataRow(SKColorType.Rgb565)] - [DataRow(SKColorType.Bgra8888)] - public void RawVideoPipeSource_Ogv_Scale(SKColorType pixelFormat) + [DataRow(SkiaSharp.SKColorType.Rgb565)] + [DataRow(SkiaSharp.SKColorType.Bgra8888)] + public void RawVideoPipeSource_Ogv_Scale(SkiaSharp.SKColorType pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Ogv.Extension}"); var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); @@ -383,10 +418,17 @@ public void Scale_Mp4_Multithreaded() [SupportedOSPlatform("windows")] [WindowsOnlyDataTestMethod, Timeout(BaseTimeoutMilliseconds)] - [DataRow(SKColorType.Rgb565)] - [DataRow(SKColorType.Bgra8888)] + [DataRow(System.Drawing.Imaging.PixelFormat.Format24bppRgb)] + [DataRow(System.Drawing.Imaging.PixelFormat.Format32bppArgb)] // [DataRow(PixelFormat.Format48bppRgb)] - public void Video_ToMP4_Resize_Args_Pipe(SKColorType pixelFormat) + public void Video_ToMP4_Resize_Args_Pipe(System.Drawing.Imaging.PixelFormat pixelFormat) => Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat); + + [DataTestMethod, Timeout(BaseTimeoutMilliseconds)] + [DataRow(SkiaSharp.SKColorType.Rgb565)] + [DataRow(SkiaSharp.SKColorType.Bgra8888)] + public void Video_ToMP4_Resize_Args_Pipe(SkiaSharp.SKColorType pixelFormat) => Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat); + + private static void Video_ToMP4_Resize_Args_Pipe_Internal(dynamic pixelFormat) { using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}"); var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256)); @@ -401,14 +443,25 @@ public void Video_ToMP4_Resize_Args_Pipe(SKColorType pixelFormat) [SupportedOSPlatform("windows")] [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] - public void Video_Snapshot_InMemory() + public void Video_Snapshot_InMemory_SystemDrawingCommon() { - using var bitmap = FFMpegImage.Snapshot(TestResources.Mp4Video); + using var bitmap = Extensions.System.Drawing.Common.FFMpegImage.Snapshot(TestResources.Mp4Video); var input = FFProbe.Analyse(TestResources.Mp4Video); Assert.AreEqual(input.PrimaryVideoStream!.Width, bitmap.Width); Assert.AreEqual(input.PrimaryVideoStream.Height, bitmap.Height); - Assert.AreEqual(bitmap.ColorType, SKColorType.Bgra8888); + Assert.AreEqual(bitmap.RawFormat, ImageFormat.Png); + } + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + public void Video_Snapshot_InMemory_SkiaSharp() + { + using var bitmap = Extensions.SkiaSharp.FFMpegImage.Snapshot(TestResources.Mp4Video); + + var input = FFProbe.Analyse(TestResources.Mp4Video); + Assert.AreEqual(input.PrimaryVideoStream!.Width, bitmap.Width); + Assert.AreEqual(input.PrimaryVideoStream.Height, bitmap.Height); + Assert.AreEqual(bitmap.ColorType, SkiaSharp.SKColorType.Bgra8888); } [TestMethod, Timeout(BaseTimeoutMilliseconds)] @@ -565,11 +618,16 @@ public void Video_OutputsData() [SupportedOSPlatform("windows")] [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] - public void Video_TranscodeInMemory() + public void Video_TranscodeInMemory_WindowsOnly() => Video_TranscodeInMemory_Internal(System.Drawing.Imaging.PixelFormat.Format24bppRgb); + + [TestMethod, Timeout(BaseTimeoutMilliseconds)] + public void Video_TranscodeInMemory() => Video_TranscodeInMemory_Internal(SkiaSharp.SKColorType.Rgb565); + + private static void Video_TranscodeInMemory_Internal(dynamic pixelFormat) { using var resStream = new MemoryStream(); var reader = new StreamPipeSink(resStream); - var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, SKColorType.Rgb565, 128, 128)); + var writer = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 128, 128)); FFMpegArguments .FromPipeInput(writer) From a66bdba21146d82e35eda94666344d3aa15bc077 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Thu, 16 Feb 2023 14:35:00 +0100 Subject: [PATCH 15/25] Improved SkiaSharp CreateVideoFrame performance --- FFMpegCore.Test/Utilities/BitmapSources.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/FFMpegCore.Test/Utilities/BitmapSources.cs b/FFMpegCore.Test/Utilities/BitmapSources.cs index 755f781..8965435 100644 --- a/FFMpegCore.Test/Utilities/BitmapSources.cs +++ b/FFMpegCore.Test/Utilities/BitmapSources.cs @@ -50,10 +50,11 @@ public static Extensions.SkiaSharp.BitmapVideoFrameWrapper CreateVideoFrame(int { var bitmap = new SKBitmap(w, h, fmt, SKAlphaType.Opaque); + using var bitmapCanvas = new SKCanvas(bitmap); SetVideoFramePixels(index, w, h, scaleNoise, offset, ((int x, int y, byte red, byte green, byte blue) args) => { var color = new SKColor(args.red, args.blue, args.green); - bitmap.SetPixel(args.x, args.y, color); + bitmapCanvas.DrawPoint(args.x, args.y, color); }); return new Extensions.SkiaSharp.BitmapVideoFrameWrapper(bitmap); From 55b652a77f8f3a0a7a4d916b7cf0ab2e5e71b077 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Thu, 16 Feb 2023 14:39:09 +0100 Subject: [PATCH 16/25] Added TODO for additional SKColorType support --- FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs b/FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs index 2556883..7bb98fb 100644 --- a/FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs +++ b/FFMpegCore.Extensions.SkiaSharp/BitmapVideoFrameWrapper.cs @@ -38,6 +38,7 @@ public void Dispose() private static string ConvertStreamFormat(SKColorType fmt) { + // TODO: Add support for additional formats switch (fmt) { case SKColorType.Gray8: From f3c7df1ff516e1b27f037b5f7333d812816e602d Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Thu, 16 Feb 2023 14:39:14 +0100 Subject: [PATCH 17/25] Code formatting --- FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs index f36f83d..c946507 100644 --- a/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs +++ b/FFMpegCore.Extensions.System.Drawing.Common/FFMpegImage.cs @@ -29,6 +29,7 @@ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? capture using var bitmap = new Bitmap(ms); return bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat); } + /// /// Saves a 'png' thumbnail to an in-memory bitmap /// From 7b32ba5a27fd64ab09f1d3270b5e36928163cb4b Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Thu, 16 Feb 2023 14:48:06 +0100 Subject: [PATCH 18/25] Fixed ambiguous method call The two constructors were causing issues with ambiguous method calls but are a good idea, so this is more of a workaround. Error message: The call is ambiguous between the following methods or properties: 'FFMpegCore.Pipes.RawVideoPipeSource.RawVideoPipeSource(System.Collections.Generic.IEnumerator)' and 'FFMpegCore.Pipes.RawVideoPipeSource.RawVideoPipeSource(System.Collections.Generic.IEnumerable)' --- FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs b/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs index fe4c881..2f3028f 100644 --- a/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs +++ b/FFMpegCore/FFMpeg/Pipes/RawVideoPipeSource.cs @@ -15,13 +15,11 @@ public class RawVideoPipeSource : IPipeSource private bool _formatInitialized; private readonly IEnumerator _framesEnumerator; - public RawVideoPipeSource(IEnumerator framesEnumerator) + public RawVideoPipeSource(IEnumerable framesEnumerator) { - _framesEnumerator = framesEnumerator; + _framesEnumerator = framesEnumerator.GetEnumerator(); } - public RawVideoPipeSource(IEnumerable framesEnumerator) : this(framesEnumerator.GetEnumerator()) { } - public string GetStreamArguments() { if (!_formatInitialized) From 01a33f9a1f0f443fbeb9c320ca9e44eaf092badd Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 20 Feb 2023 13:18:01 +0100 Subject: [PATCH 19/25] Updated package properties for SkiaSharp --- .../FFMpegCore.Extensions.SkiaSharp.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj b/FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj index 25e820a..f055149 100644 --- a/FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj +++ b/FFMpegCore.Extensions.SkiaSharp/FFMpegCore.Extensions.SkiaSharp.csproj @@ -2,12 +2,12 @@ true - Image extension for FFMpegCore using System.Common.Drawing + Image extension for FFMpegCore using SkiaSharp 5.0.0 - ffmpeg ffprobe convert video audio mediafile resize analyze muxing - Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev + ffmpeg ffprobe convert video audio mediafile resize analyze muxing skiasharp + Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev, Dimitri Vranken From b63258217d4bffa4f6ab525acf73c8eb2ea8a4a0 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 20 Feb 2023 13:18:14 +0100 Subject: [PATCH 20/25] Updated example program to avoid commented-out code --- FFMpegCore.Examples/Program.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/FFMpegCore.Examples/Program.cs b/FFMpegCore.Examples/Program.cs index 24d29d5..e89de17 100644 --- a/FFMpegCore.Examples/Program.cs +++ b/FFMpegCore.Examples/Program.cs @@ -2,7 +2,10 @@ using FFMpegCore; using FFMpegCore.Enums; using FFMpegCore.Extensions.System.Drawing.Common; +using FFMpegCore.Extensions.SkiaSharp; using FFMpegCore.Pipes; +using SkiaSharp; +using FFMpegImage = FFMpegCore.Extensions.System.Drawing.Common.FFMpegImage; var inputPath = "/path/to/input"; var outputPath = "/path/to/output"; @@ -77,12 +80,14 @@ await FFMpegArguments var inputImagePath = "/path/to/input/image"; { FFMpeg.PosterWithAudio(inputPath, inputAudioPath, outputPath); - // or + // or using FFMpegCore.Extensions.System.Drawing.Common #pragma warning disable CA1416 - using var image = Image.FromFile(inputImagePath); // Using FFMpegCore.Extensions.System.Drawing.Common - //using var image = SKBitmap.Decode(inputImagePath); // Using FFMpegCore.Extensions.SkiaSharp + using var image = Image.FromFile(inputImagePath); image.AddAudio(inputAudioPath, outputPath); #pragma warning restore CA1416 + // or using FFMpegCore.Extensions.SkiaSharp + using var skiaSharpImage = SKBitmap.Decode(inputImagePath); + skiaSharpImage.AddAudio(inputAudioPath, outputPath); } IVideoFrame GetNextFrame() => throw new NotImplementedException(); From 9646c440bb549046775b3f52ecb62add58eef76a Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 20 Feb 2023 13:23:06 +0100 Subject: [PATCH 21/25] Fixed code style issues --- FFMpegCore.Examples/Program.cs | 2 +- FFMpegCore.Test/VideoTest.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/FFMpegCore.Examples/Program.cs b/FFMpegCore.Examples/Program.cs index e89de17..d7abde4 100644 --- a/FFMpegCore.Examples/Program.cs +++ b/FFMpegCore.Examples/Program.cs @@ -1,8 +1,8 @@ using System.Drawing; using FFMpegCore; using FFMpegCore.Enums; -using FFMpegCore.Extensions.System.Drawing.Common; using FFMpegCore.Extensions.SkiaSharp; +using FFMpegCore.Extensions.System.Drawing.Common; using FFMpegCore.Pipes; using SkiaSharp; using FFMpegImage = FFMpegCore.Extensions.System.Drawing.Common.FFMpegImage; diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 03e0b1a..a6be018 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -184,7 +184,7 @@ private static void Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(dynamic [SupportedOSPlatform("windows")] [WindowsOnlyTestMethod, Timeout(BaseTimeoutMilliseconds)] - public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly_Async() => + public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly_Async() => await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(System.Drawing.Imaging.PixelFormat.Format24bppRgb, System.Drawing.Imaging.PixelFormat.Format32bppRgb); [TestMethod, Timeout(BaseTimeoutMilliseconds)] From 317ba47dd282792da0b782330c6c7461833f2b13 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 20 Feb 2023 13:46:58 +0100 Subject: [PATCH 22/25] Refactored video frame generator to pure function --- FFMpegCore.Test/Utilities/BitmapSources.cs | 26 +++++++++------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/FFMpegCore.Test/Utilities/BitmapSources.cs b/FFMpegCore.Test/Utilities/BitmapSources.cs index 8965435..6c15710 100644 --- a/FFMpegCore.Test/Utilities/BitmapSources.cs +++ b/FFMpegCore.Test/Utilities/BitmapSources.cs @@ -37,11 +37,11 @@ public static Extensions.System.Drawing.Common.BitmapVideoFrameWrapper CreateVid { var bitmap = new Bitmap(w, h, fmt); - SetVideoFramePixels(index, w, h, scaleNoise, offset, ((int x, int y, byte red, byte green, byte blue) args) => + foreach (var (x, y, red, green, blue) in GenerateVideoFramePixels(index, w, h, scaleNoise, offset)) { - var color = Color.FromArgb(args.red, args.blue, args.green); - bitmap.SetPixel(args.x, args.y, color); - }); + var color = Color.FromArgb(red, blue, green); + bitmap.SetPixel(x, y, color); + } return new Extensions.System.Drawing.Common.BitmapVideoFrameWrapper(bitmap); } @@ -51,22 +51,16 @@ public static Extensions.SkiaSharp.BitmapVideoFrameWrapper CreateVideoFrame(int var bitmap = new SKBitmap(w, h, fmt, SKAlphaType.Opaque); using var bitmapCanvas = new SKCanvas(bitmap); - SetVideoFramePixels(index, w, h, scaleNoise, offset, ((int x, int y, byte red, byte green, byte blue) args) => + foreach (var (x, y, red, green, blue) in GenerateVideoFramePixels(index, w, h, scaleNoise, offset)) { - var color = new SKColor(args.red, args.blue, args.green); - bitmapCanvas.DrawPoint(args.x, args.y, color); - }); + var color = new SKColor(red, blue, green); + bitmapCanvas.DrawPoint(x, y, color); + } return new Extensions.SkiaSharp.BitmapVideoFrameWrapper(bitmap); } - private static void SetVideoFramePixels( - int index, - int w, - int h, - float scaleNoise, - float offset, - Action<(int x, int y, byte red, byte green, byte blue)> setPixel) + private static IEnumerable<(int x, int y, byte red, byte green, byte blue)> GenerateVideoFramePixels(int index, int w, int h, float scaleNoise, float offset) { offset = offset * index; @@ -81,7 +75,7 @@ private static void SetVideoFramePixels( var value = (byte)((Perlin.Noise(nx, ny) + 1.0f) / 2.0f * 255); - setPixel((x, y, (byte)(value * xf), (byte)(value * yf), value)); + yield return ((x, y, (byte)(value * xf), (byte)(value * yf), value)); } } } From 5593bc4a4b119ca1a56ef5400383a6de4f0cde18 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 20 Feb 2023 13:50:19 +0100 Subject: [PATCH 23/25] Improved SkiaSharp CreateVideoFrame performance Execution time is now approximately one fifth of what it was previously --- FFMpegCore.Test/Utilities/BitmapSources.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/FFMpegCore.Test/Utilities/BitmapSources.cs b/FFMpegCore.Test/Utilities/BitmapSources.cs index 6c15710..f3b657a 100644 --- a/FFMpegCore.Test/Utilities/BitmapSources.cs +++ b/FFMpegCore.Test/Utilities/BitmapSources.cs @@ -50,12 +50,9 @@ public static Extensions.SkiaSharp.BitmapVideoFrameWrapper CreateVideoFrame(int { var bitmap = new SKBitmap(w, h, fmt, SKAlphaType.Opaque); - using var bitmapCanvas = new SKCanvas(bitmap); - foreach (var (x, y, red, green, blue) in GenerateVideoFramePixels(index, w, h, scaleNoise, offset)) - { - var color = new SKColor(red, blue, green); - bitmapCanvas.DrawPoint(x, y, color); - } + bitmap.Pixels = GenerateVideoFramePixels(index, w, h, scaleNoise, offset) + .Select(args => new SKColor(args.red, args.blue, args.green)) + .ToArray(); return new Extensions.SkiaSharp.BitmapVideoFrameWrapper(bitmap); } From e3aa7a5eae21a2e5f69150278f76f57732e2a28a Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 20 Feb 2023 13:57:48 +0100 Subject: [PATCH 24/25] Removed invalid assertion --- FFMpegCore.Test/VideoTest.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index a6be018..9814666 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -461,7 +461,8 @@ public void Video_Snapshot_InMemory_SkiaSharp() var input = FFProbe.Analyse(TestResources.Mp4Video); Assert.AreEqual(input.PrimaryVideoStream!.Width, bitmap.Width); Assert.AreEqual(input.PrimaryVideoStream.Height, bitmap.Height); - Assert.AreEqual(bitmap.ColorType, SkiaSharp.SKColorType.Bgra8888); + // Note: The resulting ColorType is dependent on the execution environment and therefore not assessed, + // e.g. Bgra8888 on Windows and Rgba8888 on macOS. } [TestMethod, Timeout(BaseTimeoutMilliseconds)] From 46a8ec7da5dcdd609a1a49d037502f4c68f9cd82 Mon Sep 17 00:00:00 2001 From: Dimitri Vranken Date: Mon, 20 Feb 2023 14:10:18 +0100 Subject: [PATCH 25/25] Increased unit test timeout Existing (untouched) tests are not stable in CI test execution --- FFMpegCore.Test/VideoTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FFMpegCore.Test/VideoTest.cs b/FFMpegCore.Test/VideoTest.cs index 9814666..4403065 100644 --- a/FFMpegCore.Test/VideoTest.cs +++ b/FFMpegCore.Test/VideoTest.cs @@ -14,7 +14,7 @@ namespace FFMpegCore.Test [TestClass] public class VideoTest { - private const int BaseTimeoutMilliseconds = 10_000; + private const int BaseTimeoutMilliseconds = 15_000; [TestMethod, Timeout(BaseTimeoutMilliseconds)] public void Video_ToOGV()