mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2025-12-16 11:05:44 +00:00
Compare commits
11 commits
65cbc5552c
...
67af2aa01d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
67af2aa01d | ||
|
|
560c791802 | ||
|
|
e44611bd25 | ||
|
|
b863f5d19e | ||
|
|
930d493b8c | ||
|
|
2f06ec99f3 | ||
|
|
53445322e4 | ||
|
|
15acd9f0da | ||
|
|
ef313ea411 | ||
|
|
62e829d9b4 | ||
|
|
97053929a9 |
4 changed files with 108 additions and 7 deletions
|
|
@ -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()
|
||||||
|
|
|
||||||
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);
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,8 @@ 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);
|
||||||
ThrowIfExitCodeNotZero(result, cancellationToken);
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
ThrowIfExitCodeNotZero(result);
|
||||||
|
|
||||||
return ParseOutput(result);
|
return ParseOutput(result);
|
||||||
}
|
}
|
||||||
|
|
@ -123,7 +124,8 @@ 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);
|
||||||
ThrowIfExitCodeNotZero(result, cancellationToken);
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
ThrowIfExitCodeNotZero(result);
|
||||||
|
|
||||||
return ParseOutput(result);
|
return ParseOutput(result);
|
||||||
}
|
}
|
||||||
|
|
@ -150,7 +152,8 @@ public static class FFProbe
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = await task.ConfigureAwait(false);
|
var result = await task.ConfigureAwait(false);
|
||||||
ThrowIfExitCodeNotZero(result, cancellationToken);
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
ThrowIfExitCodeNotZero(result);
|
||||||
|
|
||||||
pipeArgument.Post();
|
pipeArgument.Post();
|
||||||
return ParseOutput(result);
|
return ParseOutput(result);
|
||||||
|
|
@ -212,13 +215,10 @@ public static class FFProbe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ThrowIfExitCodeNotZero(IProcessResult result, CancellationToken cancellationToken = default)
|
private static void ThrowIfExitCodeNotZero(IProcessResult result)
|
||||||
{
|
{
|
||||||
if (result.ExitCode != 0)
|
if (result.ExitCode != 0)
|
||||||
{
|
{
|
||||||
// if cancellation requested, then we are not interested in the exit code, just throw the cancellation exception
|
|
||||||
// to get consistent and expected behavior.
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
var message = $"ffprobe exited with non-zero exit-code ({result.ExitCode} - {string.Join("\n", result.ErrorData)})";
|
var message = $"ffprobe exited with non-zero exit-code ({result.ExitCode} - {string.Join("\n", result.ErrorData)})";
|
||||||
throw new FFMpegException(FFMpegExceptionType.Process, message, null, string.Join("\n", result.ErrorData));
|
throw new FFMpegException(FFMpegExceptionType.Process, message, null, string.Join("\n", result.ErrorData));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue