Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,26 +175,21 @@ Critical performance feature - DO NOT break compatibility:

### XML and JSON Parsing

AOT-safe parsers in `MediaInfoXmlParser.cs`:
AOT-safe parser in `MediaInfoXmlParser.cs`, avoiding `XmlSerializer` (not AOT-compatible):

- **MediaInfoFromXml()**: Parses specific MediaInfo XML elements into `MediaInfoToolXmlSchema.MediaInfo`
- Manually parses only known elements needed by PlexCleaner (id, format, language, etc.)
- Used by sidecar file system to parse XML output when JSON unavailable
- Avoids XmlSerializer (not AOT-compatible)
- **GenericXmlToJson()**: Converts any XML file to JSON format
- Preserves all elements and attributes (unlike MediaInfoFromXml's selective parsing)
- Used by the sidecar v4 to v5 upgrade, which converts the stored MediaInfo XML to JSON
- Preserves every element below the root with its attributes, except namespace and `xsi` attributes
- Does not write the root element's own attributes
- Handles attributes: prefix with `@` for elements with children, no prefix for leaf elements
- Detects arrays: elements appearing multiple times become JSON arrays
- Two-pass algorithm: collect children to detect arrays, then write JSON
- Uses `XmlReader` and `Utf8JsonWriter` for streaming efficiency
- Reads with `XmlReader` into an in-memory `ElementData` tree, then writes it with `Utf8JsonWriter`
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
- Special handling for MediaInfo's mixed attribute/text content format (creatingLibrary)
- **MediaInfoXmlToJson()**: Converts parsed MediaInfo XML to MediaInfo JSON schema
- Bridges between XML and JSON schema types
- Maps only known MediaInfo track properties

Parser design patterns:

- Forward-only `XmlReader` with depth tracking for streaming
- Forward-only `XmlReader` with depth tracking
- Recursive `ElementData` tree for generic XML-to-JSON conversion
- Namespace filtering (skip `xmlns`, `xsi` attributes)
- Special handling for MediaInfo's mixed attribute/text content format
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Utility to optimize media files for Direct Play in Plex, Emby, Jellyfin, etc.

## Release History

- Version 3.24:
- Removed code that nothing called, as the first step toward broader test coverage, with no change in processing behavior.
- The MediaInfo XML parser that built typed XML schema objects, and those schema types. The sidecar v4 to v5 upgrade keeps its generic XML to JSON conversion.
- An unused tool execution path that streamed standard output, and unused tool command-line builder options.
- An unused media props self-test method.
- Version 3.23:
- Added a minimum version for every media tool, and PlexCleaner exits with an error at startup when an installed tool is older.
- The minimums are FFmpeg 8.0, HandBrake 1.10.0, MediaInfo 25.07, MKVToolNix 94.0, and 7-Zip 25.01.
Expand Down
4 changes: 0 additions & 4 deletions PlexCleaner/FfProbeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class GlobalOptions(ArgumentsBuilder argumentsBuilder)

public GlobalOptions LogLevelError() => LogLevel("error");

public GlobalOptions LogLevelQuiet() => LogLevel("quiet");

public GlobalOptions HideBanner() => Add("-hide_banner");

public GlobalOptions AnalyzeDuration() => Add("-analyzeduration");
Expand Down Expand Up @@ -56,8 +54,6 @@ public class FfProbeOptions(ArgumentsBuilder argumentsBuilder)

public FfProbeOptions ShowPackets() => Add("-show_packets");

public FfProbeOptions ShowFrames() => Add("-show_frames");

public FfProbeOptions ShowFormat() => Add("-show_format");

public FfProbeOptions AnalyzeFrames() => Add("-analyze_frames");
Expand Down
2 changes: 0 additions & 2 deletions PlexCleaner/MediaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class MediaInfoOptions(ArgumentsBuilder argumentsBuilder)
{
public MediaInfoOptions OutputFormat(string option) => Add($"--Output={option}");

public MediaInfoOptions OutputFormatXml() => OutputFormat("XML");

public MediaInfoOptions OutputFormatJson() => OutputFormat("JSON");

public MediaInfoOptions InputFile(string option) => Add($"\"{option}\"");
Expand Down
94 changes: 0 additions & 94 deletions PlexCleaner/MediaInfoToolXmlSchema.cs

This file was deleted.

171 changes: 0 additions & 171 deletions PlexCleaner/MediaInfoXmlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,139 +10,6 @@ namespace PlexCleaner;

public static class MediaInfoXmlParser
{
// Parses known MediaInfo XML elements and converts to MediaInfo object
public static MediaInfoToolXmlSchema.MediaInfo MediaInfoFromXml(string xml)
{
MediaInfoToolXmlSchema.MediaInfo mediaInfo = new();
using StringReader stringReader = new(xml);
XmlReaderSettings settings = new()
{
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null,
IgnoreWhitespace = true,
IgnoreComments = true,
};

using XmlReader reader = XmlReader.Create(stringReader, settings);
while (reader.Read())
{
if (
reader.NodeType == XmlNodeType.Element
&& reader.LocalName.Equals("mediainfo", StringComparison.OrdinalIgnoreCase)
)
{
ParseMediaInfo(reader, mediaInfo);
break;
}
}

return mediaInfo;
}

private static void ParseMediaInfo(XmlReader reader, MediaInfoToolXmlSchema.MediaInfo mediaInfo)
{
int mediaInfoDepth = reader.Depth;
while (reader.Read() && reader.Depth > mediaInfoDepth)
{
if (
reader.NodeType == XmlNodeType.Element
&& reader.LocalName.Equals("media", StringComparison.OrdinalIgnoreCase)
)
{
ParseMedia(reader, mediaInfo.Media);
}
}
}

private static void ParseMedia(XmlReader reader, MediaInfoToolXmlSchema.Media media)
{
int mediaDepth = reader.Depth;
while (reader.Read() && reader.Depth > mediaDepth)
{
if (
reader.NodeType == XmlNodeType.Element
&& reader.LocalName.Equals("track", StringComparison.OrdinalIgnoreCase)
)
{
MediaInfoToolXmlSchema.Track track = ParseTrack(reader);
media.Tracks.Add(track);
}
}
}

private static MediaInfoToolXmlSchema.Track ParseTrack(XmlReader reader)
{
MediaInfoToolXmlSchema.Track track = new();
if (reader.HasAttributes)
{
track.Type = reader.GetAttribute("type") ?? string.Empty;
}

int trackDepth = reader.Depth;
while (reader.Read() && reader.Depth > trackDepth)
{
if (reader.NodeType == XmlNodeType.Element)
{
string elementName = reader.LocalName;
if (reader.Read() && reader.NodeType == XmlNodeType.Text)
{
switch (elementName.ToLowerInvariant())
{
case "id":
track.Id = reader.Value;
break;
case "uniqueid":
track.UniqueId = reader.Value;
break;
case "duration":
track.Duration = reader.Value;
break;
case "format":
track.Format = reader.Value;
break;
case "format_profile":
track.FormatProfile = reader.Value;
break;
case "format_level":
track.FormatLevel = reader.Value;
break;
case "hdr_format":
track.HdrFormat = reader.Value;
break;
case "codecid":
track.CodecId = reader.Value;
break;
case "language":
track.Language = reader.Value;
break;
case "default":
track.Default = reader.Value;
break;
case "forced":
track.Forced = reader.Value;
break;
case "muxingmode":
track.MuxingMode = reader.Value;
break;
case "streamorder":
track.StreamOrder = reader.Value;
break;
case "scantype":
track.ScanType = reader.Value;
break;
case "title":
track.Title = reader.Value;
break;
default:
break;
}
}
}
}

return track;
}

// Parses all MediaInfo XML elements and converts to JSON
public static string GenericXmlToJson(string xml)
{
Expand Down Expand Up @@ -394,42 +261,4 @@ private class ElementData
public Dictionary<string, List<ElementData>> Children { get; } = [];
public string TextContent { get; set; } = string.Empty;
}

// Parses known MediaInfo XML elements and converts to JSON
public static string MediaInfoXmlToJson(string mediaInfoXml)
{
// Serialize from XML
MediaInfoToolXmlSchema.MediaInfo xmlMediaInfo = MediaInfoToolXmlSchema.MediaInfo.FromXml(
mediaInfoXml
);

// Copy to JSON schema
MediaInfoToolJsonSchema.MediaInfo jsonMediaInfo = new();
foreach (MediaInfoToolXmlSchema.Track xmlTrack in xmlMediaInfo.Media.Tracks)
{
MediaInfoToolJsonSchema.Track jsonTrack = new()
{
Type = xmlTrack.Type,
Id = xmlTrack.Id,
UniqueId = xmlTrack.UniqueId,
Duration = xmlTrack.Duration,
Format = xmlTrack.Format,
FormatProfile = xmlTrack.FormatProfile,
FormatLevel = xmlTrack.FormatLevel,
HdrFormat = xmlTrack.HdrFormat,
CodecId = xmlTrack.CodecId,
Language = xmlTrack.Language,
Default = xmlTrack.Default,
Forced = xmlTrack.Forced,
MuxingMode = xmlTrack.MuxingMode,
StreamOrder = xmlTrack.StreamOrder,
ScanType = xmlTrack.ScanType,
Title = xmlTrack.Title,
};
jsonMediaInfo.Media.Tracks.Add(jsonTrack);
}

// Serialize to JSON
return JsonSerializer.Serialize(jsonMediaInfo, MediaInfoToolJsonContext.Default.MediaInfo);
}
}
Loading
Loading