# asmsafone/musicplayer architecture

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

- Interactive diagram: https://gitdiagram.com/asmsafone/musicplayer
- Repository: https://github.com/asmsafone/musicplayer

## Overview

#### Purpose and runtime

MusicPlayer is a Python Telegram music bot. It receives Telegram commands through Pyrogram and streams audio/video into Telegram voice chats through Py-TgCalls. It supports independent playback queues per chat, including YouTube content, playlists, uploaded media, and live stream URLs.

Primary process entry point: `main.py`. Runtime configuration is loaded from `config.py`, with environment-variable examples in `sample.env`.

#### Application bootstrap and deployment

`main.py` starts the bot/user session and registers the application behavior implemented under `core/`. Deployment targets include direct Python execution, Docker, and Heroku-style process startup.

- `main.py`
- `Dockerfile`
- `Procfile`
- `startup.sh`
- `heroku.yml`

The host must provide FFmpeg for media processing/streaming. Python dependencies are declared in `requirements.txt`; the Python runtime version is pinned by `runtime.txt`.

#### Telegram command and authorization boundary

Telegram messages are the external control plane. Pyrogram handlers dispatch commands such as play, skip, pause, queue management, language changes, and restart. Admin/sudo restrictions protect sensitive operations and optionally restrict playback requests.

- `core/admins.py`
- `core/decorators.py`
- `core/groups.py`
- `main.py`

This boundary separates untrusted group/user commands from operations that alter voice-chat state, queue state, or process lifecycle.

#### Playback orchestration and per-chat queues

The queue subsystem holds playback state separately for each Telegram chat. Commands append tracks, inspect/reorder queues, loop tracks, advance/skip, clear queues, and import/export queue contents. Playback control coordinates this state with the voice-chat streaming layer.

- `core/queue.py`
- `core/song.py`
- `core/stream.py`

The central control flow is: command request → resolve a playable source → enqueue or replace the chat’s current item → start/alter Py-TgCalls stream → advance according to completion, skip, or loop behavior.

#### Media source resolution and streaming pipeline

Track handling accepts search terms, YouTube links/live streams/playlists, Telegram media, and custom URL streams. It is designed to begin streaming while download/conversion progresses rather than materializing persistent media files. FFmpeg is the key media runtime boundary; upstream providers include YouTube and optionally Spotify credentials for source metadata/resolution.

- `core/song.py`
- `core/stream.py`
- `core/funcs.py`

`core/song.py` represents or resolves requested media, while `core/stream.py` supplies the stream to Telegram voice chat and applies configured audio/video mode and quality.

#### Localization and user-facing responses

Command responses are language-driven rather than hardcoded to one locale. The selected language can be changed per group, and translation catalogs provide the user-visible command/status strings.

- `lang/__init__.py`
- `lang/en.json`
- `lang/ar.json`
- `lang/tr.json`

#### Configuration and external services

Required external identities are Telegram API credentials plus a Pyrogram string session; an optional bot token supports bot operation. Telegram is both the command transport and voice-chat platform. Spotify is optional. Environment settings also govern sudo users, command prefixes, default language, media mode, quality, and admin-only playback.

- `config.py`
- `sample.env`
- `genStr.py`

`genStr.py` is the session-generation entry point for creating the Pyrogram session value consumed by the main runtime.

