{"record":{"id":"0c41f4fbc8df6ca4","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-parse-mxf-file-filepath-ex-message","errorCode":null,"errorMessage":"Failed to parse MXF file '{filePath}': {ex.Message}","messagePattern":"Failed to parse MXF file '(.+?)': (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/seconv/Core/ContainerSubtitleLoader.cs","lineNumber":159,"sourceCode":"    /// essences (PNG payloads in MxfParser.GetImages) are *not* surfaced here —\n    /// MxfParser collects the bitmaps but doesn't carry per-essence PTS, so there's no\n    /// timing context for image output. Flagged as a warning instead.\n    /// </summary>\n    private static List<LoadedTrack>? LoadMxf(string filePath, ConversionOptions options)\n    {\n        // MxfParser walks the KLV structure inside its constructor, so any malformed\n        // packet (zero-length essence, truncated BER length, etc.) surfaces here as a\n        // low-level exception like IndexOutOfRangeException. Wrap it so the user sees\n        // an MXF-contextual error instead of a stack-traceless \"Index was outside the\n        // bounds of the array\".\n        MxfParser parser;\n        try\n        {\n            parser = new MxfParser(filePath);\n        }\n        catch (Exception ex) when (ex is not InvalidOperationException)\n        {\n            throw new InvalidOperationException(\n                $\"Failed to parse MXF file '{filePath}': {ex.Message}\", ex);\n        }\n\n        if (!parser.IsValid)\n        {\n            // Not a real MXF (no Header Partition Pack signature). Fall through to the\n            // text loader — the file might just have a misleading extension.\n            return null;\n        }\n\n        var subtitleTexts = parser.GetSubtitles();\n        var images = parser.GetImages();\n\n        if (subtitleTexts.Count == 0)\n        {\n            if (images.Count > 0)\n            {\n                throw new InvalidOperationException(","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/seconv/Core/ContainerSubtitleLoader.cs#L141-L177","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Verify the file is a complete, valid MXF (ffprobe / a commercial MXF inspector).","Re-acquire or re-wrap the source if it is truncated or corrupt.","If the extension is misleading, rename it to its true format so the loader routes it correctly (MXF only triggers for .mxf).","Inspect InnerException in the thrown InvalidOperationException for the underlying parser failure."],"exampleFix":"// before\nvar tracks = ContainerSubtitleLoader.TryLoadTracks(mxfPath, options);\n\n// after\nList<LoadedTrack>? tracks;\ntry { tracks = ContainerSubtitleLoader.TryLoadTracks(mxfPath, options); }\ncatch (InvalidOperationException ex)\n{\n    Console.Error.WriteLine($\"{ex.Message} (root: {ex.InnerException?.Message})\");\n    return;\n}","handlingStrategy":"try-catch","validationCode":"// Best-effort pre-check: confirm the file has an MXF header partition pack.\nif (new FileInfo(filePath).Length < 16) return null;\nusing var peek = File.OpenRead(filePath);\nvar head = new byte[14]; peek.Read(head, 0, 14);\nif (!Encoding.ASCII.GetString(head).Contains(\"\\x06\\x0e\\\\x2b\\x34\")) return null;  // MXF OID prefix","typeGuard":null,"tryCatchPattern":"try { tracks = ContainerSubtitleLoader.TryLoadTracks(mxfPath, options); }\ncatch (InvalidOperationException ex) { Log.Error($\"{ex.Message} (root: {ex.InnerException?.Message})\"); return; }","preventionTips":["Verify MXF integrity (ffprobe) before batch-converting.","Catch InvalidOperationException and inspect InnerException for the underlying KLV parse failure.","Keep the original source so a corrupt MXF can be re-acquired."],"tags":["mxf","container","corrupt-file","subtitle"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}