Compare commits

...

26 commits

Author SHA1 Message Date
SinisterMaya
edaf3f5bac
Merge adfc781e4c into a599c48511 2025-10-18 11:26:27 +02:00
Malte Rosenbjerg
a599c48511
Merge pull request #585 from rosenbjerg/fix-fps-handling-in-join-image-sequence
Fix fps handling in JoinImageSequence
2025-10-18 01:04:10 +02:00
Malte Rosenbjerg
0e62fb4d57
Merge branch 'main' into fix-fps-handling-in-join-image-sequence 2025-10-18 00:37:20 +02:00
Malte Rosenbjerg
4db4d2c8ea
Merge pull request #586 from rosenbjerg/improve-percentage-progress-test
Improve test for percentage progress events
2025-10-18 00:36:19 +02:00
Malte Rosenbjerg
f20bd0c433
Merge branch 'main' into improve-percentage-progress-test 2025-10-18 00:01:33 +02:00
Malte Rosenbjerg
7d4545ffe0
Merge pull request #587 from rosenbjerg/ensure-test-context-cancellationtoken-is-used
Ensure TestContext.CancellationToken is used
2025-10-18 00:01:15 +02:00
Malte Rosenbjerg
90786394a6 Increase timeout due to slow windows CI agent ... 2025-10-17 23:49:53 +02:00
Malte Rosenbjerg
9a0f784c71 Increate timeout further due to slow windows CI .... 2025-10-17 23:29:13 +02:00
Malte Rosenbjerg
90be0888e8 Create BaseTimeoutMilliseconds in AudioTests 2025-10-17 23:15:28 +02:00
Malte Rosenbjerg
f11b168ed9 Increase timeout because of slow windows CI agents 2025-10-17 23:15:16 +02:00
Malte Rosenbjerg
d0f6db1a2a Dont override in test using cancellationtoken 2025-10-17 22:52:33 +02:00
Malte Rosenbjerg
40414ad008 Remove extranous blank line 2025-10-17 22:46:45 +02:00
Malte Rosenbjerg
0ea445cdb8 Apply suggestions from Copilot review 2025-10-17 22:46:03 +02:00
Malte Rosenbjerg
326b3e2719 Use local CancellationTokenSource 2025-10-17 22:43:25 +02:00
Malte Rosenbjerg
fab7ff0aab Ensure TestContext.CancellationToken is used 2025-10-17 22:37:39 +02:00
Malte Rosenbjerg
2003100909 Improve test for percentage progress events 2025-10-17 22:14:00 +02:00
Malte Rosenbjerg
0d07456c6e
Merge pull request #584 from rosenbjerg/improve-cancellation-handling
Improve cancellation handling
2025-10-17 22:12:50 +02:00
Malte Rosenbjerg
34a9174b90
Merge pull request #583 from rosenbjerg/fix-GetCreationTime-function
Fix GetCreationTime
2025-10-17 21:57:10 +02:00
Malte Rosenbjerg
d890429269 Remove extranous blank line 2025-10-17 21:56:40 +02:00
Malte Rosenbjerg
670986dcb2 Extract method for reuse 2025-10-17 21:54:19 +02:00
Malte Rosenbjerg
7c070765b8 Remove stray space 2025-10-17 21:51:43 +02:00
Malte Rosenbjerg
4baddaab7f Add test verifying cancellation before processing starts 2025-10-17 21:51:11 +02:00
Malte Rosenbjerg
6b1e34ce08 Change fps parameter to double 2025-10-17 21:50:49 +02:00
Malte Rosenbjerg
3c3da28a99 Update test assertion on video duration 2025-10-17 21:50:34 +02:00
Malte Rosenbjerg
0f800c4333 Provide fps argument to input parameter as well 2025-10-17 21:49:42 +02:00
Malte Rosenbjerg
e01b73787d Improve cancellation handling 2025-10-17 21:48:30 +02:00
8 changed files with 164 additions and 65 deletions

View file

