{"record":{"id":"0497b08e1251b127","repo":"nilaoda/N_m3u8DL-RE","slug":"bad-vtt","errorCode":null,"errorMessage":"Bad vtt!","messagePattern":"Bad vtt!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/N_m3u8DL-RE.Common/Entity/WebVttSub.cs","lineNumber":49,"sourceCode":"    /// 从字节数组解析WEBVTT\n    /// </summary>\n    /// <param name=\"textBytes\"></param>\n    /// <param name=\"encoding\"></param>\n    /// <returns></returns>\n    public static WebVttSub Parse(byte[] textBytes, Encoding encoding, long BaseTimestamp = 0L)\n    {\n        return Parse(encoding.GetString(textBytes), BaseTimestamp);\n    }\n\n    /// <summary>\n    /// 从字符串解析WEBVTT\n    /// </summary>\n    /// <param name=\"text\"></param>\n    /// <returns></returns>\n    public static WebVttSub Parse(string text, long BaseTimestamp = 0L)\n    {\n        if (!text.Trim().StartsWith(\"WEBVTT\"))\n            throw new Exception(\"Bad vtt!\");\n\n        text += Environment.NewLine;\n\n        var webSub = new WebVttSub();\n        var needPayload = false;\n        var timeLine = \"\";\n        var regex1 = TSMapRegex();\n\n        if (regex1.IsMatch(text))\n        {\n            var timestamp = TSValueRegex().Match(regex1.Match(text).Value).Groups[1].Value;\n            webSub.MpegtsTimestamp = Convert.ToInt64(timestamp);\n        }\n\n        var payloads = new List<string>();\n        foreach (var line in text.Split('\\n'))\n        {\n            if (line.Contains(\" --> \"))","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE.Common/Entity/WebVttSub.cs#L31-L67","documentation":"WebVttSub.Parse throws this when the input text does not begin with the mandatory 'WEBVTT' header. The WebVTT specification requires every valid subtitle file to start with 'WEBVTT' on its first line, so the parser rejects anything else up front instead of failing later with confusing cue-parsing errors.","triggerScenarios":"Calling WebVttSub.Parse(string) with a string whose Trim() does not start with \"WEBVTT\" — e.g. an SRT file, an HTML error page, empty content, or a byte-order-mark/mojibake-corrupted header.","commonSituations":"Downloading subtitle segments that are actually SRT or VTT sprites, a proxy/CDN returning an HTML error page instead of the .vtt file, saving the file with a UTF-8 BOM handled as text before 'WEBVTT', or passing an empty/default string.","solutions":["Verify the source actually serves WebVTT: check that the content begins with 'WEBVTT' before calling Parse.","If the content is SRT, convert it to WebVTT first (add the WEBVTT header and replace comma decimal separators in timestamps with dots).","Strip any UTF-8 BOM or leading whitespace/BOM bytes before parsing.","Wrap Parse in try/catch and surface a clear 'not a WebVTT file' message to the user."],"exampleFix":"// before\nvar sub = WebVttSub.Parse(downloadedText);\n\n// after\ndownloadedText = downloadedText.TrimStart('\\ufeff');\nif (!downloadedText.TrimStart().StartsWith(\"WEBVTT\"))\n    throw new Exception(\"Downloaded subtitle is not WebVTT (missing WEBVTT header)\");\nvar sub = WebVttSub.Parse(downloadedText);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(text) || !text.TrimStart('\\ufeff').TrimStart().StartsWith(\"WEBVTT\"))\n    throw new ArgumentException(\"Input is not a WebVTT file (missing WEBVTT header)\");","typeGuard":"static bool IsWebVtt(string text) => !string.IsNullOrWhiteSpace(text) && text.TrimStart('\\ufeff').TrimStart().StartsWith(\"WEBVTT\");","tryCatchPattern":"try { var sub = WebVttSub.Parse(text); }\ncatch (Exception ex) when (ex.Message == \"Bad vtt!\") { /* handle non-WebVTT input */ }","preventionTips":["Always check the first non-whitespace characters equal 'WEBVTT' before parsing.","Strip UTF-8 BOM from downloaded subtitle text.","Detect SRT content (timestamp pattern 00:00:00,000) and convert before parsing."],"tags":["webvtt","subtitle","parsing","validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"e113dee70c924ee08dae5460624909b04d84cb76","analyzedAt":"2026-09-13T02:43:21.078Z","contentChangedAt":"2026-09-13T02:43:21.078Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}