SubtitleEdit/subtitleedit · error · InvalidOperationException

Failed to parse MXF file '{filePath}': {ex.Message}

Error message

Failed to parse MXF file '{filePath}': {ex.Message}

What it means

Thrown when the MxfParser constructor raises a low-level exception (e.g. IndexOutOfRangeException, EndOfStreamException) while walking the MXF KLV structure. The loader catches it (filtering out its own InvalidOperationException) and re-throws with MXF context so the user sees a meaningful message instead of a bare 'Index was outside the bounds of the array'. The original exception is preserved as InnerException.

Source

Thrown at src/seconv/Core/ContainerSubtitleLoader.cs:159

    /// essences (PNG payloads in MxfParser.GetImages) are *not* surfaced here —
    /// MxfParser collects the bitmaps but doesn't carry per-essence PTS, so there's no
    /// timing context for image output. Flagged as a warning instead.
    /// </summary>
    private static List<LoadedTrack>? LoadMxf(string filePath, ConversionOptions options)
    {
        // MxfParser walks the KLV structure inside its constructor, so any malformed
        // packet (zero-length essence, truncated BER length, etc.) surfaces here as a
        // low-level exception like IndexOutOfRangeException. Wrap it so the user sees
        // an MXF-contextual error instead of a stack-traceless "Index was outside the
        // bounds of the array".
        MxfParser parser;
        try
        {
            parser = new MxfParser(filePath);
        }
        catch (Exception ex) when (ex is not InvalidOperationException)
        {
            throw new InvalidOperationException(
                $"Failed to parse MXF file '{filePath}': {ex.Message}", ex);
        }

        if (!parser.IsValid)
        {
            // Not a real MXF (no Header Partition Pack signature). Fall through to the
            // text loader — the file might just have a misleading extension.
            return null;
        }

        var subtitleTexts = parser.GetSubtitles();
        var images = parser.GetImages();

        if (subtitleTexts.Count == 0)
        {
            if (images.Count > 0)
            {
                throw new InvalidOperationException(

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Verify the file is a complete, valid MXF (ffprobe / a commercial MXF inspector).
  2. Re-acquire or re-wrap the source if it is truncated or corrupt.
  3. If the extension is misleading, rename it to its true format so the loader routes it correctly (MXF only triggers for .mxf).
  4. Inspect InnerException in the thrown InvalidOperationException for the underlying parser failure.

Example fix

// before
var tracks = ContainerSubtitleLoader.TryLoadTracks(mxfPath, options);

// after
List<LoadedTrack>? tracks;
try { tracks = ContainerSubtitleLoader.TryLoadTracks(mxfPath, options); }
catch (InvalidOperationException ex)
{
    Console.Error.WriteLine($"{ex.Message} (root: {ex.InnerException?.Message})");
    return;
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Best-effort pre-check: confirm the file has an MXF header partition pack.
if (new FileInfo(filePath).Length < 16) return null;
using var peek = File.OpenRead(filePath);
var head = new byte[14]; peek.Read(head, 0, 14);
if (!Encoding.ASCII.GetString(head).Contains("\x06\x0e\\x2b\x34")) return null;  // MXF OID prefix

Try / catch

try { tracks = ContainerSubtitleLoader.TryLoadTracks(mxfPath, options); }
catch (InvalidOperationException ex) { Log.Error($"{ex.Message} (root: {ex.InnerException?.Message})"); return; }

Prevention

When it happens

Trigger: Calling the MXF load path on a truncated/corrupt .mxf where a KLV packet has a zero-length essence, a malformed BER length, or the file is cut mid-packet. The parser throws inside its constructor (ContainerSubtitleLoader.cs:162) and the 'when' filter lets only non-InvalidOperationException exceptions through.

Common situations: A partially downloaded/incomplete MXF; an MXF from a faulty recorder with corrupt KLV packets; a file that is not really MXF but was given a .mxf extension (though that usually falls through via the IsValid check, a structurally-similar-but-broken file can still throw).

Understand the failure class

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/0c41f4fbc8df6ca4. Report an issue: GitHub.