|
| 1 | +import yt_dlp |
| 2 | +import requests |
| 3 | +import re |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +import asyncio |
| 7 | + |
| 8 | +async def bash(cmd): |
| 9 | + cmd_ = cmd.split() |
| 10 | + process = await asyncio.create_subprocess_exec(*cmd_, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) |
| 11 | + stdout, stderr = await process.communicate() |
| 12 | + e = stderr.decode().strip() |
| 13 | + o = stdout.decode().strip() |
| 14 | + return o, e |
| 15 | + |
| 16 | +#Download videos from youtube------------------------------------------------------------------------------------------- |
| 17 | +async def download_from_youtube(url): |
| 18 | + await bash(f'yt-dlp -f best --no-warnings --prefer-ffmpeg {url}') |
| 19 | + o, e = await bash(f'yt-dlp -f best --get-filename {url}') |
| 20 | + with yt_dlp.YoutubeDL({'format': 'best'}) as ydl: |
| 21 | + info_dict = ydl.extract_info(url, download=False) |
| 22 | + video_title = info_dict.get('title', None) |
| 23 | + video_ext = info_dict.get('ext', None) |
| 24 | + file = f"{video_title}.{video_ext}" |
| 25 | + os.rename(f"{o}", file) |
| 26 | + return file |
| 27 | + |
| 28 | +#for ytdlp supported sites ------------------------------------------------------------------------------------------ |
| 29 | + |
| 30 | +async def ytdl(url): |
| 31 | + if 'HLS' in url: |
| 32 | + await bash(f'yt-dlp -f best --no-warnings --hls-prefer-ffmpeg {url}') |
| 33 | + elif 'm3u8' in url: |
| 34 | + await bash(f'yt-dlp -f best --no-warnings --hls-prefer-ffmpeg {url}') |
| 35 | + else: |
| 36 | + await bash(f'yt-dlp -f best --prefer-ffmpeg --no-warnings {url}') |
| 37 | + o, e = await bash(f'yt-dlp -f best --get-filename {url}') |
| 38 | + with yt_dlp.YoutubeDL({'format': 'best'}) as ydl: |
| 39 | + info_dict = ydl.extract_info(url, download=False) |
| 40 | + video_title = info_dict.get('title', None) |
| 41 | + video_ext = info_dict.get('ext', None) |
| 42 | + file = f"{video_title}.{video_ext}" |
| 43 | + os.rename(f"{o}", file) |
| 44 | + return file |
| 45 | + |
| 46 | +#weburl download------------------------------------------------------------------------------ |
| 47 | + |
| 48 | +#Does the url contain a downloadable resource |
| 49 | +def is_downloadable(url): |
| 50 | + h = requests.head(url, allow_redirects=True) |
| 51 | + header = h.headers |
| 52 | + content_type = header.get('content-type') |
| 53 | + if 'text' in content_type.lower(): |
| 54 | + return False |
| 55 | + if 'html' in content_type.lower(): |
| 56 | + return False |
| 57 | + return True |
| 58 | + |
| 59 | +#Get filename from content-disposition |
| 60 | +def get_filename_from_cd(cd): |
| 61 | + if not cd: |
| 62 | + return None |
| 63 | + fname = re.findall('filename=(.+)', cd) |
| 64 | + if len(fname) == 0: |
| 65 | + return None |
| 66 | + return fname[0] |
| 67 | + |
| 68 | +def weburl(url): |
| 69 | + x = is_downloadable(url) |
| 70 | + if x is False: |
| 71 | + return None |
| 72 | + elif x is True: |
| 73 | + pass |
| 74 | + else: |
| 75 | + return None |
| 76 | + r = requests.get(url, allow_redirects=True) |
| 77 | + filename = get_filename_from_cd(r.headers.get('content-disposition')) |
| 78 | + open(filename, 'wb').write(r.content) |
| 79 | + return filename |
0 commit comments