Yt-dlp & FFmpeg Telegram Media Archiver Bot
An asynchronous Rust-based Telegram bot engine that orchestrates yt-dlp and FFmpeg subprocesses to securely download, compress, and serve media from X (Twitter) and YouTube.
Performance & metrics
- Lock contention is aggressively minimized by extracting memory snapshots prior to executing disk I/O, ensuring the critical path remains unblocked
- The Tokio event loop remains strictly non-blocking during heavy media transcoding pipelines by utilizing proper asynchronous subprocess spawning
- Memory footprint is optimized by reading process standard outputs asynchronously in chunked buffers
Why I built it
Relying on ad-ridden, heavily rate-limited third-party websites to archive media from X and YouTube disrupts workflow efficiency. I engineered this isolated bot architecture to directly orchestrate CLI tools (yt-dlp, FFmpeg) as asynchronous subprocesses over Telegram, integrating a strict authorization layer to prevent abuse and ensure absolute ownership of the media pipeline.
Architecture & decisions
The core operates on the teloxide framework utilizing the tokio asynchronous runtime. While network I/O is handled asynchronously, the actual video extraction is offloaded to yt-dlp via tokio::process::Command. The subprocess's stdout is piped and parsed in real-time using Regex to project a dynamic progress bar onto the Telegram UI. If the downloaded payload exceeds predefined environment size constraints, ffmpeg is conditionally invoked to aggressively compress the media utilizing libx264 and aac codecs with faststart headers. In-memory state (users and audit logs) is strictly protected via Arc<RwLock>, while physical persistence is guaranteed through an atomic I/O write-and-rename mechanism to completely eliminate JSON file corruption.
Key features
- Real-time asynchronous progress reporting achieved by regex-parsing yt-dlp piped stdout buffers
- Autonomous FFmpeg H.264/AAC downsampling triggered by environment-based file size thresholds
- Native compatibility with Local Telegram API servers via custom endpoint routing to bypass default 50MB upload restrictions
- Atomic file writing (rename syscall over .tmp) to enforce zero-corruption data persistence
- Thread-safe authorization and metric tracking utilizing RwLock synchronization primitives
Technical review
This project effectively demonstrates the robust integration of the Rust asynchronous ecosystem with native OS subprocesses. Pinging the tokio::process::Command stdout buffer in a non-blocking loop to update a UI progress bar requires precise stream handling. Additionally, to avoid the overhead of a dedicated database, I opted for local JSON storage. To counter the inherent risks of concurrent file I/O corruption, I implemented a strict atomic write pattern leveraging temporary files and OS-level atomic rename operations, guaranteeing absolute data integrity.
Related projects
Rust Telegram Watchlist Orchestrator
An asynchronous Telegram bot engineered in Rust for managing localized media watchlists, featuring randomized suggestion engines and flat-file state persistence.
- Rust
- Teloxide
- Tokio
- +2
Lain Discord Bot (Rust)
A high-performance, asynchronous Discord bot written in Rust, featuring targeted message delivery and strict RBAC (Role-Based Access Control) for administrators.
- Rust
- Serenity
- Discord API
GitHub Commit History Rewriter & Metadata Automator
A multi-threaded DevOps script engineered to automate the rewriting of Git commit history across bulk repositories. It dynamically manipulates commit author metadata using environment filters and force-pushes the sanitized trees concurrently.
- PowerShell
- Shell
- GitHub REST API
- +2
Need something similar?
I can build a system like this for you, too.
Describe your project in a few sentences, and we'll work out the right approach and an estimated scope together.