{"record":{"id":"a25b5b32029017b1","repo":"nilaoda/N_m3u8DL-RE","slug":"download-first-segment-failed-simpleliverecordmanager2","errorCode":null,"errorMessage":"Download first segment failed!","messagePattern":"Download first segment failed!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs","lineNumber":284,"sourceCode":"                var allName = segments.Select(s => OtherUtil.GetFileNameFromInput(s.Url, false));\n                var allSamePath = allName.Count() > 1 && allName.Distinct().Count() == 1;\n                SamePathDic[task.Id] = allSamePath;\n            }\n\n            // 下载第一个分片\n            if (!readInfo || StreamExtractor.ExtractorType == ExtractorType.MSS)\n            {\n                var seg = segments.First();\n                segments = segments.Skip(1);\n                // 获取文件名\n                var filename = GetSegmentName(seg, allHasDatetime, SamePathDic[task.Id]);\n                var index = seg.Index;\n                var path = Path.Combine(tmpDir, filename + $\".{streamSpec.Extension ?? \"clip\"}.tmp\");\n                var result = await Downloader.DownloadSegmentAsync(seg, path, speedContainer, headers);\n                FileDic[seg] = result;\n                if (result is not { Success: true })\n                {\n                    throw new Exception(\"Download first segment failed!\");\n                }\n                task.Increment(1);\n                if (result is { Success: true })\n                {\n                    // 修复MSS init\n                    if (StreamExtractor.ExtractorType == ExtractorType.MSS)\n                    {\n                        var processor = new MSSMoovProcessor(streamSpec);\n                        var header = processor.GenHeader(File.ReadAllBytes(result.ActualFilePath));\n                        await File.WriteAllBytesAsync(FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath, header);\n                        if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))\n                        {\n                            // 需要重新解密init\n                            var enc = FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath;\n                            var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + \"_dec\" + Path.GetExtension(enc));\n                            var dResult = await MP4DecryptUtil.DecryptAsync(decryptEngine, decryptionBinaryPath, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID);\n                            if (dResult)\n                            {","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/nilaoda/N_m3u8DL-RE/blob/e113dee70c924ee08dae5460624909b04d84cb76/src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs#L266-L302","documentation":"SimpleLiveRecordManager2.RecordStreamAsync throws when the first recorded segment fails to download (result.Success not true). Recording cannot proceed without the first segment of each recording session.","triggerScenarios":"Downloader.DownloadSegmentAsync for the first seg returns Success == false during a live record — HTTP failure, timeout, or write failure to tmpDir/filename.tmp.","commonSituations":"Live stream segment URLs expiring between playlist fetch and download; unstable network during long recordings; disk space exhaustion on the tmp volume.","solutions":["Retry the recording with a more stable connection","Check disk space and permissions in the temp/output directory","Reduce concurrency (--save-name threads) if the CDN rate-limits parallel requests","Confirm segment URLs resolve (curl) — live CDNs rotate URLs quickly"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if (new DriveInfo(tmpDir).AvailableFreeSpace < 1_000_000_000) throw new IOException(\"insufficient space for recording\");","typeGuard":"static bool IsSuccess(DownloadResult r) => r is { Success: true };","tryCatchPattern":"try { await RecordStreamAsync(...); }\ncatch (Exception ex) when (ex.Message == \"Download first segment failed!\")\n{\n    logger.Warn(\"first live segment failed; restarting recording session\");\n    await RestartRecordingAsync();\n}","preventionTips":["Ensure ample free disk space for the full recording duration","Lower concurrency if the CDN throttles or bans parallel requests","Prefer wired/stable connections for multi-hour live recordings"],"tags":["csharp","live","recording","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"}