FF-1785: Created a track remover based on the TrackSelectorFlowElement.cs

This commit is contained in:
John Andrews
2024-09-26 13:05:15 +12:00
parent 976ac481d8
commit fd86919ace
5 changed files with 137 additions and 6 deletions

View File

@@ -180,8 +180,6 @@ public class FfmpegBuilderAudioConvert : TrackSelectorFlowElement<FfmpegBuilderA
public override int Execute(NodeParameters args)
{
bool converting = false;
Regex? regex = null;
foreach (var track in Model.AudioStreams)
{

View File

@@ -4,10 +4,19 @@ namespace FileFlows.VideoNodes.FfmpegBuilderNodes;
public class FfmpegBuilderAudioTrackRemover: FfmpegBuilderNode
{
public override string HelpUrl => "https://fileflows.com/docs/plugins/video-nodes/ffmpeg-builder/track-remover";
/// <inheritdoc />
public override bool Obsolete => true;
/// <inheritdoc />
public override string ObsoleteMessage => "Has been replaced with `Track Remover`";
/// <inheritdoc />
public override string HelpUrl => "https://fileflows.com/docs/plugins/video-nodes/ffmpeg-builder/track-remover-obsolete";
/// <inheritdoc />
public override string Icon => "fas fa-eraser";
/// <inheritdoc />
public override int Outputs => 2;

View File

@@ -134,8 +134,9 @@ public abstract class TrackSelectorFlowElement<T> : FfmpegBuilderNode where T :
/// Tests if a stream matches the specified conditions
/// </summary>
/// <param name="stream">the stream to check</param>
/// <param name="index">the index of the stream in the model</param>
/// <returns>true if matches, otherwise false</returns>
protected bool StreamMatches(IVideoStream stream)
protected bool StreamMatches(IVideoStream stream, int? index = null)
{
foreach (var kv in TrackSelectionOptions)
{
@@ -182,8 +183,24 @@ public abstract class TrackSelectorFlowElement<T> : FfmpegBuilderNode where T :
Args?.Logger?.ILog($"Channels does not match '{stream}' = {kvValue}");
return false;
}
break;
case "index":
{
if (index == null)
{
Args?.Logger?.ILog($"No index given for stream '{stream}'");
return false;
}
if (Args.MathHelper.IsTrue(kvValue, index.Value))
Args?.Logger?.ILog($"Index Matches '{stream}[{index.Value}]' = {kvValue}");
else
{
Args?.Logger?.ILog($"Index does not match '{stream}[{index.Value}]' = {kvValue}");
return false;
}
break;
}
}
}

View File

@@ -0,0 +1,91 @@
using FileFlows.VideoNodes.FfmpegBuilderNodes.Models;
namespace FileFlows.VideoNodes.FfmpegBuilderNodes;
/// <summary>
/// Flow element used to remove matching tracks
/// </summary>
public class FfmpegBuilderTrackRemover: TrackSelectorFlowElement<FfmpegBuilderTrackRemover>
{
/// <inheritdoc />
public override string HelpUrl => "https://fileflows.com/docs/plugins/video-nodes/ffmpeg-builder/track-remover";
/// <inheritdoc />
public override string Icon => "fas fa-eraser";
/// <inheritdoc />
public override int Outputs => 2;
/// <summary>
/// Gets or sets the stream types to remove
/// </summary>
[Select(nameof(StreamTypeOptions), 1)]
public string StreamType { get; set; }
/// <summary>
/// The static stream type list
/// </summary>
private static List<ListOption> _StreamTypeOptions;
/// <summary>
/// Gets the available stream types
/// </summary>
public static List<ListOption> StreamTypeOptions
{
get
{
if (_StreamTypeOptions == null)
{
_StreamTypeOptions = new List<ListOption>
{
new () { Label = "Video", Value = "Video" },
new () { Label = "Audio", Value = "Audio" },
new () { Label = "Subtitle", Value = "Subtitle" }
};
}
return _StreamTypeOptions;
}
}
/// <inheritdoc />
public override int Execute(NodeParameters args)
{
if(string.IsNullOrEmpty(StreamType) || StreamType.Equals("audio", StringComparison.CurrentCultureIgnoreCase))
return RemoveTracks(Model.AudioStreams) ? 1 : 2;
if (StreamType.Equals("subtitle", StringComparison.CurrentCultureIgnoreCase))
return RemoveTracks(Model.SubtitleStreams) ? 1 : 2;
if (StreamType.Equals("video", StringComparison.CurrentCultureIgnoreCase))
return RemoveTracks(Model.VideoStreams) ? 1 : 2;
return 2;
}
/// <summary>
/// Iteerate the tracks/streams and remove any that match the conditions
/// </summary>
/// <param name="tracks">the track to iterate</param>
/// <typeparam name="T">The type of the track</typeparam>
/// <returns>true if any tracks were removed/deleted</returns>
private bool RemoveTracks<T>(List<T> tracks) where T: FfmpegStream
{
bool removing = false;
int index = -1;
foreach (var track in tracks)
{
if (track.Deleted)
continue;
index++;
if (CustomTrackSelection && StreamMatches(track, index) == false)
{
Args.Logger?.ILog("Stream does not match conditions: " + track);
continue;
}
Args.Logger?.ILog($"Deleting Stream: {track}");
track.Deleted = true;
removing = true;
}
return removing;
}
}

View File

@@ -297,7 +297,7 @@
}
},
"FfmpegBuilderAudioTrackRemover": {
"Label": "FFMPEG Builder: Track Remover",
"Label": "FFMPEG Builder: Track Remover (Obsolete)",
"Outputs": {
"1": "Tracks set to remove",
"2": "Tracks NOT set to removed"
@@ -320,6 +320,22 @@
"MatchType-Help": "The value to match against."
}
},
"FfmpegBuilderTrackRemover": {
"Label": "FFMPEG Builder: Track Remover",
"Outputs": {
"1": "Tracks set to remove",
"2": "Tracks NOT set to removed"
},
"Description": "Allows you to remove tracks based on specified conditions.",
"Fields": {
"StreamType": "Type",
"StreamType-Help": "The type of tracks to reorder in the FFMPEG Builder",
"CustomTrackSelection": "Remove",
"TrackSelectionOptions": "Matching",
"TrackSelectionOptionsKey": "Property",
"TrackSelectionOptionsValue": "Value"
}
},
"FfmpegBuilderAudioTrackReorder": {
"Label": "FFMPEG Builder: Track Reorder",
"Outputs": {