mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2024-11-10 00:24:14 +01:00
Merge pull request #292 from JKamsker/fix/dynamic_argumentText
Using correct map_metadata dynamically
This commit is contained in:
commit
6fb653e3aa
5 changed files with 76 additions and 8 deletions
|
@ -4,8 +4,10 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FFMpegCore.Test
|
||||
|
@ -50,5 +52,29 @@ public void TestMetaDataBuilderIntegrity()
|
|||
Assert.IsTrue(serialized.Contains("title=Chapter 01", StringComparison.OrdinalIgnoreCase));
|
||||
Assert.IsTrue(serialized.Contains("album_artist=Pachelbel", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestMapMetadata()
|
||||
{
|
||||
//-i "whaterver0" // index: 0
|
||||
//-f concat -safe 0
|
||||
//-i "\AppData\Local\Temp\concat_b511f2bf-c4af-4f71-b9bd-24d706bf4861.txt" // index: 1
|
||||
//-i "\AppData\Local\Temp\metadata_210d3259-3d5c-43c8-9786-54b5c414fa70.txt" // index: 2
|
||||
//-map_metadata 2
|
||||
|
||||
var text0 = FFMpegArguments.FromFileInput("whaterver0")
|
||||
.AddMetaData("WhatEver3")
|
||||
.Text;
|
||||
|
||||
var text1 = FFMpegArguments.FromFileInput("whaterver0")
|
||||
.AddDemuxConcatInput(new[] { "whaterver", "whaterver1" })
|
||||
.AddMetaData("WhatEver3")
|
||||
.Text;
|
||||
|
||||
|
||||
|
||||
Assert.IsTrue(Regex.IsMatch(text0, "metadata_[0-9a-f-]+\\.txt\" -map_metadata 1"), "map_metadata index is calculated incorrectly.");
|
||||
Assert.IsTrue(Regex.IsMatch(text1, "metadata_[0-9a-f-]+\\.txt\" -map_metadata 2"), "map_metadata index is calculated incorrectly.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FFMpegCore.Extend
|
||||
|
|
16
FFMpegCore/FFMpeg/Arguments/IDynamicArgument.cs
Normal file
16
FFMpegCore/FFMpeg/Arguments/IDynamicArgument.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
public interface IDynamicArgument
|
||||
{
|
||||
/// <summary>
|
||||
/// Same as <see cref="IArgument.Text"/>, but this receives the arguments generated before as parameter
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
//public string GetText(StringBuilder context);
|
||||
public string GetText(IEnumerable<IArgument> context);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,16 @@
|
|||
using System;
|
||||
using FFMpegCore.Extend;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
public class MetaDataArgument : IInputArgument
|
||||
public class MetaDataArgument : IInputArgument, IDynamicArgument
|
||||
{
|
||||
private readonly string _metaDataContent;
|
||||
private readonly string _tempFileName = Path.Combine(GlobalFFOptions.Current.TemporaryFilesFolder, $"metadata_{Guid.NewGuid()}.txt");
|
||||
|
@ -15,7 +20,7 @@ public MetaDataArgument(string metaDataContent)
|
|||
_metaDataContent = metaDataContent;
|
||||
}
|
||||
|
||||
public string Text => $"-i \"{_tempFileName}\" -map_metadata 1";
|
||||
public string Text => GetText(null);
|
||||
|
||||
public Task During(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
||||
|
||||
|
@ -23,5 +28,17 @@ public MetaDataArgument(string metaDataContent)
|
|||
public void Pre() => File.WriteAllText(_tempFileName, _metaDataContent);
|
||||
|
||||
public void Post() => File.Delete(_tempFileName);
|
||||
|
||||
public string GetText(IEnumerable<IArgument>? arguments)
|
||||
{
|
||||
arguments ??= Enumerable.Empty<IArgument>();
|
||||
|
||||
var index = arguments
|
||||
.TakeWhile(x => x != this)
|
||||
.OfType<IInputArgument>()
|
||||
.Count();
|
||||
|
||||
return $"-i \"{_tempFileName}\" -map_metadata {index}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using FFMpegCore.Arguments;
|
||||
using FFMpegCore.Builders.MetaData;
|
||||
using FFMpegCore.Pipes;
|
||||
|
@ -13,10 +15,16 @@ namespace FFMpegCore
|
|||
public sealed class FFMpegArguments : FFMpegArgumentsBase
|
||||
{
|
||||
private readonly FFMpegGlobalArguments _globalArguments = new FFMpegGlobalArguments();
|
||||
|
||||
|
||||
private FFMpegArguments() { }
|
||||
|
||||
public string Text => string.Join(" ", _globalArguments.Arguments.Concat(Arguments).Select(arg => arg.Text));
|
||||
public string Text => GetText();
|
||||
|
||||
private string GetText()
|
||||
{
|
||||
var allArguments = _globalArguments.Arguments.Concat(Arguments).ToArray();
|
||||
return string.Join(" ", allArguments.Select(arg => arg is IDynamicArgument dynArg ? dynArg.GetText(allArguments) : arg.Text));
|
||||
}
|
||||
|
||||
public static FFMpegArguments FromConcatInput(IEnumerable<string> filePaths, Action<FFMpegArgumentOptions>? addArguments = null) => new FFMpegArguments().WithInput(new ConcatArgument(filePaths), addArguments);
|
||||
public static FFMpegArguments FromDemuxConcatInput(IEnumerable<string> filePaths, Action<FFMpegArgumentOptions>? addArguments = null) => new FFMpegArguments().WithInput(new DemuxConcatArgument(filePaths), addArguments);
|
||||
|
@ -26,7 +34,7 @@ private FFMpegArguments() { }
|
|||
public static FFMpegArguments FromDeviceInput(string device, Action<FFMpegArgumentOptions>? addArguments = null) => new FFMpegArguments().WithInput(new InputDeviceArgument(device), addArguments);
|
||||
public static FFMpegArguments FromPipeInput(IPipeSource sourcePipe, Action<FFMpegArgumentOptions>? addArguments = null) => new FFMpegArguments().WithInput(new InputPipeArgument(sourcePipe), addArguments);
|
||||
|
||||
|
||||
|
||||
public FFMpegArguments WithGlobalOptions(Action<FFMpegGlobalArguments> configureOptions)
|
||||
{
|
||||
configureOptions(_globalArguments);
|
||||
|
@ -81,4 +89,4 @@ internal void Post()
|
|||
argument.Post();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue