{"record":{"id":"fdf812117ad0bcc4","repo":"nilaoda/N_m3u8DL-RE","slug":"download-init-file-failed-simpleliverecordmanager2","errorCode":null,"errorMessage":"Download init file failed!","messagePattern":"Download init file failed!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs","lineNumber":218,"sourceCode":"            Logger.DebugMarkUp(string.Join(\",\", segments.Select(sss => GetSegmentName(sss, false, false))));\n\n            // 下载init\n            if (!initDownloaded && streamSpec.Playlist?.MediaInit != null) \n            {\n                task.MaxValue += 1;\n                // 对于fMP4，自动开启二进制合并\n                if (!DownloaderConfig.MyOptions.BinaryMerge && streamSpec.MediaType != MediaType.SUBTITLES)\n                {\n                    DownloaderConfig.MyOptions.BinaryMerge = true;\n                    Logger.WarnMarkUp($\"[darkorange3_1]{ResString.autoBinaryMerge}[/]\");\n                }\n\n                var path = Path.Combine(tmpDir, \"_init.mp4.tmp\");\n                var result = await Downloader.DownloadSegmentAsync(streamSpec.Playlist.MediaInit, path, speedContainer, headers);\n                FileDic[streamSpec.Playlist.MediaInit] = result;\n                if (result is not { Success: true })\n                {\n                    throw new Exception(\"Download init file failed!\");\n                }\n                mp4InitFile = result.ActualFilePath;\n                task.Increment(1);\n\n                // 读取mp4信息\n                if (result is { Success: true })\n                {\n                    currentKID = MP4DecryptUtil.GetMP4Info(result.ActualFilePath).KID;\n                    // MPD的cenc:default_KID优先\n                    if (streamSpec.Playlist?.MediaInit?.EncryptInfo.KID != null)\n                    {\n                        currentKID = streamSpec.Playlist.MediaInit.EncryptInfo.KID;\n                        Logger.WarnMarkUp($\"[grey]KID (from MPD): {currentKID}[/]\");\n                    }\n                    // 从文件读取KEY\n                    await SearchKeyAsync(currentKID);\n                    // 实时解密\n                    if ((streamSpec.Playlist.MediaInit.IsEncrypted || !string.IsNullOrEmpty(currentKID)) && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID) && StreamExtractor.ExtractorType != ExtractorType.MSS)","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs#L200-L236","documentation":"SimpleLiveRecordManager2.RecordStreamAsync (live recording mode) downloads the MP4 init segment before recording; a failed init download aborts the recording. Same contract as the VOD manager: result.Success must be true.","triggerScenarios":"Downloader.DownloadSegmentAsync for streamSpec.Playlist.MediaInit returns Success == false while recording a live stream — server refused, timeout, or the live playlist's init URL rotated/expired.","commonSituations":"Long live recordings where the signed init URL expired; CDN issues; proxies interrupting the connection at recording start.","solutions":["Restart the recording — init downloads usually fail transiently","Verify the init URL from the current live playlist is reachable with curl","Check auth headers/cookies for the live CDN","Avoid heavy proxy usage or configure a reliable proxy via --proxy"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"using var resp = await new HttpClient().SendAsync(new HttpRequestMessage(HttpMethod.Head, playlist.MediaInit.Url));\nif (!resp.IsSuccessStatusCode) throw new Exception(\"live init segment unreachable\");","typeGuard":"static bool IsSuccess(DownloadResult r) => r is { Success: true };","tryCatchPattern":"try { await RecordStreamAsync(...); }\ncatch (Exception ex) when (ex.Message == \"Download init file failed!\")\n{\n    logger.Warn(\"live init download failed; restarting recording\");\n    await RestartRecordingAsync();\n}","preventionTips":["Start recordings with valid current cookies; live CDNs rotate authorization","Use a reliable direct connection or well-configured proxy","Monitor long recordings and auto-restart on init failures"],"tags":["csharp","live","recording","init-segment","download"],"backgroundTag":"http-request-failed","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"}