fixed video downloader

This commit is contained in:
levina 2021-09-14 04:25:21 +07:00 committed by GitHub
parent 8e20300b4a
commit 1db42e47a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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