Main entry points: [`main.py`](https://github.com/asmsafone/musicplayer/blob/master/main.py) (Command Handlers), [`core/song.py`](https://github.com/asmsafone/musicplayer/blob/master/core/song.py) (Source Resolution), [`core/stream.py`](https://github.com/asmsafone/musicplayer/blob/master/core/stream.py) (Voice Chat Stream), [`core/queue.py`](https://github.com/asmsafone/musicplayer/blob/master/core/queue.py) (Per-Chat Queues)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime &amp; Deployment"]
  node_main["Application Bootstrap<br/>Python entry point<br/>[main.py]"]
  node_config["Runtime Configuration<br/>configuration module<br/>[config.py]"]
  node_deployment["Deployment Launchers<br/>deployment artifacts"]
  node_session_generator["Session Generator<br/>utility entry point<br/>[genStr.py]"]
end

subgraph group_control["Telegram Control Plane"]
  node_commands["Command Handlers<br/>Pyrogram handlers<br/>[main.py]"]
  node_authorization{{"Admin &amp; Sudo Guard<br/>authorization boundary<br/>[admins.py]"}}
  node_group_policy["Group Policy<br/>group settings<br/>[groups.py]"]
end

subgraph group_playback["Playback Pipeline"]
  node_queue["Per-Chat Queues<br/>queue state<br/>[queue.py]"]
  node_song["Source Resolution<br/>media resolver<br/>[song.py]"]
  node_playback_control["Playback Control<br/>orchestrator<br/>[stream.py]"]
  node_ffmpeg{{"FFmpeg<br/>media runtime"}}
  node_voice_stream["Voice Chat Stream<br/>Py-TgCalls integration<br/>[stream.py]"]
  node_media_utilities["Media Utilities<br/>media helpers<br/>[funcs.py]"]
end

subgraph group_experience["Localization"]
  node_translations["Translation Catalogs<br/>locale resources<br/>[en.json]"]
  node_language_selection["Group Language<br/>locale selection<br/>[__init__.py]"]
end

subgraph group_external["External Platforms"]
  node_telegram(("Telegram<br/>command and voice platform"))
  node_media_providers(("YouTube &amp; Spotify<br/>media providers"))
end

node_config -->|"configures"| node_main
node_session_generator -->|"creates session for"| node_config
node_deployment -->|"launches"| node_main
node_main -->|"registers"| node_commands
node_commands -->|"checks"| node_authorization
node_commands -->|"applies settings"| node_group_policy
node_commands -->|"resolves request"| node_song
node_commands -->|"updates"| node_queue
node_authorization -->|"permits control"| node_playback_control
node_queue -->|"supplies current item"| node_playback_control
node_song -->|"enqueues playable item"| node_queue
node_song -->|"resolves from"| node_media_providers
node_song -->|"uses"| node_media_utilities
node_playback_control -->|"starts or alters"| node_voice_stream
node_voice_stream -->|"processes through"| node_ffmpeg
node_voice_stream -->|"streams into"| node_telegram
node_commands -->|"changes"| node_language_selection
node_language_selection -->|"selects"| node_translations
node_commands -->|"receives messages from"| node_telegram
node_main -->|"opens Pyrogram session"| node_telegram

click node_main "https://github.com/asmsafone/musicplayer/blob/master/main.py"
click node_config "https://github.com/asmsafone/musicplayer/blob/master/config.py"
click node_deployment "https://github.com/asmsafone/musicplayer/tree/master/Dockerfile"
click node_commands "https://github.com/asmsafone/musicplayer/blob/master/main.py"
click node_authorization "https://github.com/asmsafone/musicplayer/blob/master/core/admins.py"
click node_group_policy "https://github.com/asmsafone/musicplayer/blob/master/core/groups.py"
click node_queue "https://github.com/asmsafone/musicplayer/blob/master/core/queue.py"
click node_song "https://github.com/asmsafone/musicplayer/blob/master/core/song.py"
click node_playback_control "https://github.com/asmsafone/musicplayer/blob/master/core/stream.py"
click node_voice_stream "https://github.com/asmsafone/musicplayer/blob/master/core/stream.py"
click node_media_utilities "https://github.com/asmsafone/musicplayer/blob/master/core/funcs.py"
click node_translations "https://github.com/asmsafone/musicplayer/blob/master/lang/en.json"
click node_language_selection "https://github.com/asmsafone/musicplayer/blob/master/lang/__init__.py"
click node_session_generator "https://github.com/asmsafone/musicplayer/blob/master/genStr.py"

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_main,node_config,node_deployment,node_session_generator toneBlue
class node_commands,node_authorization,node_group_policy toneAmber
class node_queue,node_song,node_playback_control,node_ffmpeg,node_voice_stream,node_media_utilities toneMint
class node_translations,node_language_selection toneRose
class node_telegram,node_media_providers toneIndigo
```

## Components

### Runtime & Deployment

- [Application Bootstrap](https://github.com/asmsafone/musicplayer/blob/master/main.py): Python entry point · Starts sessions and application behavior. · `main.py`
- [Runtime Configuration](https://github.com/asmsafone/musicplayer/blob/master/config.py): configuration module · Loads credentials and playback settings. · `config.py`
- [Deployment Launchers](https://github.com/asmsafone/musicplayer/tree/master/Dockerfile): deployment artifacts · Supports Docker, Heroku, and direct process startup. · `Dockerfile`
- [Session Generator](https://github.com/asmsafone/musicplayer/blob/master/genStr.py): utility entry point · Creates the Pyrogram session value for runtime use. · `genStr.py`

### Telegram Control Plane

- [Command Handlers](https://github.com/asmsafone/musicplayer/blob/master/main.py): Pyrogram handlers · Dispatches playback, queue, language, and restart commands. · `main.py`
- [Admin & Sudo Guard](https://github.com/asmsafone/musicplayer/blob/master/core/admins.py): authorization boundary · Restricts sensitive and optionally playback operations. · `core/admins.py`
- [Group Policy](https://github.com/asmsafone/musicplayer/blob/master/core/groups.py): group settings · `core/groups.py`

### Playback Pipeline

- [Per-Chat Queues](https://github.com/asmsafone/musicplayer/blob/master/core/queue.py): queue state · Maintains independent playback state for each chat. · `core/queue.py`
- [Source Resolution](https://github.com/asmsafone/musicplayer/blob/master/core/song.py): media resolver · Resolves search, links, playlists, uploads, and stream URLs. · `core/song.py`
- [Playback Control](https://github.com/asmsafone/musicplayer/blob/master/core/stream.py): orchestrator · Coordinates queue changes, completion, skip, and looping. · `core/stream.py`
- **FFmpeg**: media runtime · Processes media for audio or video streaming.
- [Voice Chat Stream](https://github.com/asmsafone/musicplayer/blob/master/core/stream.py): Py-TgCalls integration · Streams resolved media into Telegram voice chats. · `core/stream.py`
- [Media Utilities](https://github.com/asmsafone/musicplayer/blob/master/core/funcs.py): media helpers · `core/funcs.py`

### Localization

- [Translation Catalogs](https://github.com/asmsafone/musicplayer/blob/master/lang/en.json): locale resources · Provides language-driven command and status text. · `lang/en.json`
- [Group Language](https://github.com/asmsafone/musicplayer/blob/master/lang/__init__.py): locale selection · Stores or applies the selected group language. · `lang/__init__.py`

### External Platforms

- **Telegram**: command and voice platform · Provides message transport and voice chats.
- **YouTube & Spotify**: media providers · Supply playable sources and optional metadata resolution.

## Connections

- Runtime Configuration → Application Bootstrap: configures
- Session Generator → Runtime Configuration: creates session for
- Deployment Launchers → Application Bootstrap: launches
- Application Bootstrap → Command Handlers: registers
- Command Handlers → Admin & Sudo Guard: checks
- Command Handlers → Group Policy: applies settings
- Command Handlers → Source Resolution: resolves request
- Command Handlers → Per-Chat Queues: updates
- Admin & Sudo Guard → Playback Control: permits control
- Per-Chat Queues → Playback Control: supplies current item
- Source Resolution → Per-Chat Queues: enqueues playable item
- Source Resolution → YouTube & Spotify: resolves from
- Source Resolution → Media Utilities: uses
- Playback Control → Voice Chat Stream: starts or alters
- Voice Chat Stream → FFmpeg: processes through
- Voice Chat Stream → Telegram: streams into
- Command Handlers → Group Language: changes
- Group Language → Translation Catalogs: selects
- Command Handlers → Telegram: receives messages from
- Application Bootstrap → Telegram: opens Pyrogram session
