{"record":{"id":"aa016f48184bb9de","repo":"nilaoda/N_m3u8DL-RE","slug":"download-init-file-failed","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/SimpleDownloadManager.cs","lineNumber":173,"sourceCode":"            Logger.WarnMarkUp($\"[darkorange3_1]{ResString.autoBinaryMerge4}[/]\");\n        }\n\n        // 下载init\n        if (streamSpec.Playlist?.MediaInit != null)\n        {\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                mp4Info = MP4DecryptUtil.GetMP4Info(result.ActualFilePath);\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                else\n                {\n                    currentKID = mp4Info.KID;\n                }","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE/DownloadManager/SimpleDownloadManager.cs#L155-L191","documentation":"During download of an HLS/DASH stream, SimpleDownloadManager.DownloadStreamAsync fetches the MP4 initialization segment (MediaInit) before any media segments. If the init download completes without Success it throws, aborting the whole stream download since segments cannot be decoded without init data.","triggerScenarios":"Downloader.DownloadSegmentAsync returns a DownloadResult with Success == false for streamSpec.Playlist.MediaInit — HTTP failure, 403/404 on the init URL, network timeout, or a null/failed result stored in FileDic.","commonSituations":"Init segment URL expired (signed URLs), server returning 403 for the init request, proxy blocking the request, or playlist referencing an init file that was removed from the CDN.","solutions":["Re-run with --no-proxy or check the init URL manually with curl to confirm reachability","Retry the download — transient network failures are the usual cause","Check authentication headers/cookies required by the init segment URL","Verify the playlist's EXT-X-MAP / initialization attribute URL is still valid"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// preflight the init URL\nusing var hc = new HttpClient();\nusing var resp = await hc.SendAsync(new HttpRequestMessage(HttpMethod.Head, playlist.MediaInit.Url));\nif (!resp.IsSuccessStatusCode) throw new Exception($\"init segment unreachable: {(int)resp.StatusCode}\");","typeGuard":"static bool IsSuccess(DownloadResult r) => r is { Success: true };","tryCatchPattern":"try { await DownloadStreamAsync(...); }\ncatch (Exception ex) when (ex.Message == \"Download init file failed!\")\n{\n    logger.Warn(\"init segment download failed, retrying...\");\n    await RetryAsync(DownloadStreamAsync, 3);\n}","preventionTips":["Use fresh auth cookies/tokens; signed init URLs expire quickly","Preflight init URLs with a HEAD request before a long download","Ensure stable network/proxy for the whole run"],"tags":["csharp","hls","dash","download","init-segment"],"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"}