# nilaoda/n_m3u8dl-cli architecture

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

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

## Overview

##### Project Type & Purpose
- The project is a command‐line application (CLI) developed in .NET Framework.
- Its core purpose is to download m3u8 streams (including live streams) and output saved files (mp4, ts, or audio-only), while supporting decryption (AES-128 and ChaCha20), multi-threaded downloads, range limits, and integration with external tools such as FFmpeg.
- The README shows rich command-line options and an integrated mechanism for launching via URI protocol, which further emphasizes its role as a highly configurable media downloader.

##### Analyzing the File Structure
- The top-level solution file (N_m3u8DL-CLI.sln) indicates a Visual Studio .NET solution.
- The project folder “N_m3u8DL-CLI” contains all the main source code files:
– Files like Program.cs serve as the entry point.
– Classes such as DownloadManager.cs, Downloader.cs, ProgressReporter.cs, Logger (LOGGER.cs), and Watcher.cs define the core processing logic.
– A series of “Decode…” files (Decode51CtoKey.cs, DecodeCdeledu.cs, DecodeDdyun.cs, etc.) indicate modules specialised to handle various decryption strategies for different m3u8 sources.
– Utility classes for parsing (Parser.cs, MPDParser.cs, IqJsonParser.cs) and for handling options (MyOptions.cs) suggest modular separation of concerns.
– The integration file FFmpeg.cs hints at the use of an external tool (ffmpeg) for media operations.
- Additionally, supporting resources (configuration files like App.config, resource strings, and dynamically generated documentation in the docs folder) provide context for runtime configuration and documentation.

##### Key Architectural Components
a. Command Line Interface (CLI) & Options Parser:
– Start with an “Entry Point” (Program.cs) that handles argument parsing via MyOptions.cs.

b. Download Manager & Downloader Components:
– Illustrate a module labeled “Download Manager” (DownloadManager.cs) responsible for managing multiple download threads.
– Inside this area, include “Downloader” (Downloader.cs) which handles the low-level HTTP requests, threading, timeout management, and connection retries.
– Highlight interdependencies with “ProgressReporter” (ProgressReporter.cs) for tracking and reporting download progress.

c. Parsing and Decryption Modules:
– Show a modular “Decrypter” component that further divides into specialized submodules (e.g., Decrypter.cs and various Decode* files) to handle different encryption schemes (AES-128, ChaCha20, and other custom decryptions).
– Present communication between the parser and decryption modules so that after segment parsing, the proper decryption routine is applied when needed.

d. External Tools & Integration:
– Include FFmpeg integration (FFmpeg.cs) for merging media segments.
– Optionally, indicate a “File Merger” component utilizing either binary merge or invoking FFmpeg for final media file assembly.
– Also, portray optional integration with proxy handling and custom HTTP headers provided through the options.

e. Ancillary Utilities:
– Logger (LOGGER.cs) for error/debug information.
– Watcher (Watcher.cs) likely for monitoring file system changes or download progress.
– Utility classes for handling configuration and resources (e.g., App.config, strings.resx).

##### Relationships & Data Flow
- Start from the user’s CLI input, which flows into the Options Parser.
- The parsed options direct the flow of execution: the Download Manager coordinates the segment downloads by dispatching tasks to multiple Downloader workers.
- As playlists are parsed, the Parser sends each segment URL to the Downloader, optionally routing segments through the appropriate Decryption module.
- Post-download, the segments are merged by either a native binary merger or by calling FFmpeg, with progress tracked by the ProgressReporter.
- Error handling and logging are pervasive across all modules via the Logger component.

##### Architectural Patterns & Design Principles
- The design follows a modular architecture where each functionality (parsing, downloading, decryption, merging) resides in its own component with single responsibility.
- There is a clear separation between I/O related tasks (download, file merging) and business logic (parsing m3u8 data, decrypting).
- Multi-threading is managed via configurable options (maxThreads, minThreads), indicating a scalable concurrent download architecture.
- The project leverages external protocols (m3u8dl:// registering) to integrate with the OS’s URI handling, showing extensibility.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Input
    UI["User Input (CLI)"]:::cli

    %% CLI & Options Parser Subgraph
    subgraph "CLI & Options Parser"
        CLI1["Program.cs"]:::cli
        CLI2["MyOptions.cs"]:::cli
    end

    %% Download Manager Subgraph
    subgraph "Download Manager"
        DM["DownloadManager.cs"]:::download
        DLD["Downloader.cs"]:::download
        PR["ProgressReporter.cs"]:::download
    end

    %% Parsing Modules Subgraph
    subgraph "Parsing Modules"
        P1["Parser.cs"]:::parsing
        P2["MPDParser.cs"]:::parsing
        P3["HLSTags.cs"]:::parsing
        P4["IqJsonParser.cs"]:::parsing
    end

    %% Decryption Modules Subgraph
    subgraph "Decryption Modules"
        DECR["Decrypter.cs"]:::decryption
        DEC1["Decode51CtoKey.cs"]:::decryption
        DEC2["DecodeCdeledu.cs"]:::decryption
        DEC3["DecodeDdyun.cs"]:::decryption
        DEC4["DecodeHuke88Key.cs"]:::decryption
        DEC5["DecodeImooc.cs"]:::decryption
        DEC6["DecodeNfmovies.cs"]:::decryption
    end

    %% External Tools & Merger Subgraph
    subgraph "External Tools & Merger"
        FF["FFmpeg.cs"]:::external
    end

    %% Utilities Subgraph
    subgraph "Utilities"
        LOG["LOGGER.cs"]:::utility
        WCH["Watcher.cs"]:::utility
    end

    %% Configuration & Resources Subgraph
    subgraph "Configuration & Resources"
        CFG["App.config"]:::config
        RS["Resource Strings"]:::config
    end

    %% Data Flow Connections
    UI --> CLI1
    CLI1 --> CLI2
    CLI2 --> DM
    DM --> DLD
    DM --> PR
    DLD --> P1
    %% Branch: if not encrypted go directly to merger
    P1 -->|"if_not_encrypted"| FF
    %% Branch: if encrypted, go through decryption then merger
    P1 -->|"if_encrypted"| DECR
    DECR --> FF

    %% Logging and Monitoring (dashed connections)
    DM --- LOG
    DLD --- LOG
    P1 --- LOG
    DECR --- LOG
    FF --- LOG
    DM --- WCH

    %% Styles
    classDef cli fill:#f4e1ff,stroke:#7d3cff,stroke-width:2px;
    classDef download fill:#e1f7ff,stroke:#00aaff,stroke-width:2px;
    classDef parsing fill:#e1ffe1,stroke:#00cc44,stroke-width:2px;
    classDef decryption fill:#fff1e6,stroke:#ff9933,stroke-width:2px;
    classDef external fill:#fffde7,stroke:#fbc02d,stroke-width:2px;
    classDef utility fill:#fce4ec,stroke:#e91e63,stroke-width:2px;
    classDef config fill:#eceff1,stroke:#607d8b,stroke-width:2px;

    %% Click Events for CLI & Options Parser
    click CLI1 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/Program.cs"
    click CLI2 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/MyOptions.cs"

    %% Click Events for Download Manager
    click DM "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/DownloadManager.cs"
    click DLD "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/Downloader.cs"
    click PR "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/ProgressReporter.cs"

    %% Click Events for Parsing Modules
    click P1 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/Parser.cs"
    click P2 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/MPDParser.cs"
    click P3 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/HLSTags.cs"
    click P4 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/IqJsonParser.cs"

    %% Click Events for Decryption Modules
    click DECR "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/Decrypter.cs"
    click DEC1 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/Decode51CtoKey.cs"
    click DEC2 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/DecodeCdeledu.cs"
    click DEC3 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/DecodeDdyun.cs"
    click DEC4 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/DecodeHuke88Key.cs"
    click DEC5 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/DecodeImooc.cs"
    click DEC6 "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/DecodeNfmovies.cs"

    %% Click Event for External Tools
    click FF "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/FFmpeg.cs"

    %% Click Events for Utilities
    click LOG "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/LOGGER.cs"
    click WCH "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/Watcher.cs"

    %% Click Events for Configuration & Resources
    click CFG "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/App.config"
    click RS "https://github.com/nilaoda/n_m3u8dl-cli/blob/master/N_m3u8DL-CLI/strings.resx"
```