@ -9,6 +9,10 @@ namespace FFMpegCore.Test;
[TestClass]
public class AudioTest
{
private const int BaseTimeoutMilliseconds = 30_000;
public TestContext TestContext { get; set; }
[TestMethod]
public void Audio_Remove()
{
@ -41,6 +45,7 @@ public class AudioTest
await FFMpegArguments
.FromPipeInput(new StreamPipeSource(file), options => options.ForceFormat("s16le"))
.OutputToPipe(new StreamPipeSink(memoryStream), options => options.ForceFormat("mp3"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously();
}
@ -70,7 +75,7 @@ public class AudioTest
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -83,12 +88,13 @@ public class AudioTest
.FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_ToLibVorbis_Args_Pipe()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -101,12 +107,13 @@ public class AudioTest
.FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.LibVorbis))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public async Task Audio_ToAAC_Args_Pipe_Async()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -119,12 +126,13 @@ public class AudioTest
.FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously();
Assert.IsTrue(success);
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_ValidDefaultConfiguration()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -137,12 +145,13 @@ public class AudioTest
.FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_InvalidChannels()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -153,11 +162,12 @@ public class AudioTest
.FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_InvalidFormat()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -168,11 +178,12 @@ public class AudioTest
.FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_ToAAC_Args_Pipe_InvalidSampleRate()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -183,11 +194,12 @@ public class AudioTest
.FromPipeInput(audioSamplesSource)
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_Pan_ToMono()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -196,6 +208,7 @@ public class AudioTest
.OutputToFile(outputFile, true,
argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan(1, "c0 < 0.9 * c0 + 0.1 * c1")))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
var mediaAnalysis = FFProbe.Analyse(outputFile);
@ -206,7 +219,7 @@ public class AudioTest
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_Pan_ToMonoNoDefinitions()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -215,6 +228,7 @@ public class AudioTest
.OutputToFile(outputFile, true,
argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan(1)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
var mediaAnalysis = FFProbe.Analyse(outputFile);
@ -225,7 +239,7 @@ public class AudioTest
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_Pan_ToMonoChannelsToOutputDefinitionsMismatch()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -234,11 +248,12 @@ public class AudioTest
.OutputToFile(outputFile, true,
argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan(1, "c0=c0", "c1=c1")))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_Pan_ToMonoChannelsLayoutToOutputDefinitionsMismatch()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -247,11 +262,12 @@ public class AudioTest
.OutputToFile(outputFile, true,
argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.Pan("mono", "c0=c0", "c1=c1")))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_DynamicNormalizer_WithDefaultValues()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -260,13 +276,14 @@ public class AudioTest
.OutputToFile(outputFile, true,
argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.DynamicNormalizer()))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Audio_DynamicNormalizer_WithNonDefaultValues()
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -275,13 +292,14 @@ public class AudioTest
.OutputToFile(outputFile, true,
argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.DynamicNormalizer(250, 7, 0.9, 2, 1, false, true, true, 0.5)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
[TestMethod]
[Timeout(10000, CooperativeCancellation = true)]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
[DataRow(2)]
[DataRow(32)]
[DataRow(8)]
@ -294,6 +312,7 @@ public class AudioTest
.OutputToFile(outputFile, true,
argumentOptions => argumentOptions
.WithAudioFilters(filter => filter.DynamicNormalizer(filterWindow: filterWindow)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
}

View file

@ -17,7 +17,7 @@ namespace FFMpegCore.Test;
[TestClass]
public class VideoTest
{
private const int BaseTimeoutMilliseconds = 15_000;
private const int BaseTimeoutMilliseconds = 60_000;
private string _segmentPathSource = "";
@ -47,6 +47,7 @@ public class VideoTest
var success = FFMpegArguments
.FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -60,6 +61,7 @@ public class VideoTest
var success = FFMpegArguments
.FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -75,6 +77,7 @@ public class VideoTest
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264)
.ForcePixelFormat("yuv444p"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
var analysis = FFProbe.Analyse(outputFile);
@ -91,6 +94,7 @@ public class VideoTest
.FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -105,6 +109,7 @@ public class VideoTest
.FromFileInput(TestResources.WebmVideo)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX265))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -116,7 +121,7 @@ public class VideoTest
[DataRow(PixelFormat.Format32bppArgb)]
public void Video_ToMP4_Args_Pipe_WindowsOnly(PixelFormat pixelFormat)
{
Video_ToMP4_Args_Pipe_Internal(pixelFormat);
Video_ToMP4_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken);
}
[TestMethod]
@ -125,10 +130,10 @@ public class VideoTest
[DataRow(SKColorType.Bgra8888)]
public void Video_ToMP4_Args_Pipe(SKColorType pixelFormat)
{
Video_ToMP4_Args_Pipe_Internal(pixelFormat);
Video_ToMP4_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken);
}
private static void Video_ToMP4_Args_Pipe_Internal(dynamic pixelFormat)
private static void Video_ToMP4_Args_Pipe_Internal(dynamic pixelFormat, CancellationToken cancellationToken)
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -137,6 +142,7 @@ public class VideoTest
.FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -146,17 +152,17 @@ public class VideoTest
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly()
{
Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(PixelFormat.Format24bppRgb);
Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(PixelFormat.Format24bppRgb, TestContext.CancellationToken);
}
[TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_ToMP4_Args_Pipe_DifferentImageSizes()
{
Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(SKColorType.Rgb565);
Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(SKColorType.Rgb565, TestContext.CancellationToken);
}
private static void Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(dynamic pixelFormat)
private static void Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal(dynamic pixelFormat, CancellationToken cancellationToken)
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -170,6 +176,7 @@ public class VideoTest
.FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously());
}
@ -178,17 +185,17 @@ public class VideoTest
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_WindowsOnly_Async()
{
await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(PixelFormat.Format24bppRgb);
await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(PixelFormat.Format24bppRgb, TestContext.CancellationToken);
}
[TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Async()
{
await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(SKColorType.Rgb565);
await Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(SKColorType.Rgb565, TestContext.CancellationToken);
}
private static async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(dynamic pixelFormat)
private static async Task Video_ToMP4_Args_Pipe_DifferentImageSizes_Internal_Async(dynamic pixelFormat, CancellationToken cancellationToken)
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -202,6 +209,7 @@ public class VideoTest
.FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessAsynchronously());
}
@ -211,17 +219,18 @@ public class VideoTest
public void Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly()
{
Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(PixelFormat.Format24bppRgb,
PixelFormat.Format32bppRgb);
PixelFormat.Format32bppRgb, TestContext.CancellationToken);
}
[TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_ToMP4_Args_Pipe_DifferentPixelFormats()
{
Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(SKColorType.Rgb565, SKColorType.Bgra8888);
Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(SKColorType.Rgb565, SKColorType.Bgra8888, TestContext.CancellationToken);
}
private static void Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2)
private static void Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2,
CancellationToken cancellationToken)
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -235,6 +244,7 @@ public class VideoTest
.FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously());
}
@ -244,17 +254,18 @@ public class VideoTest
public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_WindowsOnly_Async()
{
await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(PixelFormat.Format24bppRgb,
PixelFormat.Format32bppRgb);
PixelFormat.Format32bppRgb, TestContext.CancellationToken);
}
[TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Async()
{
await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(SKColorType.Rgb565, SKColorType.Bgra8888);
await Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(SKColorType.Rgb565, SKColorType.Bgra8888, TestContext.CancellationToken);
}
private static async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2)
private static async Task Video_ToMP4_Args_Pipe_DifferentPixelFormats_Internal_Async(dynamic pixelFormatFrame1, dynamic pixelFormatFrame2,
CancellationToken cancellationToken)
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
@ -268,6 +279,7 @@ public class VideoTest
.FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessAsynchronously());
}
@ -282,6 +294,7 @@ public class VideoTest
.FromPipeInput(new StreamPipeSource(input))
.OutputToFile(output, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -297,6 +310,7 @@ public class VideoTest
await FFMpegArguments
.FromFileInput(TestResources.Mp4Video)
.OutputToPipe(pipeSource, opt => opt.ForceFormat("mp4"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously();
});
}
@ -312,6 +326,7 @@ public class VideoTest
.ForceFormat("webm"))
.OutputToPipe(new StreamPipeSink(output), opt => opt
.ForceFormat("mpegts"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
output.Position = 0;
@ -330,6 +345,7 @@ public class VideoTest
.FromFileInput(TestResources.Mp4Video)
.OutputToPipe(new StreamPipeSink(ms), opt => opt
.ForceFormat("mkv"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
});
}
@ -345,6 +361,7 @@ public class VideoTest
.OutputToPipe(pipeSource, opt => opt
.WithVideoCodec(VideoCodec.LibX264)
.ForceFormat("matroska"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously();
}
@ -355,11 +372,13 @@ public class VideoTest
FFMpegArguments
.FromFileInput(TestResources.Mp4Video)
.OutputToFile("temporary.mp4")
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
await FFMpegArguments
.FromFileInput(TestResources.Mp4Video)
.OutputToFile("temporary.mp4")
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously();
File.Delete("temporary.mp4");
@ -375,6 +394,7 @@ public class VideoTest
.OutputToPipe(new StreamPipeSink(output), opt => opt
.WithVideoCodec(VideoCodec.LibVpx)
.ForceFormat("matroska"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
@ -393,6 +413,7 @@ public class VideoTest
var success = FFMpegArguments
.FromFileInput(TestResources.Mp4Video)
.OutputToFile(outputFile, false)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -409,6 +430,7 @@ public class VideoTest
.CopyChannel()
.WithBitStreamFilter(Channel.Video, Filter.H264_Mp4ToAnnexB)
.ForceFormat(VideoType.MpegTs))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -420,7 +442,7 @@ public class VideoTest
[DataRow(PixelFormat.Format32bppArgb)]
public async Task Video_ToTS_Args_Pipe_WindowsOnly(PixelFormat pixelFormat)
{
await Video_ToTS_Args_Pipe_Internal(pixelFormat);
await Video_ToTS_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken);
}
[TestMethod]
@ -429,10 +451,10 @@ public class VideoTest
[DataRow(SKColorType.Bgra8888)]
public async Task Video_ToTS_Args_Pipe(SKColorType pixelFormat)
{
await Video_ToTS_Args_Pipe_Internal(pixelFormat);
await Video_ToTS_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken);
}
private static async Task Video_ToTS_Args_Pipe_Internal(dynamic pixelFormat)
private static async Task Video_ToTS_Args_Pipe_Internal(dynamic pixelFormat, CancellationToken cancellationToken)
{
using var output = new TemporaryFile($"out{VideoType.Ts.Extension}");
var input = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256));
@ -441,6 +463,7 @@ public class VideoTest
.FromPipeInput(input)
.OutputToFile(output, false, opt => opt
.ForceFormat(VideoType.Ts))
.CancellableThrough(cancellationToken)
.ProcessAsynchronously();
Assert.IsTrue(success);
@ -458,6 +481,7 @@ public class VideoTest
.OutputToFile(outputFile, false, opt => opt
.Resize(200, 200)
.WithVideoCodec(VideoCodec.LibTheora))
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously();
Assert.IsTrue(success);
}
@ -478,6 +502,7 @@ public class VideoTest
.WithVideoFilters(filterOptions => filterOptions
.Scale(VideoSize.Ed))
.WithVideoCodec(VideoCodec.LibTheora))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
var analysis = FFProbe.Analyse(outputFile);
@ -495,6 +520,7 @@ public class VideoTest
.OutputToFile(outputFile, false, opt => opt
.UsingMultithreading(true)
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -507,7 +533,7 @@ public class VideoTest
// [DataRow(PixelFormat.Format48bppRgb)]
public void Video_ToMP4_Resize_Args_Pipe(PixelFormat pixelFormat)
{
Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat);
Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken);
}
[TestMethod]
@ -516,10 +542,10 @@ public class VideoTest
[DataRow(SKColorType.Bgra8888)]
public void Video_ToMP4_Resize_Args_Pipe(SKColorType pixelFormat)
{
Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat);
Video_ToMP4_Resize_Args_Pipe_Internal(pixelFormat, TestContext.CancellationToken);
}
private static void Video_ToMP4_Resize_Args_Pipe_Internal(dynamic pixelFormat)
private static void Video_ToMP4_Resize_Args_Pipe_Internal(dynamic pixelFormat, CancellationToken cancellationToken)
{
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
var videoFramesSource = new RawVideoPipeSource(BitmapSource.CreateBitmaps(128, pixelFormat, 256, 256));
@ -528,6 +554,7 @@ public class VideoTest
.FromPipeInput(videoFramesSource)
.OutputToFile(outputFile, false, opt => opt
.WithVideoCodec(VideoCodec.LibX264))
.CancellableThrough(cancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
}
@ -756,7 +783,7 @@ public class VideoTest
Assert.IsTrue(success);
var result = FFProbe.Analyse(outputFile);
Assert.AreEqual(1, result.Duration.Seconds);
Assert.AreEqual(3, result.Duration.Seconds);
Assert.AreEqual(imageAnalysis.PrimaryVideoStream!.Width, result.PrimaryVideoStream!.Width);
Assert.AreEqual(imageAnalysis.PrimaryVideoStream!.Height, result.PrimaryVideoStream.Height);
}
@ -781,6 +808,7 @@ public class VideoTest
FFMpegArguments
.FromFileInput(TestResources.Mp4Video)
.OutputToFile(outputFile, false, opt => opt.WithDuration(TimeSpan.FromSeconds(video.Duration.TotalSeconds - 2)))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(File.Exists(outputFile));
@ -802,12 +830,12 @@ public class VideoTest
var timeDone = TimeSpan.Zero;
var analysis = FFProbe.Analyse(TestResources.Mp4Video);
var events = new List<double>();
void OnPercentageProgess(double percentage)
{
if (percentage < 100)
{
percentageDone = percentage;
}
events.Add(percentage);
percentageDone = percentage;
}
void OnTimeProgess(TimeSpan time)
@ -824,12 +852,16 @@ public class VideoTest
.WithDuration(analysis.Duration))
.NotifyOnProgress(OnPercentageProgess, analysis.Duration)
.NotifyOnProgress(OnTimeProgess)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(success);
Assert.IsTrue(File.Exists(outputFile));
Assert.AreNotEqual(0.0, percentageDone);
Assert.AreNotEqual(100.0, percentageDone);
Assert.IsGreaterThan(1, events.Count);
CollectionAssert.AllItemsAreUnique(events);
Assert.AreNotEqual(100.0, events.First());
Assert.AreEqual(100.0, events.Last(), 0.001);
Assert.AreNotEqual(TimeSpan.Zero, timeDone);
Assert.AreNotEqual(analysis.Duration, timeDone);
}
@ -849,6 +881,7 @@ public class VideoTest
.WithDuration(TimeSpan.FromSeconds(2)))
.NotifyOnError(_ => dataReceived = true)
.Configure(opt => opt.Encoding = Encoding.UTF8)
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
Assert.IsTrue(dataReceived);
@ -861,17 +894,17 @@ public class VideoTest
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_TranscodeInMemory_WindowsOnly()
{
Video_TranscodeInMemory_Internal(PixelFormat.Format24bppRgb);
Video_TranscodeInMemory_Internal(PixelFormat.Format24bppRgb, TestContext.CancellationToken);
}
[TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_TranscodeInMemory()
{
Video_TranscodeInMemory_Internal(SKColorType.Rgb565);
Video_TranscodeInMemory_Internal(SKColorType.Rgb565, TestContext.CancellationToken);
}
private static void Video_TranscodeInMemory_Internal(dynamic pixelFormat)
private static void Video_TranscodeInMemory_Internal(dynamic pixelFormat, CancellationToken cancellationToken)
{
using var resStream = new MemoryStream();
var reader = new StreamPipeSink(resStream);
@ -882,6 +915,7 @@ public class VideoTest
.OutputToPipe(reader, opt => opt
.WithVideoCodec("vp9")
.ForceFormat("webm"))
.CancellableThrough(cancellationToken)
.ProcessSynchronously();
resStream.Position = 0;
@ -901,6 +935,7 @@ public class VideoTest
.OutputToPipe(new StreamPipeSink(memoryStream), opt => opt
.WithVideoCodec("vp9")
.ForceFormat("webm"))
.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously();
memoryStream.Position = 0;
@ -924,6 +959,8 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(out var cancel)
.CancellableThrough(TestContext.CancellationToken)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(false);
await Task.Delay(300, TestContext.CancellationToken);
@ -947,11 +984,13 @@ public class VideoTest
.WithAudioCodec(AudioCodec.Aac)
.WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(out var cancel);
.CancellableThrough(out var cancel)
.CancellableThrough(TestContext.CancellationToken);
Task.Delay(300, TestContext.CancellationToken).ContinueWith(_ => cancel(), TestContext.CancellationToken);
var result = task.ProcessSynchronously(false);
var result = task.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously(false);
Assert.IsFalse(result);
}
@ -971,6 +1010,7 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(out var cancel, 10000)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(false);
await Task.Delay(300, TestContext.CancellationToken);
@ -1004,6 +1044,7 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(cts.Token)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously(false);
cts.CancelAfter(300);
@ -1030,6 +1071,7 @@ public class VideoTest
.WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(cts.Token)
.CancellableThrough(TestContext.CancellationToken)
.ProcessAsynchronously();
cts.CancelAfter(300);
@ -1057,7 +1099,31 @@ public class VideoTest
cts.CancelAfter(300);
Assert.ThrowsExactly<OperationCanceledException>(() => task.ProcessSynchronously());
Assert.ThrowsExactly<OperationCanceledException>(() => task.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
[TestMethod]
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
public void Video_Cancel_CancellationToken_Before_Throws()
{
using var outputFile = new TemporaryFile("out.mp4");
var cts = new CancellationTokenSource();
cts.Cancel();
var task = FFMpegArguments
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
.WithCustomArgument("-re")
.ForceFormat("lavfi"))
.OutputToFile(outputFile, false, opt => opt
.WithAudioCodec(AudioCodec.Aac)
.WithVideoCodec(VideoCodec.LibX264)
.WithSpeedPreset(Speed.VeryFast))
.CancellableThrough(cts.Token);
Assert.ThrowsExactly<OperationCanceledException>(() => task.CancellableThrough(TestContext.CancellationToken)
.ProcessSynchronously());
}
[TestMethod]

View file

@ -4,12 +4,11 @@ namespace FFMpegCore.Arguments;
public class GifPaletteArgument : IArgument
{
private readonly int _fps;
private readonly double _fps;
private readonly Size? _size;
private readonly int _streamIndex;
public GifPaletteArgument(int streamIndex, int fps, Size? size)
public GifPaletteArgument(int streamIndex, double fps, Size? size)
{
_streamIndex = streamIndex;
_fps = fps;

View file

@ -132,7 +132,8 @@ public static class FFMpeg
}
return FFMpegArguments
.FromFileInput(Path.Combine(tempFolderName, $"%09d{fileExtension}"), false)
.FromFileInput(Path.Combine(tempFolderName, $"%09d{fileExtension}"), false, options => options
.WithFramerate(frameRate))
.OutputToFile(output, true, options => options
.ForcePixelFormat("yuv420p")
.Resize(width!.Value, height!.Value)

View file

@ -258,7 +258,7 @@ public class FFMpegArgumentOptions : FFMpegArgumentsBase
return WithArgument(new ID3V2VersionArgument(id3v2Version));
}
public FFMpegArgumentOptions WithGifPaletteArgument(int streamIndex, Size? size, int fps = 12)
public FFMpegArgumentOptions WithGifPaletteArgument(int streamIndex, Size? size, double fps = 12)
{
return WithArgument(new GifPaletteArgument(streamIndex, fps, size));
}

View file

@ -12,6 +12,8 @@ public class FFMpegArgumentProcessor
private static readonly Regex ProgressRegex = new(@"time=(\d\d:\d\d:\d\d.\d\d?)", RegexOptions.Compiled);
private readonly List<Action<FFOptions>> _configurations;
private readonly FFMpegArguments _ffMpegArguments;
private CancellationTokenRegistration? _cancellationTokenRegistration;
private bool _cancelled;
private FFMpegLogLevel? _logLevel;
private Action<string>? _onError;
private Action<string>? _onOutput;
@ -69,15 +71,22 @@ public class FFMpegArgumentProcessor
return this;
}
private void Cancel(int timeout)
{
_cancelled = true;
CancelEvent?.Invoke(this, timeout);
}
public FFMpegArgumentProcessor CancellableThrough(out Action cancel, int timeout = 0)
{
cancel = () => CancelEvent?.Invoke(this, timeout);
cancel = () => Cancel(timeout);
return this;
}
public FFMpegArgumentProcessor CancellableThrough(CancellationToken token, int timeout = 0)
{
token.Register(() => CancelEvent?.Invoke(this, timeout));
_cancellationTokenRegistration?.Dispose();
_cancellationTokenRegistration = token.Register(() => Cancel(timeout));
return this;
}
@ -101,7 +110,8 @@ public class FFMpegArgumentProcessor
public bool ProcessSynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
{
var options = GetConfiguredOptions(ffMpegOptions);
var processArguments = PrepareProcessArguments(options, out var cancellationTokenSource);
var processArguments = PrepareProcessArguments(options);
using var cancellationTokenSource = new CancellationTokenSource();
IProcessResult? processResult = null;
try
@ -122,7 +132,8 @@ public class FFMpegArgumentProcessor
public async Task<bool> ProcessAsynchronously(bool throwOnError = true, FFOptions? ffMpegOptions = null)
{
var options = GetConfiguredOptions(ffMpegOptions);
var processArguments = PrepareProcessArguments(options, out var cancellationTokenSource);
var processArguments = PrepareProcessArguments(options);
using var cancellationTokenSource = new CancellationTokenSource();
IProcessResult? processResult = null;
try
@ -143,15 +154,18 @@ public class FFMpegArgumentProcessor
private async Task<IProcessResult> Process(ProcessArguments processArguments, CancellationTokenSource cancellationTokenSource)
{
IProcessResult processResult = null!;
if (_cancelled)
{
_cancellationTokenRegistration?.Dispose();
throw new OperationCanceledException("cancelled before starting processing");
}
_ffMpegArguments.Pre();
using var instance = processArguments.Start();
var cancelled = false;
void OnCancelEvent(object sender, int timeout)
{
cancelled = true;
instance.SendInput("q");
if (!cancellationTokenSource.Token.WaitHandle.WaitOne(timeout, true))
@ -172,8 +186,9 @@ public class FFMpegArgumentProcessor
_ffMpegArguments.Post();
}), _ffMpegArguments.During(cancellationTokenSource.Token)).ConfigureAwait(false);
if (cancelled)
if (_cancelled)
{
_cancellationTokenRegistration?.Dispose();
throw new OperationCanceledException("ffmpeg processing was cancelled");
}
@ -182,6 +197,7 @@ public class FFMpegArgumentProcessor
finally
{
CancelEvent -= OnCancelEvent;
_cancellationTokenRegistration?.Dispose();
}
}
@ -214,8 +230,7 @@ public class FFMpegArgumentProcessor
return options;
}
private ProcessArguments PrepareProcessArguments(FFOptions ffOptions,
out CancellationTokenSource cancellationTokenSource)
private ProcessArguments PrepareProcessArguments(FFOptions ffOptions)
{
FFMpegHelper.RootExceptionCheck();
FFMpegHelper.VerifyFFMpegExists(ffOptions);
@ -245,7 +260,6 @@ public class FFMpegArgumentProcessor
WorkingDirectory = ffOptions.WorkingDirectory
};
var processArguments = new ProcessArguments(startInfo);
cancellationTokenSource = new CancellationTokenSource();
if (_onOutput != null)
{

View file

@ -62,7 +62,7 @@ public static class SnapshotArgumentBuilder
TimeSpan? captureTime = null,
TimeSpan? duration = null,
int? streamIndex = null,
int fps = 12)
double fps = 12)
{
var defaultGifOutputSize = new Size(480, -1);

View file

@ -151,7 +151,7 @@ public static class TagExtensions
public static string? GetCreationTime(this ITagsContainer tagsContainer)
{
return TryGetTagValue(tagsContainer, "creation_time ");
return TryGetTagValue(tagsContainer, "creation_time");
}
public static string? GetRotate(this ITagsContainer tagsContainer)