nilaoda/N_m3u8DL-RE · error · Exception

Download first segment failed!

Error message

Download first segment failed!

What it means

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.

Solutions

  1. Retry the recording with a more stable connection
  2. Check disk space and permissions in the temp/output directory
  3. Reduce concurrency (--save-name threads) if the CDN rate-limits parallel requests
  4. Confirm segment URLs resolve (curl) — live CDNs rotate URLs quickly
Defensive patterns

Strategy: retry

Validate before calling

if (new DriveInfo(tmpDir).AvailableFreeSpace < 1_000_000_000) throw new IOException("insufficient space for recording");

Type guard

static bool IsSuccess(DownloadResult r) => r is { Success: true };

Try / catch

try { await RecordStreamAsync(...); }
catch (Exception ex) when (ex.Message == "Download first segment failed!")
{
    logger.Warn("first live segment failed; restarting recording session");
    await RestartRecordingAsync();
}

Prevention

When it happens

Trigger: Downloader.DownloadSegmentAsync for the first seg returns Success == false during a live record — HTTP failure, timeout, or write failure to tmpDir/filename.tmp.

Common situations: Live stream segment URLs expiring between playlist fetch and download; unstable network during long recordings; disk space exhaustion on the tmp volume.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of nilaoda/N_m3u8DL-RE@e113dee70c (2026-09-13). Data as JSON: /api/errors/a25b5b32029017b1. Report an issue: GitHub.

Appendix: source

Thrown at src/N_m3u8DL-RE/DownloadManager/SimpleLiveRecordManager2.cs:284

                var allName = segments.Select(s => OtherUtil.GetFileNameFromInput(s.Url, false));
                var allSamePath = allName.Count() > 1 && allName.Distinct().Count() == 1;
                SamePathDic[task.Id] = allSamePath;
            }

            // 下载第一个分片
            if (!readInfo || StreamExtractor.ExtractorType == ExtractorType.MSS)
            {
                var seg = segments.First();
                segments = segments.Skip(1);
                // 获取文件名
                var filename = GetSegmentName(seg, allHasDatetime, SamePathDic[task.Id]);
                var index = seg.Index;
                var path = Path.Combine(tmpDir, filename + $".{streamSpec.Extension ?? "clip"}.tmp");
                var result = await Downloader.DownloadSegmentAsync(seg, path, speedContainer, headers);
                FileDic[seg] = result;
                if (result is not { Success: true })
                {
                    throw new Exception("Download first segment failed!");
                }
                task.Increment(1);
                if (result is { Success: true })
                {
                    // 修复MSS init
                    if (StreamExtractor.ExtractorType == ExtractorType.MSS)
                    {
                        var processor = new MSSMoovProcessor(streamSpec);
                        var header = processor.GenHeader(File.ReadAllBytes(result.ActualFilePath));
                        await File.WriteAllBytesAsync(FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath, header);
                        if (seg.IsEncrypted && DownloaderConfig.MyOptions.MP4RealTimeDecryption && !string.IsNullOrEmpty(currentKID))
                        {
                            // 需要重新解密init
                            var enc = FileDic[streamSpec.Playlist!.MediaInit!]!.ActualFilePath;
                            var dec = Path.Combine(Path.GetDirectoryName(enc)!, Path.GetFileNameWithoutExtension(enc) + "_dec" + Path.GetExtension(enc));
                            var dResult = await MP4DecryptUtil.DecryptAsync(decryptEngine, decryptionBinaryPath, DownloaderConfig.MyOptions.Keys, enc, dec, currentKID);
                            if (dResult)
                            {

View on GitHub (pinned to e113dee70c)