From 1db42e47a67417778422cc7ca14fbe57951ebc85 Mon Sep 17 00:00:00 2001 From: levina <82658782+levina-lab@users.noreply.github.com> Date: Tue, 14 Sep 2021 04:25:21 +0700 Subject: [PATCH] fixed video downloader --- bot/song.py | 103 +++++++++++++++++++++------------------------------- 1 file changed, 42 insertions(+), 61 deletions(-) diff --git a/bot/song.py b/bot/song.py index 322ca76..e724384 100644 --- a/bot/song.py +++ b/bot/song.py @@ -4,6 +4,7 @@ import asyncio import math import os import time +import wget from random import randint from urllib.parse import urlparse @@ -11,6 +12,7 @@ import aiofiles import aiohttp import requests import youtube_dl +from yt_dlp import YoutubeDL from pyrogram import Client, filters from pyrogram.errors import FloodWait, MessageNotModified from pyrogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup @@ -225,67 +227,46 @@ def time_to_seconds(times): @Client.on_message(command(["vsong", f"vsong@{Veez.BOT_USERNAME}"]) & filters.group & ~filters.edited) -async def vsong(_, message: Message): - query = '' - for i in message.command[1:]: - query += ' ' + str(i) - print(query) - k = await message.reply_text("๐Ÿ”Ž **searching video...**") - ydl_ops = { - "format": "best[ext=mp4]", - "geo-bypass": True, - "nocheckcertificate": True, - "outtmpl": "downloads/%(id)s.%(ext)s", - } +async def vsong(client, message): + ydl_opts = { + 'format':'best', + 'keepvideo':True, + 'prefer_ffmpeg':False, + 'geo_bypass':True, + 'outtmpl':'%(title)s.%(ext)s', + 'quite':True +} +query = message.command[1] try: - results = [] - count = 0 - while len(results) == 0 and count < 6: - if count > 0: - await asyncio.sleep(1) - results = YoutubeSearch(query, max_results=1).to_dict() - count += 1 - try: - link = f"https://youtube.com{results[0]['url_suffix']}" - # print(results) - title = results[0]["title"] - thumbnail = results[0]["thumbnails"][0] - duration = int(float(results[0]["duration"])) - views = results[0]["views"] - thumb_name = f'thumb{message.message_id}.jpg' - thumb = requests.get(thumbnail, allow_redirects=True) - open(thumb_name, 'wb').write(thumb.content) - except Exception as e: - print(e) - await k.edit( - 'โŒ **video not found, please give a valid video name.\n\nยป if you think this is an error report to ' - '@VeezSupportGroup**') - return - except Exception as e: - await k.edit( - "๐Ÿ’ก **please give a video name too you want to download.**\n\nยป for example: `/vsong runaway`" - ) - print(str(e)) - return - await k.edit("๐Ÿ“ฅ **downloading file...**") - try: - with youtube_dl.YoutubeDL(ydl_ops) as ydl: - info_dict = ydl.extract_info(link, download=False) - video_file = ydl.prepare_filename(info_dict) - ydl.process_info(info_dict) - caption = f"๐Ÿท Name: {title}\n๐Ÿ’ก Views: `{views}`\n๐ŸŽง Request by: {message.from_user.mention()}\n\nโšก " \ - f"__Powered by Veez Music A.I__ " - buttons = InlineKeyboardMarkup([[InlineKeyboardButton("๐Ÿ—‘ Close", callback_data="cls")]]) - await k.edit("๐Ÿ“ค **uploading file...**") - await message.reply_video(video_file, caption=caption, duration=duration, thumb=thumb_name, - reply_markup=buttons, supports_streaming=True) - await k.delete() - except Exception as e: - await k.edit(f'โŒ **something went wrong !** \n`{e}`') - pass - try: - os.remove(video_file) - os.remove(thumb_name) + results = YoutubeSearch(query, max_results=1).to_dict() + link = f"https://youtube.com{results[0]['url_suffix']}" + title = results[0]["title"][:40] + thumbnail = results[0]["thumbnails"][0] + thumb_name = f"thumb{title}.jpg" + thumb = requests.get(thumbnail, allow_redirects=True) + open(thumb_name, "wb").write(thumb.content) + duration = results[0]["duration"] + results[0]["url_suffix"] + results[0]["views"] + rby = message.from_user.mention + except Exception as e: + print(e) + try: + msg = await message.reply("๐Ÿ“ฅ **downloading video...**") + with YoutubeDL(ydl_opts) as ytdl: + ytdl_data = ytdl.extract_info(link, download=True) + file_name = ytdl.prepare_filename(ytdl_data) + except Exception as e: + return await msg.edit(f"๐Ÿšซ **error:** {str(e)}") + preview = wget.download(thumbnail) + await msg.edit("๐Ÿ“ค **uploading video...**") + await message.reply_video( + file_name, + duration=int(ytdl_data["duration"]), + thumb=preview, + caption=ytdl_data['title']) + try: + os.remove(file_name) + await msg.delete() except Exception as e: print(e) - pass