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 math
import os import os
import time import time
import wget
from random import randint from random import randint
from urllib.parse import urlparse from urllib.parse import urlparse
@ -11,6 +12,7 @@ import aiofiles
import aiohttp import aiohttp
import requests import requests
import youtube_dl import youtube_dl
from yt_dlp import YoutubeDL
from pyrogram import Client, filters from pyrogram import Client, filters
from pyrogram.errors import FloodWait, MessageNotModified from pyrogram.errors import FloodWait, MessageNotModified
from pyrogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup 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) @Client.on_message(command(["vsong", f"vsong@{Veez.BOT_USERNAME}"]) & filters.group & ~filters.edited)
async def vsong(_, message: Message): async def vsong(client, message):
query = '' ydl_opts = {
for i in message.command[1:]: 'format':'best',
query += ' ' + str(i) 'keepvideo':True,
print(query) 'prefer_ffmpeg':False,
k = await message.reply_text("🔎 **searching video...**") 'geo_bypass':True,
ydl_ops = { 'outtmpl':'%(title)s.%(ext)s',
"format": "best[ext=mp4]", 'quite':True
"geo-bypass": True, }
"nocheckcertificate": True, query = message.command[1]
"outtmpl": "downloads/%(id)s.%(ext)s",
}
try: 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() results = YoutubeSearch(query, max_results=1).to_dict()
count += 1
try:
link = f"https://youtube.com{results[0]['url_suffix']}" link = f"https://youtube.com{results[0]['url_suffix']}"
# print(results) title = results[0]["title"][:40]
title = results[0]["title"]
thumbnail = results[0]["thumbnails"][0] thumbnail = results[0]["thumbnails"][0]
duration = int(float(results[0]["duration"])) thumb_name = f"thumb{title}.jpg"
views = results[0]["views"]
thumb_name = f'thumb{message.message_id}.jpg'
thumb = requests.get(thumbnail, allow_redirects=True) thumb = requests.get(thumbnail, allow_redirects=True)
open(thumb_name, 'wb').write(thumb.content) 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: except Exception as e:
print(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: try:
with youtube_dl.YoutubeDL(ydl_ops) as ydl: msg = await message.reply("📥 **downloading video...**")
info_dict = ydl.extract_info(link, download=False) with YoutubeDL(ydl_opts) as ytdl:
video_file = ydl.prepare_filename(info_dict) ytdl_data = ytdl.extract_info(link, download=True)
ydl.process_info(info_dict) file_name = ytdl.prepare_filename(ytdl_data)
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: except Exception as e:
await k.edit(f'❌ **something went wrong !** \n`{e}`') return await msg.edit(f"🚫 **error:** {str(e)}")
pass 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: try:
os.remove(video_file) os.remove(file_name)
os.remove(thumb_name) await msg.delete()
except Exception as e: except Exception as e:
print(e) print(e)
pass