# nilaoda/n_m3u8dl-re architecture

> Architecture diagram of the GitHub repository nilaoda/n_m3u8dl-re, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nilaoda/n_m3u8dl-re
- Repository: https://github.com/nilaoda/n_m3u8dl-re

## Overview

1. **Repository purpose**
   - N_m3u8DL-RE is a cross-platform .NET CLI for downloading segmented media streams: DASH, HLS, MSS.
   - It supports both VOD and live recording, plus optional decrypt, merge, subtitle handling, and muxing.

2. **Solution layout**
   - The solution is split into three main codebases:
     - `N_m3u8DL-RE` = CLI application and runtime orchestration.
     - `N_m3u8DL-RE.Parser` = manifest parsing and stream extraction.
     - `N_m3u8DL-RE.Common` = shared data models, enums, logging, HTTP helpers, and utilities.
   - `N_m3u8DL-RE.Tests` provides parser/util tests and sample manifests.

3. **Top-level execution flow**
   - `Program.cs` starts the CLI, parses options, and selects the appropriate processing path.
   - The input can be a URL or local file, then the app resolves media metadata, filters tracks, downloads segments, and optionally merges outputs.
   - The runtime is built around command-line orchestration rather than a server or long-lived service.

4. **Manifest parsing boundary**
   - `N_m3u8DL-RE.Parser` owns protocol-specific extraction:
     - `HLSExtractor`
     - `DASHExtractor2`
     - `MSSExtractor`
     - `LiveTSExtractor`
   - `StreamExtractor` and `IExtractor` define the extraction boundary between a generic input and protocol-specific stream lists.
   - The parser produces normalized track/segment descriptions used by the downloader layer.

5. **Shared domain model**
   - `N_m3u8DL-RE.Common/Entity` contains the core media model: playlists, segments, parts, stream specs, encryption info, subtitles, and Microsoft Smooth Streaming data.
   - Enums such as `MediaType`, `EncryptMethod`, `ExtractorType`, and `RoleType` describe protocol and track metadata.
   - This shared model is the main contract between parser, downloader, and merge/decrypt logic.

6. **Downloader and download managers**
   - The app layer handles actual acquisition of media segments through `Downloader/IDownloader` and `SimpleDownloader`.
   - `DownloadManager` contains different orchestration modes:
     - standard segmented download
     - live recording
     - live real-time merge / pipe-mux behavior
   - This layer coordinates concurrency, retries, speed limiting, and segment lifecycle.

7. **Decryption and key handling**
   - Decryption is supported for both HLS and MP4-based protected streams.
   - `Crypto` contains internal AES/ChaCha20 helpers, while the app can also invoke external decryption engines like `mp4decrypt`, `shaka-packager`, or `ffmpeg`.
   - HLS key processing is separated via `Processor/HLS/DefaultHLSKeyProcessor.cs`, keeping key acquisition and segment download distinct.

8. **Post-processing and muxing**
   - Merge and mux are handled as a separate stage after download, or incrementally for live recording.
   - `Util/MergeUtil`, `SubtitleUtil`, `MP4DecryptUtil`, and `MediainfoUtil` support container assembly, subtitle conversion/fixing, and media inspection.
   - External tools are important architectural dependencies here: `ffmpeg`, `mkvmerge`, and decryption binaries are invoked when configured.

9. **URL and content processors**
   - `Processor` contains protocol/site-specific adaptation logic, such as custom URL processors and content processors.
   - Examples include `DefaultUrlProcessor`, DASH/HLS content processors, and demo/specialized processors in the app project.
   - This is the extensibility boundary for site-specific URL rewriting, playlist normalization, and request shaping.

10. **Configuration and command-line model**
   - CLI behavior is controlled by strongly typed option objects and config classes such as `DownloaderConfig`, `ParserConfig`, and command-line parsers.
   - The app supports a large number of operational modes: track selection, output naming, live timing, proxy, headers, custom range, merge format, and decryption settings.
   - The architecture is heavily parameter-driven, with most behavior selected at startup rather than dynamically at runtime.

11. **HTTP, retry, and utility infrastructure**
   - `Common/Util` provides cross-cutting infrastructure:
     - `HTTPUtil` for network access
     - `RetryUtil` for transient failure handling
     - `GlobalUtil`, `HexUtil`, and binary content checks
   - Logging is centralized in `Common/Log`, with console formatting and log levels shared by the whole solution.

12. **Output and file organization**
   - Downloaded media is represented by `OutputFile`, `DownloadResult`, and related entities in the app layer.
   - The save naming and segmentation strategy is influenced by metadata such as language, codecs, resolution, bandwidth, and track role.
   - Temporary files, output files, and merged outputs are distinct artifacts in the workflow.

13. **Live recording path**
   - Live recording is a first-class path, not just a special case of VOD.
   - The live pipeline can refresh manifests, fetch segments continuously, optionally merge in real time, and optionally pipe mux to TS with ffmpeg.
   - This creates a different timing/dataflow boundary than VOD: repeated manifest polling and rolling segment accumulation.

14. **Testing boundary**
   - Tests focus on parser correctness and utility behavior, especially DASH extraction and hex helpers.
   - `N_m3u8DL-RE.Tests/Resources` includes sample manifests that act as fixtures for extraction logic.
   - The tests validate the repository’s most critical boundary: translating real manifests into normalized stream/segment structures.

15. **Diagramming takeaway**
   - A good architecture diagram should show:
     - CLI entrypoint at the top
     - parser/extractor subsystem as the protocol boundary
     - downloader/manager as the execution core
     - decrypt/mux/merge as post-processing stages
     - common models/utilities as the shared foundation
     - external tools/binaries and HTTP endpoints as outside dependencies

Main entry points: [`src/N_m3u8DL-RE.Parser/StreamExtractor.cs`](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/StreamExtractor.cs) (Stream extractor), [`src/N_m3u8DL-RE/DownloadManager`](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/DownloadManager) (Download mgr), [`src/N_m3u8DL-RE.Common/Entity`](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Entity) (Media model), [`src/N_m3u8DL-RE/Util`](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Util) (Post-process)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["N_m3u8DL-RE"]
  node_program(("Program<br/>cli entrypoint<br/>[Program.cs]"))
  node_commandline["Command line<br/>arg parsing"]
  node_downloader["Downloader<br/>segment fetch"]
  node_downloadmgr["Download mgr<br/>orchestration"]
  node_decrypt["Decrypt<br/>crypto stage"]
  node_postprocess["Post-process<br/>merge/mux"]
  node_appcfg["App config<br/>runtime config"]
  node_urlproc["URL processors<br/>site adapters"]
end

subgraph group_parser["N_m3u8DL-RE.Parser"]
  node_parserentry(("Stream extractor<br/>parse boundary<br/>[StreamExtractor.cs]"))
  node_hls["HLS extractor<br/>protocol parser<br/>[HLSExtractor.cs]"]
  node_dash["DASH extractor<br/>protocol parser<br/>[DASHExtractor2.cs]"]
  node_mss["MSS extractor<br/>protocol parser<br/>[MSSExtractor.cs]"]
  node_livets["Live TS extractor<br/>live parser<br/>[LiveTSExtractor.cs]"]
  node_parserproc["Parser processors<br/>content adaptation<br/>[Processor]"]
  node_parserutil["Parser util<br/>support code<br/>[Util]"]
end

subgraph group_common["N_m3u8DL-RE.Common"]
  node_sharedmodel[("Media model<br/>domain contract<br/>[Entity]")]
  node_enums["Enums<br/>metadata<br/>[Enum]"]
  node_commonutil["Common util<br/>infra helpers<br/>[Util]"]
  node_logging["Logging<br/>console log<br/>[Log]"]
end

subgraph group_tests["N_m3u8DL-RE.Tests"]
  node_testsuite(("Tests<br/>verification<br/>[N_m3u8DL-RE.Tests]"))
  node_fixtures["Fixtures<br/>sample manifests<br/>[Resources]"]
end

subgraph group_external["External deps"]
  node_network(("HTTP endpoints<br/>external input"))
  node_tools(("External tools<br/>helper binaries"))
end

node_program -->|"parse"| node_commandline
node_commandline -->|"configure"| node_appcfg
node_program -->|"resolve input"| node_parserentry
node_parserentry -->|"HLS"| node_hls
node_parserentry -->|"DASH"| node_dash
node_parserentry -->|"MSS"| node_mss
node_parserentry -->|"live TS"| node_livets
node_hls -->|"adapt"| node_parserproc
node_dash -->|"adapt"| node_parserproc
node_mss -->|"adapt"| node_parserproc
node_parserentry -->|"emit tracks"| node_sharedmodel
node_sharedmodel -->|"plan download"| node_downloadmgr
node_downloadmgr -->|"fetch segments"| node_downloader
node_downloader -->|"request"| node_network
node_downloadmgr -->|"decrypt"| node_decrypt
node_decrypt -->|"invoke"| node_tools
node_downloadmgr -->|"merge/mux"| node_postprocess
node_postprocess -->|"invoke"| node_tools
node_downloadmgr -->|"site rules"| node_urlproc
node_postprocess -->|"write outputs"| node_sharedmodel
node_commonutil -->|"http/retry"| node_network
node_logging -->|"log"| node_program
node_sharedmodel -->|"types"| node_enums
node_testsuite -->|"verify"| node_parserentry
node_testsuite -->|"verify"| node_commonutil
node_fixtures -->|"data"| node_testsuite

click node_program "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE/Program.cs"
click node_commandline "https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/CommandLine"
click node_downloader "https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Downloader"
click node_downloadmgr "https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/DownloadManager"
click node_decrypt "https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Crypto"
click node_postprocess "https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Util"
click node_appcfg "https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Config"
click node_urlproc "https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Processor"
click node_parserentry "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/StreamExtractor.cs"
click node_hls "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs"
click node_dash "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs"
click node_mss "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/MSSExtractor.cs"
click node_livets "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/LiveTSExtractor.cs"
click node_parserproc "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Processor"
click node_parserutil "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Util"
click node_sharedmodel "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Entity"
click node_enums "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Enum"
click node_commonutil "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Util"
click node_logging "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Log"
click node_testsuite "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Tests"
click node_fixtures "https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Tests/Resources"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_program,node_commandline,node_downloader,node_downloadmgr,node_decrypt,node_postprocess,node_appcfg,node_urlproc toneBlue
class node_parserentry,node_hls,node_dash,node_mss,node_livets,node_parserproc,node_parserutil toneAmber
class node_sharedmodel,node_enums,node_commonutil,node_logging toneMint
class node_testsuite,node_fixtures toneRose
class node_network,node_tools toneIndigo
```

## Components

### N_m3u8DL-RE

CLI runtime, download orchestration, decryption, merge, and mux stages.

- [Program](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE/Program.cs): cli entrypoint · Starts the command-line tool and selects the processing path. · `src/N_m3u8DL-RE/Program.cs`
- [Command line](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/CommandLine): arg parsing · Parses options and complex parameters into runtime settings. · `src/N_m3u8DL-RE/CommandLine`
- [Downloader](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Downloader): segment fetch · Download abstraction and implementation for media segments. · `src/N_m3u8DL-RE/Downloader`
- [Download mgr](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/DownloadManager): orchestration · Coordinates standard download, live recording, and live merge flows. · `src/N_m3u8DL-RE/DownloadManager`
- [Decrypt](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Crypto): crypto stage · Handles media decryption and external decrypt engine integration. · `src/N_m3u8DL-RE/Crypto`
- [Post-process](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Util): merge/mux · Merges outputs, fixes subtitles, and assembles final media files. · `src/N_m3u8DL-RE/Util`
- [App config](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Config): runtime config · Downloader and environment settings that steer CLI behavior. · `src/N_m3u8DL-RE/Config`
- [URL processors](https://github.com/nilaoda/n_m3u8dl-re/tree/main/src/N_m3u8DL-RE/Processor): site adapters · Site-specific URL rewriting and request shaping extensions. · `src/N_m3u8DL-RE/Processor`

### N_m3u8DL-RE.Parser

Manifest extraction, protocol adapters, and playlist normalization.

- [Stream extractor](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/StreamExtractor.cs): parse boundary · Generic entrypoint that resolves an input into stream descriptions. · `src/N_m3u8DL-RE.Parser/StreamExtractor.cs`
- [HLS extractor](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs): protocol parser · Extracts HLS playlists, keys, and track metadata. · `src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs`
- [DASH extractor](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs): protocol parser · Extracts DASH manifests and normalized segment structures. · `src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs`
- [MSS extractor](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/MSSExtractor.cs): protocol parser · Extracts Microsoft Smooth Streaming manifests and media data. · `src/N_m3u8DL-RE.Parser/Extractor/MSSExtractor.cs`
- [Live TS extractor](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Extractor/LiveTSExtractor.cs): live parser · Specialized extractor for live transport-stream style inputs. · `src/N_m3u8DL-RE.Parser/Extractor/LiveTSExtractor.cs`
- [Parser processors](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Processor): content adaptation · Default URL, content, and key processors for protocol normalization. · `src/N_m3u8DL-RE.Parser/Processor`
- [Parser util](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Parser/Util): support code · Parser helper utilities and protocol constants. · `src/N_m3u8DL-RE.Parser/Util`

### N_m3u8DL-RE.Common

Shared media model, enums, logging, HTTP, and utility infrastructure.

- [Media model](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Entity): domain contract · Playlists, segments, parts, stream specs, encryption, subtitles, and MSS data. · `src/N_m3u8DL-RE.Common/Entity`
- [Enums](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Enum): metadata · Media type, encryption, extractor type, and role enums. · `src/N_m3u8DL-RE.Common/Enum`
- [Common util](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Util): infra helpers · HTTP, retry, global, binary-check, and hex helpers. · `src/N_m3u8DL-RE.Common/Util`
- [Logging](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Common/Log): console log · Shared logger and console formatting. · `src/N_m3u8DL-RE.Common/Log`

### N_m3u8DL-RE.Tests

Parser and utility verification with fixture manifests.

- [Tests](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Tests): verification · Parser and utility tests for the critical manifest boundary. · `src/N_m3u8DL-RE.Tests`
- [Fixtures](https://github.com/nilaoda/n_m3u8dl-re/blob/main/src/N_m3u8DL-RE.Tests/Resources): sample manifests · Sample DASH manifests used to validate extraction behavior. · `src/N_m3u8DL-RE.Tests/Resources`

### External deps

Network endpoints and helper binaries invoked by the workflow.

- **HTTP endpoints**: external input · Manifest and segment sources consumed by the downloader and parser.
- **External tools**: helper binaries · ffmpeg, mkvmerge, mp4decrypt, and shaka-packager when configured.

## Connections

- Program → Command line: parse · CLI startup flows into typed option parsing.
- Command line → App config: configure · Parsed arguments populate runtime configuration.
- Program → Stream extractor: resolve input · The selected source is resolved into stream metadata.
- Stream extractor → HLS extractor: HLS · Protocol-specific extraction for HLS inputs.
- Stream extractor → DASH extractor: DASH · Protocol-specific extraction for DASH inputs.
- Stream extractor → MSS extractor: MSS · Protocol-specific extraction for MSS inputs.
- Stream extractor → Live TS extractor: live TS · Live stream inputs can use a dedicated extractor path.
- HLS extractor → Parser processors: adapt · HLS extraction uses URL/content/key processors for normalization.
- DASH extractor → Parser processors: adapt · DASH extraction uses content adaptation and URL processing.
- MSS extractor → Parser processors: adapt · MSS extraction is normalized through shared processor logic.
- Stream extractor → Media model: emit tracks · Extractors produce the shared playlist and segment contract.
- Media model → Download mgr: plan download · Normalized tracks are handed to the download manager.
- Download mgr → Downloader: fetch segments · Orchestration delegates actual segment acquisition.
- Downloader → HTTP endpoints: request · Segments and manifests are retrieved over HTTP.
- Download mgr → Decrypt: decrypt · Protected streams are decrypted during or after acquisition.
- Decrypt → External tools: invoke · External decrypt engines may be used when configured.
- Download mgr → Post-process: merge/mux · Downloaded pieces move to merge and mux post-processing.
- Post-process → External tools: invoke · ffmpeg, mkvmerge, and related binaries support final assembly.
- Download mgr → URL processors: site rules · Site-specific processors can rewrite or normalize requests.
- Post-process → Media model: write outputs · Output artifacts preserve media metadata and track structure.
- Common util → HTTP endpoints: http/retry · Shared HTTP and retry helpers support manifest and segment access.
- Logging → Program: log · CLI and subsystems share centralized console logging.
- Media model → Enums: types · Shared entities rely on protocol and track metadata enums.
- Tests → Stream extractor: verify · Tests validate manifest-to-stream extraction behavior.
- Tests → Common util: verify · Utility tests cover shared helper behavior.
- Fixtures → Tests: data · Sample manifests provide parser fixtures.
