Applied reqested changes

This commit is contained in:
Jonas Kamsker 2022-03-20 19:31:15 +01:00
parent c05337562c
commit 5b0d3d21c9
4 changed files with 31 additions and 23 deletions

View file

@ -93,12 +93,9 @@ public static int CountOccurrences(this string s, string substring,
bool aggressiveSearch = false) bool aggressiveSearch = false)
{ {
// if s or substring is null or empty, substring cannot be found in s // if s or substring is null or empty, substring cannot be found in s
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(substring))
return 0;
// if the length of substring is greater than the length of s, // if the length of substring is greater than the length of s,
// substring cannot be found in s // substring cannot be found in s
if (substring.Length > s.Length) if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(substring) || substring.Length > s.Length)
return 0; return 0;
int count = 0, n = 0; int count = 0, n = 0;

View file

@ -1,4 +1,5 @@
using System.Text; using System.Collections.Generic;
using System.Text;
namespace FFMpegCore.Arguments namespace FFMpegCore.Arguments
{ {
@ -9,6 +10,7 @@ public interface IDynamicArgument
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
/// <returns></returns> /// <returns></returns>
public string GetText(StringBuilder context); //public string GetText(StringBuilder context);
public string GetText(IEnumerable<IArgument> context);
} }
} }

View file

@ -1,6 +1,7 @@
using FFMpegCore.Extend; using FFMpegCore.Extend;
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -28,9 +29,14 @@ public MetaDataArgument(string metaDataContent)
public void Post() => File.Delete(_tempFileName); public void Post() => File.Delete(_tempFileName);
public string GetText(StringBuilder context) public string GetText(IEnumerable<IArgument>? arguments)
{ {
var index = context.ToString().CountOccurrences("-i"); arguments ??= Enumerable.Empty<IArgument>();
var index = arguments
.TakeWhile(x => x != this)
.OfType<IInputArgument>()
.Count();
return $"-i \"{_tempFileName}\" -map_metadata {index}"; return $"-i \"{_tempFileName}\" -map_metadata {index}";
} }

View file

@ -22,24 +22,27 @@ private FFMpegArguments() { }
private string GetText() private string GetText()
{ {
var sb = new StringBuilder(); //var sb = new StringBuilder();
var appendSpace = false; //var appendSpace = false;
foreach (var arg in _globalArguments.Arguments.Concat(Arguments)) //foreach (var arg in _globalArguments.Arguments.Concat(Arguments))
{ //{
if (appendSpace) // if (appendSpace)
{ // {
sb.Append(' '); // sb.Append(' ');
} // }
else // else
{ // {
appendSpace = true; // appendSpace = true;
} // }
sb.Append(arg is IDynamicArgument dynArg ? dynArg.GetText(sb) : arg.Text); // sb.Append(arg is IDynamicArgument dynArg ? dynArg.GetText(sb) : arg.Text);
} //}
return sb.ToString(); //return sb.ToString();
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 FromConcatInput(IEnumerable<string> filePaths, Action<FFMpegArgumentOptions>? addArguments = null) => new FFMpegArguments().WithInput(new ConcatArgument(filePaths), addArguments);