mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2025-12-14 18:15:44 +00:00
Merge branch 'main' into PR-592_Snapshot_Cancellation_Support
This commit is contained in:
commit
935e1cf17c
7 changed files with 183 additions and 14 deletions
|
|
@ -1,4 +1,6 @@
|
||||||
using FFMpegCore.Test.Resources;
|
using FFMpegCore.Exceptions;
|
||||||
|
using FFMpegCore.Helpers;
|
||||||
|
using FFMpegCore.Test.Resources;
|
||||||
|
|
||||||
namespace FFMpegCore.Test;
|
namespace FFMpegCore.Test;
|
||||||
|
|
||||||
|
|
@ -285,4 +287,68 @@ public class FFProbeTests
|
||||||
var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: World\"");
|
var info = FFProbe.Analyse(TestResources.Mp4Video, customArguments: "-headers \"Hello: World\"");
|
||||||
Assert.AreEqual(3, info.Duration.Seconds);
|
Assert.AreEqual(3, info.Duration.Seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[Timeout(10000, CooperativeCancellation = true)]
|
||||||
|
public async Task Parallel_FFProbe_Cancellation_Should_Throw_Only_OperationCanceledException()
|
||||||
|
{
|
||||||
|
// Warm up FFMpegCore environment
|
||||||
|
FFProbeHelper.VerifyFFProbeExists(GlobalFFOptions.Current);
|
||||||
|
|
||||||
|
var mp4 = TestResources.Mp4Video;
|
||||||
|
if (!File.Exists(mp4))
|
||||||
|
{
|
||||||
|
Assert.Inconclusive($"Test video not found: {mp4}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
|
||||||
|
using var semaphore = new SemaphoreSlim(Environment.ProcessorCount, Environment.ProcessorCount);
|
||||||
|
var tasks = Enumerable.Range(0, 50).Select(x => Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await semaphore.WaitAsync(cts.Token);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var analysis = await FFProbe.AnalyseAsync(mp4, cancellationToken: cts.Token);
|
||||||
|
return analysis;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
semaphore.Release();
|
||||||
|
}
|
||||||
|
}, cts.Token)).ToList();
|
||||||
|
|
||||||
|
// Wait for 2 tasks to finish, then cancel all
|
||||||
|
await Task.WhenAny(tasks);
|
||||||
|
await Task.WhenAny(tasks);
|
||||||
|
await cts.CancelAsync();
|
||||||
|
|
||||||
|
var exceptions = new List<Exception>();
|
||||||
|
foreach (var task in tasks)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await task;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
exceptions.Add(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.IsNotEmpty(exceptions, "No exceptions were thrown on cancellation. Test was useless. " +
|
||||||
|
".Try adjust cancellation timings to make cancellation at the moment, when ffprobe is still running.");
|
||||||
|
|
||||||
|
// Check that all exceptions are OperationCanceledException
|
||||||
|
CollectionAssert.AllItemsAreInstancesOfType(exceptions, typeof(OperationCanceledException));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[Timeout(10000, CooperativeCancellation = true)]
|
||||||
|
public async Task FFProbe_Should_Throw_FFMpegException_When_Exits_With_Non_Zero_Code()
|
||||||
|
{
|
||||||
|
var input = TestResources.SrtSubtitle; //non media file
|
||||||
|
await Assert.ThrowsAsync<FFMpegException>(async () => await FFProbe.AnalyseAsync(input,
|
||||||
|
cancellationToken: TestContext.CancellationToken, customArguments: "--some-invalid-argument"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,40 @@ public class VideoTest
|
||||||
Assert.IsTrue(success);
|
Assert.IsTrue(success);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
|
||||||
|
public async Task Video_MetadataBuilder()
|
||||||
|
{
|
||||||
|
using var outputFile = new TemporaryFile($"out{VideoType.Mp4.Extension}");
|
||||||
|
|
||||||
|
await FFMpegArguments
|
||||||
|
.FromFileInput(TestResources.WebmVideo)
|
||||||
|
.AddMetaData(FFMetadataBuilder.Empty()
|
||||||
|
.WithTag("title", "noname")
|
||||||
|
.WithTag("artist", "unknown")
|
||||||
|
.WithChapter("Chapter 1", 1.1)
|
||||||
|
.WithChapter("Chapter 2", 1.23))
|
||||||
|
.OutputToFile(outputFile, false, opt => opt
|
||||||
|
.WithVideoCodec(VideoCodec.LibX264))
|
||||||
|
.CancellableThrough(TestContext.CancellationToken)
|
||||||
|
.ProcessAsynchronously();
|
||||||
|
|
||||||
|
var analysis = await FFProbe.AnalyseAsync(outputFile, cancellationToken: TestContext.CancellationToken);
|
||||||
|
Assert.IsTrue(analysis.Format.Tags!.TryGetValue("title", out var title));
|
||||||
|
Assert.IsTrue(analysis.Format.Tags!.TryGetValue("artist", out var artist));
|
||||||
|
Assert.AreEqual("noname", title);
|
||||||
|
Assert.AreEqual("unknown", artist);
|
||||||
|
|
||||||
|
Assert.HasCount(2, analysis.Chapters);
|
||||||
|
Assert.AreEqual("Chapter 1", analysis.Chapters.First().Title);
|
||||||
|
Assert.AreEqual(1.1, analysis.Chapters.First().Duration.TotalSeconds);
|
||||||
|
Assert.AreEqual(1.1, analysis.Chapters.First().End.TotalSeconds);
|
||||||
|
|
||||||
|
Assert.AreEqual("Chapter 2", analysis.Chapters.Last().Title);
|
||||||
|
Assert.AreEqual(1.23, analysis.Chapters.Last().Duration.TotalSeconds);
|
||||||
|
Assert.AreEqual(1.1 + 1.23, analysis.Chapters.Last().End.TotalSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
|
[Timeout(BaseTimeoutMilliseconds, CooperativeCancellation = true)]
|
||||||
public void Video_ToH265_MKV_Args()
|
public void Video_ToH265_MKV_Args()
|
||||||
|
|
@ -1018,7 +1052,7 @@ public class VideoTest
|
||||||
{
|
{
|
||||||
using var outputFile = new TemporaryFile("out.mp4");
|
using var outputFile = new TemporaryFile("out.mp4");
|
||||||
|
|
||||||
var cts = new CancellationTokenSource();
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
|
||||||
|
|
||||||
var task = FFMpegArguments
|
var task = FFMpegArguments
|
||||||
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
||||||
|
|
@ -1029,7 +1063,6 @@ public class VideoTest
|
||||||
.WithVideoCodec(VideoCodec.LibX264)
|
.WithVideoCodec(VideoCodec.LibX264)
|
||||||
.WithSpeedPreset(Speed.VeryFast))
|
.WithSpeedPreset(Speed.VeryFast))
|
||||||
.CancellableThrough(cts.Token)
|
.CancellableThrough(cts.Token)
|
||||||
.CancellableThrough(TestContext.CancellationToken)
|
|
||||||
.ProcessAsynchronously(false);
|
.ProcessAsynchronously(false);
|
||||||
|
|
||||||
cts.CancelAfter(300);
|
cts.CancelAfter(300);
|
||||||
|
|
@ -1045,7 +1078,7 @@ public class VideoTest
|
||||||
{
|
{
|
||||||
using var outputFile = new TemporaryFile("out.mp4");
|
using var outputFile = new TemporaryFile("out.mp4");
|
||||||
|
|
||||||
var cts = new CancellationTokenSource();
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
|
||||||
|
|
||||||
var task = FFMpegArguments
|
var task = FFMpegArguments
|
||||||
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
||||||
|
|
@ -1056,7 +1089,6 @@ public class VideoTest
|
||||||
.WithVideoCodec(VideoCodec.LibX264)
|
.WithVideoCodec(VideoCodec.LibX264)
|
||||||
.WithSpeedPreset(Speed.VeryFast))
|
.WithSpeedPreset(Speed.VeryFast))
|
||||||
.CancellableThrough(cts.Token)
|
.CancellableThrough(cts.Token)
|
||||||
.CancellableThrough(TestContext.CancellationToken)
|
|
||||||
.ProcessAsynchronously();
|
.ProcessAsynchronously();
|
||||||
|
|
||||||
cts.CancelAfter(300);
|
cts.CancelAfter(300);
|
||||||
|
|
@ -1070,7 +1102,7 @@ public class VideoTest
|
||||||
{
|
{
|
||||||
using var outputFile = new TemporaryFile("out.mp4");
|
using var outputFile = new TemporaryFile("out.mp4");
|
||||||
|
|
||||||
var cts = new CancellationTokenSource();
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
|
||||||
|
|
||||||
var task = FFMpegArguments
|
var task = FFMpegArguments
|
||||||
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
||||||
|
|
@ -1094,7 +1126,7 @@ public class VideoTest
|
||||||
{
|
{
|
||||||
using var outputFile = new TemporaryFile("out.mp4");
|
using var outputFile = new TemporaryFile("out.mp4");
|
||||||
|
|
||||||
var cts = new CancellationTokenSource();
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
|
||||||
|
|
||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
var task = FFMpegArguments
|
var task = FFMpegArguments
|
||||||
|
|
@ -1117,7 +1149,7 @@ public class VideoTest
|
||||||
{
|
{
|
||||||
using var outputFile = new TemporaryFile("out.mp4");
|
using var outputFile = new TemporaryFile("out.mp4");
|
||||||
|
|
||||||
var cts = new CancellationTokenSource();
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.CancellationToken);
|
||||||
|
|
||||||
var task = FFMpegArguments
|
var task = FFMpegArguments
|
||||||
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
.FromFileInput("testsrc2=size=320x240[out0]; sine[out1]", false, args => args
|
||||||
|
|
|
||||||
62
FFMpegCore/FFMetadataInputArgument.cs
Normal file
62
FFMpegCore/FFMetadataInputArgument.cs
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace FFMpegCore;
|
||||||
|
|
||||||
|
public class FFMetadataBuilder
|
||||||
|
{
|
||||||
|
private Dictionary<string, string> Tags { get; } = new();
|
||||||
|
private List<FFMetadataChapter> Chapters { get; } = [];
|
||||||
|
|
||||||
|
public static FFMetadataBuilder Empty()
|
||||||
|
{
|
||||||
|
return new FFMetadataBuilder();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FFMetadataBuilder WithTag(string key, string value)
|
||||||
|
{
|
||||||
|
Tags.Add(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FFMetadataBuilder WithChapter(string title, long durationMs)
|
||||||
|
{
|
||||||
|
Chapters.Add(new FFMetadataChapter(title, durationMs));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FFMetadataBuilder WithChapter(string title, double durationSeconds)
|
||||||
|
{
|
||||||
|
Chapters.Add(new FFMetadataChapter(title, Convert.ToInt64(durationSeconds * 1000)));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetMetadataFileContent()
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
sb.AppendLine(";FFMETADATA1");
|
||||||
|
|
||||||
|
foreach (var tag in Tags)
|
||||||
|
{
|
||||||
|
sb.AppendLine($"{tag.Key}={tag.Value}");
|
||||||
|
}
|
||||||
|
|
||||||
|
long totalDurationMs = 0;
|
||||||
|
foreach (var chapter in Chapters)
|
||||||
|
{
|
||||||
|
sb.AppendLine("[CHAPTER]");
|
||||||
|
sb.AppendLine("TIMEBASE=1/1000");
|
||||||
|
sb.AppendLine($"START={totalDurationMs}");
|
||||||
|
sb.AppendLine($"END={totalDurationMs + chapter.DurationMs}");
|
||||||
|
sb.AppendLine($"title={chapter.Title}");
|
||||||
|
totalDurationMs += chapter.DurationMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class FFMetadataChapter(string title, long durationMs)
|
||||||
|
{
|
||||||
|
public string Title { get; } = title;
|
||||||
|
public long DurationMs { get; } = durationMs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -109,6 +109,11 @@ public sealed class FFMpegArguments : FFMpegArgumentsBase
|
||||||
return WithInput(new MetaDataArgument(content), addArguments);
|
return WithInput(new MetaDataArgument(content), addArguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FFMpegArguments AddMetaData(FFMetadataBuilder metaDataBuilder, Action<FFMpegArgumentOptions>? addArguments = null)
|
||||||
|
{
|
||||||
|
return WithInput(new MetaDataArgument(metaDataBuilder.GetMetadataFileContent()), addArguments);
|
||||||
|
}
|
||||||
|
|
||||||
public FFMpegArguments AddMetaData(IReadOnlyMetaData metaData, Action<FFMpegArgumentOptions>? addArguments = null)
|
public FFMpegArguments AddMetaData(IReadOnlyMetaData metaData, Action<FFMpegArgumentOptions>? addArguments = null)
|
||||||
{
|
{
|
||||||
return WithInput(new MetaDataArgument(MetaDataSerializer.Instance.Serialize(metaData)), addArguments);
|
return WithInput(new MetaDataArgument(MetaDataSerializer.Instance.Serialize(metaData)), addArguments);
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,14 @@
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<IsPackable>true</IsPackable>
|
<IsPackable>true</IsPackable>
|
||||||
<Description>A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications</Description>
|
<Description>A .NET Standard FFMpeg/FFProbe wrapper for easily integrating media analysis and conversion into your .NET applications</Description>
|
||||||
<PackageVersion>5.3.0</PackageVersion>
|
<PackageVersion>5.4.0</PackageVersion>
|
||||||
<PackageOutputPath>../nupkg</PackageOutputPath>
|
<PackageOutputPath>../nupkg</PackageOutputPath>
|
||||||
<PackageReleaseNotes>
|
<PackageReleaseNotes>
|
||||||
- **Fixed race condition on Named pipe dispose/disconnect** by techtel-pstevens
|
- Fixed exception thrown on cancelling ffprobe analysis - by snechaev
|
||||||
- **More extensions for snapshot function(jpg, bmp, webp)** by GorobVictor
|
- Added FFMetadataBuilder - by rosenbjerg
|
||||||
- **Include more GUID characters in pipe path** by reima, rosenbjerg
|
- Fix JoinImageSequence by passing framerate argument to input as well as output - by rosenbjerg
|
||||||
- **Updated dependencies and minor cleanup**: by rosenbjerg
|
- Change fps input from int to double - by rosenbjerg
|
||||||
|
- Fix GetCreationTime method on ITagsContainer - by rosenbjerg
|
||||||
</PackageReleaseNotes>
|
</PackageReleaseNotes>
|
||||||
<PackageTags>ffmpeg ffprobe convert video audio mediafile resize analyze muxing</PackageTags>
|
<PackageTags>ffmpeg ffprobe convert video audio mediafile resize analyze muxing</PackageTags>
|
||||||
<Authors>Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev</Authors>
|
<Authors>Malte Rosenbjerg, Vlad Jerca, Max Bagryantsev</Authors>
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,7 @@ public static class FFProbe
|
||||||
|
|
||||||
var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments);
|
var instance = PrepareStreamAnalysisInstance(filePath, ffOptions ?? GlobalFFOptions.Current, customArguments);
|
||||||
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
|
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
ThrowIfExitCodeNotZero(result);
|
ThrowIfExitCodeNotZero(result);
|
||||||
|
|
||||||
return ParseOutput(result);
|
return ParseOutput(result);
|
||||||
|
|
@ -123,6 +124,7 @@ public static class FFProbe
|
||||||
{
|
{
|
||||||
var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments);
|
var instance = PrepareStreamAnalysisInstance(uri.AbsoluteUri, ffOptions ?? GlobalFFOptions.Current, customArguments);
|
||||||
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
|
var result = await instance.StartAndWaitForExitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
ThrowIfExitCodeNotZero(result);
|
ThrowIfExitCodeNotZero(result);
|
||||||
|
|
||||||
return ParseOutput(result);
|
return ParseOutput(result);
|
||||||
|
|
@ -150,6 +152,7 @@ public static class FFProbe
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = await task.ConfigureAwait(false);
|
var result = await task.ConfigureAwait(false);
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
ThrowIfExitCodeNotZero(result);
|
ThrowIfExitCodeNotZero(result);
|
||||||
|
|
||||||
pipeArgument.Post();
|
pipeArgument.Post();
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,6 @@ public static class ProcessArgumentsExtensions
|
||||||
public static async Task<IProcessResult> StartAndWaitForExitAsync(this ProcessArguments processArguments, CancellationToken cancellationToken = default)
|
public static async Task<IProcessResult> StartAndWaitForExitAsync(this ProcessArguments processArguments, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
using var instance = processArguments.Start();
|
using var instance = processArguments.Start();
|
||||||
return await instance.WaitForExitAsync(cancellationToken);
|
return await instance.WaitForExitAsync(cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue