How nilaoda/n_m3u8dl-re works
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: StreamExtractor.cs Stream extractor · DownloadManager Download mgr · Entity Media model · Util Post-process
Components 23
N_m3u8DL-RE
CLI runtime, download orchestration, decryption, merge, and mux stages.
- Program · Starts the command-line tool and selects the processing path.
src/N_m3u8DL-RE/Program.cs - Command line · Parses options and complex parameters into runtime settings.
src/N_m3u8DL-RE/CommandLine - Downloader · Download abstraction and implementation for media segments.
src/N_m3u8DL-RE/Downloader - Download mgr · Coordinates standard download, live recording, and live merge flows.
src/N_m3u8DL-RE/DownloadManager - Decrypt · Handles media decryption and external decrypt engine integration.
src/N_m3u8DL-RE/Crypto - Post-process · Merges outputs, fixes subtitles, and assembles final media files.
src/N_m3u8DL-RE/Util - App config · Downloader and environment settings that steer CLI behavior.
src/N_m3u8DL-RE/Config - URL processors · 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 · Generic entrypoint that resolves an input into stream descriptions.
src/N_m3u8DL-RE.Parser/StreamExtractor.cs - HLS extractor · Extracts HLS playlists, keys, and track metadata.
src/N_m3u8DL-RE.Parser/Extractor/HLSExtractor.cs - DASH extractor · Extracts DASH manifests and normalized segment structures.
src/N_m3u8DL-RE.Parser/Extractor/DASHExtractor2.cs - MSS extractor · Extracts Microsoft Smooth Streaming manifests and media data.
src/N_m3u8DL-RE.Parser/Extractor/MSSExtractor.cs - Live TS extractor · Specialized extractor for live transport-stream style inputs.
src/N_m3u8DL-RE.Parser/Extractor/LiveTSExtractor.cs - Parser processors · Default URL, content, and key processors for protocol normalization.
src/N_m3u8DL-RE.Parser/Processor - Parser util · 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 · Playlists, segments, parts, stream specs, encryption, subtitles, and MSS data.
src/N_m3u8DL-RE.Common/Entity - Enums · Media type, encryption, extractor type, and role enums.
src/N_m3u8DL-RE.Common/Enum - Common util · HTTP, retry, global, binary-check, and hex helpers.
src/N_m3u8DL-RE.Common/Util - Logging · Shared logger and console formatting.
src/N_m3u8DL-RE.Common/Log
N_m3u8DL-RE.Tests
Parser and utility verification with fixture manifests.
External deps
Network endpoints and helper binaries invoked by the workflow.
- HTTP endpoints · Manifest and segment sources consumed by the downloader and parser.
- External tools · ffmpeg, mkvmerge, mp4decrypt, and shaka-packager when configured.
Connections 26
- 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.