این کد من from pyrogram import Client # pip install pyrogram from pyrogram import…
- User
این کد من
from pyrogram import Client # pip install pyrogram from pyrogram import filters # pip install tgcrypto from pyrogram.types import Message
from pytgcalls import filters as fl # pip install py-tgcalls -U from pytgcalls import idle from pytgcalls import PyTgCalls from pytgcalls.types import ChatUpdate from pytgcalls.types import GroupCallParticipant from pytgcalls.types import MediaStream from pytgcalls.types import Update
import os import re import subprocess import random import string
api_id = 13315602 api_hash = "21dc35844ae3c0e31faacc8f0ade17db"
proxy = { "scheme": "socks5", # "socks4", "socks5" and "http" are supported "hostname": "127.0.0.1", "port": 2080, "username": "username", "password": "password" }
phone_num = '+989115050944'
app = Client( "app_sesion", api_id=api_id, api_hash=api_hash, phone_number=phone_num, # proxy=proxy )
call_py = PyTgCalls(app)
def generate_random_string(length=10): # ترکیب حروف کوچک، بزرگ و اعداد characters = string.ascii_letters + string.digits # ایجاد یک رشته رندوم از ترکیب حروف و اعداد random_string = ''.join(random.choice(characters) for _ in range(length)) return random_string
async def progress_callback(current, total, msg, file_name): if total > 0: percent = (current / total) * 100 await msg.edit(f"Downloading {file_name}: {percent:.2f}% completed.")
def download_video(url: str) -> str: output_file = f'video_{generate_random_string()}.mp4' # نام فایل خروجی با زمان منحصر به فرد try: subprocess.run(['yt-dlp', '-o', output_file, url], check=True) return output_file except subprocess.CalledProcessError: return None
@app.on_message(filters.text) async def handle_link(client: Client, update: Message): url = update.text if 'youtube.com' in url or 'youtu.be' in url: await update.reply_text("Download Started. Please Waite...") video_file = download_video(url) # یا استفاده از yt-dlp if video_file and os.path.exists(video_file): await client.send_video(update.chat.id,video_file) os.remove(video_file) # حذف فایل بعد از ارسال else: await update.reply_text('خطا در دانلود ویدیو.') else: await update.reply_text('لطفاً یک لینک معتبر یوتیوب ارسال کنید.')
@app.on_message(filters.private & filters.document) async def play_handler(_, message: Message): if message.chat.id == 7618452400: file_name = message.document.file_name index_format = file_name.rfind('.') progress_msg = await message.reply("Download Started. Please Waite...") if re.search(r"mp3", file_name): await app.download_media(message, f"{file_name[:index_format]}.mp3", progress=progress_callback, progress_args=(progress_msg, file_name)) elif re.search(r"mp4|mkv", file_name): await app.download_media(message, f"{file_name[:index_format]}.mkv", progress=progress_callback, progress_args=(progress_msg, file_name))
@app.on_message(filters.private & filters.audio) async def play_handler(_, message: Message): if message.chat.id == 7618452400: file_name = message.audio.file_name index_format = file_name.rfind('.') progress_msg = await message.reply("Download Started. Please Waite...") await app.download_media(message, f"{file_name[:index_format]}.mp3", progress=progress_callback, progress_args=(progress_msg, file_name))
- Assistant
کدی که شما نوشتهاید، یک ربات تلگرامی با استفاده از کتابخانههای
pyrogramوpytgcallsاست. این ربات میتواند ویدیوها و فایلهای صوتی را از لینکهای یوتیوب دانلود کرده و آنها را در گروهها پخش کند.نگاهی به عملکرد کد
-
وارد کردن کتابخانهها:
pyrogramبرای ارتباط با API تلگرام.pytgcallsبرای تماسهای گروهی و پخش رسانه.
-
- User
اینم ارور من
(myenv) root@xuquo7ti:~# python3 main.py PyTgCalls v2.0.6 powered by NTgCalls v1.2.3, Copyright (C) 2021-2024 Laky-64 https://github.com/Laky-64 Licensed under the terms of the GNU Lesser General Public License v3 or later (LGPLv3+)
[youtube] Extracting URL: https://youtube.com/shorts/xaRq6WPpBlA?si=MJT1RcW7Kedu0bw3 [youtube] xaRq6WPpBlA: Downloading webpage [youtube] xaRq6WPpBlA: Downloading ios player API JSON [youtube] xaRq6WPpBlA: Downloading mweb player API JSON [youtube] xaRq6WPpBlA: Downloading m3u8 information [info] xaRq6WPpBlA: Downloading 1 format(s): 400+251 [download] Destination: video_wwDJbwjO4Q.mp4.f400.mp4 [download] 100% of 2.28MiB in 00:00:00 at 35.27MiB/s [download] Destination: video_wwDJbwjO4Q.mp4.f251.webm [download] 100% of 232.48KiB in 00:00:00 at 1.49MiB/s [Merger] Merging formats into "video_wwDJbwjO4Q.mp4.webm" Deleting original file video_wwDJbwjO4Q.mp4.f251.webm (pass -k to keep) Deleting original file video_wwDJbwjO4Q.mp4.f400.mp4 (pass -k to keep)
- Assistant
به نظر میرسد که اروری که شما با آن مواجه شدهاید، یک هشدار است و در واقع نشاندهندهی موفقیتآمیز بودن فرایند دانلود و ادغام ویدیوها با استفاده از
yt-dlpاست. قدرت این دستورات این است که آنها ویدیو را از یوتیوب دانلود کرده و در فرمتهای مختلفی ذخیره میکنند و سپس آنها را ادغام میکنند.توضیح ارور
در پیام خروجی شما، میتوانیم چند نکته را مشاهده کنیم:
-
دانلود موفق:
-
- User
(myenv) root@xuquo7ti:~# python3 main.py PyTgCalls v2.0.6 powered by NTgCalls v1.2.3, Copyright (C) 2021-2024 Laky-64 https://github.com/Laky-64 Licensed under the terms of the GNU Lesser General Public License v3 or later (LGPLv3+)
[youtube] Extracting URL: https://youtube.com/shorts/xaRq6WPpBlA?si=MJT1RcW7Kedu0bw3 [youtube] xaRq6WPpBlA: Downloading webpage [youtube] xaRq6WPpBlA: Downloading ios player API JSON [youtube] xaRq6WPpBlA: Downloading mweb player API JSON [youtube] xaRq6WPpBlA: Downloading m3u8 information [info] xaRq6WPpBlA: Downloading 1 format(s): 400+251 [download] Destination: video_RRpYZuoTFo.mp4.f400.mp4 [download] 100% of 2.28MiB in 00:00:00 at 11.11MiB/s [download] Destination: video_RRpYZuoTFo.mp4.f251.webm [download] 100% of 232.48KiB in 00:00:00 at 1.48MiB/s [Merger] Merging formats into "video_RRpYZuoTFo.mp4.webm" Deleting original file video_RRpYZuoTFo.mp4.f251.webm (pass -k to keep) Deleting original file video_RRpYZuoTFo.mp4.f400.mp4 (pass -k to keep)
- Assistant
پیام خروجی جدید شما نشان میدهد که
yt-dlpهمچنان ویدیو را با موفقیت دانلود کرده و در حال ادغام فرمتهای مختلف است. با این حال، به نظر میرسد که فایل نهایی به جای MP4 به صورت WEBM ایجاد میشود. بیایید نگاهی به این مشکل بیندازیم.علت
مشکل اصلی این است که به نظر میرسد
yt-dlpبهطور پیشفرض فایل نهایی را به فرمت WEBM تغییر میدهد. این میتواند به خاطر اولویتهای فرمت یا گزینههایی باشد که در دستورات شما مشخص کردهاید.اصلاحات پیشنهادی
برای اطمینان از اینکه فایل نهایی با فرمت MP4 ایجاد میشود، باید به گزینههای دستوری
yt-dlpتوجه کنید. اینجا چند کار میتوانید انجام دهید: - User
فایل پایتونی رو بعد بستن ssh به سرور لینوکسی قطع میکنه
- Assistant
وقتی شما به سرور لینوکسی از طریق SSH متصل میشوید و سپس جلسه SSH را قطع میکنید (به هر دلیلی)، هر فرایندی که در آن جلسه اجرا شده باشد نیز متوقف خواهد شد. برای جلوگیری از این مشکل، میتوانید یکی از روشهای زیر را استفاده کنید:
۱. استفاده از
nohupاگر میخواهید یک برنامه را بدون وابستگی به ورودی/خروجی ترمینال اجرا کنید، میتوانید از
nohupاستفاده کنید: