-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutubeDl.py
58 lines (48 loc) · 1.84 KB
/
youtubeDl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests, yt_dlp, dotenv
import os, discord
class NotFoundError(Exception):
pass
DIR = 'downloads/'
if not os.path.exists('downloads'): os.mkdir('downloads')
def findSong(keyword: str):
ydl_opts = {
'format': 'bestaudio/best',
'noplaylist': True,
'quiet': True,
'default_search': 'ytsearch1',
'quiet': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
data = ydl.extract_info(keyword, download=False)
videoTitle = data['entries'][0]['title']
return videoTitle
def downloadAudio(keyword: str, guild_id: discord.interactions.Interaction.guild_id) -> tuple[str, str]: #remember to add ffmpeg to the system enviroment variables
filename = f'{hex(int.from_bytes(keyword.encode())%(1<<42)).replace("0x", "")}'
ydl_opts = {
'format': 'bestaudio/best',
'noplaylist': True,
'quiet': True,
'default_search': 'ytsearch1',
'outtmpl': os.path.join(f'{DIR}{filename}-{guild_id}'),
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'quiet': False,
}
errExtension = ''
# Download the audio
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
data = ydl.extract_info(keyword, download=True)
print(data['entries'][0]['original_url'])
except:
ydl_opts['outtmpl'] = f'{DIR}{filename}err-{guild_id}'
errExtension = 'err'
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
data = ydl.extract_info(keyword, download=True)
return data['entries'][0]['original_url'], f'{DIR}{filename}{errExtension}-{guild_id}.mp3'
def clean(file: str):
if os.path.exists(file):
os.remove(file)