-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathYouTube.py
121 lines (101 loc) · 4.43 KB
/
YouTube.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from os import getcwd
from os.path import join, isfile
from shutil import which
from platform import system
from io import BytesIO
from contextlib import redirect_stdout
from requests import get
import re, subprocess, yt_dlp
class YouTube:
def __init__(self, update: bool = True):
# try to install/update yt-dlp on each run
if update:
try:
subprocess.check_call(['pip', 'install', '-U', 'yt-dlp'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print('yt-dlp updated successfully.')
except subprocess.CalledProcessError as e:
print(f'There was an error while updating yt-dlp: {e}')
self.options = {
'outtmpl': '-',
'logtostderr': True,
'quiet': True,
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}]
}
# Check if the platform is Windows
if system() == 'Windows':
ffmpeg_executable = join(getcwd(), '')
if isfile(ffmpeg_executable):
self.options['ffmpeg_location'] = join(getcwd(), '')
elif which('ffmpeg') is None:
raise SystemExit('FFmpeg is required but not found. Exiting.')
else:
# For macOS and Linux, check if ffmpeg is available globally
if which('ffmpeg') is None:
raise SystemExit('FFmpeg is required but not found. Exiting.')
self.ydl = yt_dlp.YoutubeDL(self.options)
self.filter = re.compile(r'^.*(youtu\.be/|v/|u/\w/|embed/|watch\?v=|\&v=)([^#\&\?]*).*')
def extract_url(self, url: str):
try:
match = self.filter.match(url)
vid_id = match.group(2) if match else None
if vid_id and len(vid_id) == 11:
return f'https://youtube.com/watch?v={vid_id}'
return False
except Exception as e:
print(f'Error extracting URL: {e}')
return False
def download(self, url: str):
try:
if 'maxresdefault' in url:
with BytesIO() as buffer:
r = get(url)
if r.status_code == 200:
buffer.write(r.content)
return buffer.getvalue()
else:
print(f'Failed to download thumbnail: {r.status_code}')
return None
else:
with BytesIO() as buffer:
with redirect_stdout(buffer), self.ydl as ugh:
ugh.download([url])
return buffer.getvalue()
except Exception as e:
print(f'Download error: {e}')
return None
def get_size(self, info):
# find the highest opus (audio only) entry and get the filesize
max_opus = None
max_filesize = 0
if 'formats' in info and info['formats']:
for format_entry in info['formats']:
if format_entry.get('acodec') == 'opus' and format_entry.get('filesize'):
filesize = format_entry['filesize']
if filesize > max_filesize:
max_filesize = filesize
max_opus = format_entry
return (max_opus.get('filesize') / (1024 ** 2)) if max_opus else False
def is_livestream(self, info):
return bool(info.get('is_live'))
def get_info(self, url: str):
try:
info = self.ydl.extract_info(url, download = False)
except Exception as e: # in case if the video does not exist
print(f'Failed to extract video info: {e}')
return False
# try to get the metadata song title / artist name
# if they are not available, use the video title / channel name
parsed_info = {
'title': info.get('track', info.get('title', None)),
'artist': info.get('artist', None),
'thumbnail': info.get('thumbnail', None),
'duration': info.get('duration', None),
'filesize': self.get_size(info),
'is_live': self.is_livestream(info)
}
return parsed_info