├── LICENSE ├── README.md ├── bot.py ├── config.py └── requirements.txt /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2024 Hexa 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🎧 CompressBot 🎥 2 | 3 | ![Screenshot from 2024-08-10 09-12-16](https://github.com/user-attachments/assets/935c3079-5da2-4bf9-9362-c922b410d1a8) 4 | 5 | Welcome to **CompressBot**—your all-in-one Telegram bot for compressing audio and video files with ease! 🚀 6 | 7 | ## 🌟 Features 8 | 9 | - **Compress Audio 🎧**: Convert and compress your audio files to a smaller size while maintaining good quality. 10 | - **Compress Video 🎥**: Reduce the size of your video files with efficient compression without significant loss in quality. 11 | - **User-Friendly**: Simple commands and easy-to-use interface. 12 | - **Fast Processing ⚡**: Get your compressed files quickly. 13 | 14 | ## 🛠 Installation 15 | 16 | Follow these steps to set up the bot locally: 17 | 18 | 1. **Clone the repository**: 19 | ```bash 20 | git clone https://github.com/superhexa/CompressBot.git 21 | cd CompressBot 22 | ``` 23 | 24 | 2. **Install Dependencies**: 25 | ```bash 26 | pip install -r requirements.txt 27 | ``` 28 | 29 | 3. **Configure Environment Variables**: 30 | - Open the `config.py` file in the root directory. 31 | - Add your credentials: 32 | ```python 33 | API_ID = 'api_id' 34 | API_HASH = 'api_hash' 35 | API_TOKEN = 'bot_token' 36 | ``` 37 | 38 | 4. **Run the bot**: 39 | ```bash 40 | python bot.py 41 | ``` 42 | 43 | ## 📦 Requirements 44 | 45 | - Python 3.7+ 46 | - [Pyrogram](https://docs.pyrogram.org/) - For Telegram bot API interaction 47 | - [Pydub](https://pydub.com/) - For audio processing 48 | - [FFmpeg](https://ffmpeg.org/) - For video compression 49 | 50 | ## 🚀 Usage 51 | 52 | 1. **Start the bot** by sending the `/start` command. 53 | 2. **Choose** between compressing audio or video files. 54 | 3. **Upload** your media file. 55 | 4. **Receive** the compressed file instantly! 56 | 57 | ## 🛠 Configuration 58 | 59 | You can adjust the compression parameters in the script to suit your needs: 60 | 61 | - **Audio Compression**: Adjust the `bitrate` and `format` in the `handle_audio` function. 62 | - **Video Compression**: Modify the FFmpeg command in the `handle_video` function to tweak video resolution, bitrate, etc. 63 | 64 | ## 🐛 Issues 65 | 66 | If you encounter any issues or have suggestions, please feel free to open an [issue](https://github.com/superhexa/CompressBot/issues) or submit a pull request. 67 | 68 | ## 📜 License 69 | 70 | This project is licensed under the MIT License—see the [LICENSE](LICENSE) file for details. 71 | 72 | --- 73 | 74 | **Enjoy CompressBot and make your media sharing easier! 🎉** 75 | -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tempfile 3 | import subprocess 4 | from pyrogram import Client, filters 5 | from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery 6 | from pydub import AudioSegment 7 | from config import * 8 | 9 | app = Client("bot", api_id=API_ID, api_hash=API_HASH, bot_token=API_TOKEN) 10 | 11 | @app.on_message(filters.command("start")) 12 | def start(client, message): 13 | markup = InlineKeyboardMarkup([[InlineKeyboardButton("Compress Audio 🎧", callback_data="compress_audio"), 14 | InlineKeyboardButton("Compress Video 🎥", callback_data="compress_video")]]) 15 | message.reply_text("Choose what you want to compress:", reply_markup=markup) 16 | 17 | @app.on_callback_query() 18 | def callback(client, callback_query: CallbackQuery): 19 | callback_query.message.reply_text("Send me a file.") 20 | 21 | @app.on_message(filters.voice | filters.audio) 22 | def handle_audio(client, message): 23 | file = client.download_media(message.voice.file_id if message.chat.type == "voice" else message.audio.file_id) 24 | audio = AudioSegment.from_file(file).set_channels(AUDIO_CHANNELS).set_frame_rate(AUDIO_SAMPLE_RATE) 25 | with tempfile.NamedTemporaryFile(suffix=TEMP_FILE_SUFFIX_AUDIO, delete=False) as temp_file: 26 | temp_filename = temp_file.name 27 | audio.export(temp_filename, format=AUDIO_FORMAT, bitrate=AUDIO_BITRATE) 28 | message.reply_document(temp_filename) 29 | os.remove(file) 30 | os.remove(temp_filename) 31 | 32 | @app.on_message(filters.video | filters.animation) 33 | def handle_media(client, message): 34 | file = client.download_media(message.video.file_id if message.video else message.animation.file_id) 35 | with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file: 36 | temp_filename = temp_file.name 37 | if message.animation: subprocess.run(f'ffmpeg -i "{file}" "{temp_filename}"', shell=True, check=True) 38 | subprocess.run(f'ffmpeg -i "{file}" -filter_complex "scale={VIDEO_SCALE}" -r {VIDEO_FPS} -c:v {VIDEO_CODEC} -pix_fmt {VIDEO_PIXEL_FORMAT} -b:v {VIDEO_BITRATE} -crf {VIDEO_CRF} -preset {VIDEO_PRESET} -c:a {VIDEO_AUDIO_CODEC} -b:a {VIDEO_AUDIO_BITRATE} -ac {VIDEO_AUDIO_CHANNELS} -ar {VIDEO_AUDIO_SAMPLE_RATE} -profile:v {VIDEO_PROFILE} -map_metadata -1 "{temp_filename}"', shell=True, check=True) 39 | message.reply_video(temp_filename) 40 | os.remove(file) 41 | os.remove(temp_filename) 42 | 43 | app.run() 44 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | # BOT Credentials 2 | API_ID = 123456 3 | API_HASH = "317v2nq9b..." 4 | API_TOKEN = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11" 5 | 6 | # Audio compression settings 7 | AUDIO_BITRATE = "32k" 8 | AUDIO_FORMAT = "mp3" 9 | AUDIO_CHANNELS = 1 10 | AUDIO_SAMPLE_RATE = 44100 11 | 12 | # Video compression settings 13 | VIDEO_SCALE = "min(1920,iw):min(1080,ih)" 14 | VIDEO_FPS = 24 15 | VIDEO_CODEC = "libx265" 16 | VIDEO_BITRATE = "100k" 17 | VIDEO_CRF = 30 18 | VIDEO_PRESET = "ultrafast" 19 | VIDEO_PIXEL_FORMAT = "yuv420p" 20 | VIDEO_PROFILE = "main" 21 | VIDEO_AUDIO_CODEC = "aac" 22 | VIDEO_AUDIO_BITRATE = "64k" 23 | VIDEO_AUDIO_CHANNELS = 1 24 | VIDEO_AUDIO_SAMPLE_RATE = 44100 25 | VIDEO_SCALE = '640:360' 26 | 27 | # Temporary file settings 28 | TEMP_FILE_SUFFIX_AUDIO = ".mp3" 29 | TEMP_FILE_SUFFIX_VIDEO = ".mp4" 30 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pyrogram 2 | pydub 3 | --------------------------------------------------------------------------------