-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytdownloader.py
75 lines (66 loc) · 2.03 KB
/
ytdownloader.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
from mutagen.easyid3 import EasyID3
import youtube_dl
import os
import subprocess
import time
import threading
class YTDownloader:
"""
Class for downloading videos from YouTube.
"""
def __init__(self):
"""
Initialize the class with the URL of the video and the path to save the video.
"""
self.queue = []
self.thread = threading.Thread(target=self.run)
self.thread.start()
def add_to_queue(self, data):
"""
Add a video to the queue.
"""
self.queue.append(data)
def get_queue(self):
"""
Return the queue.
"""
return self.queue
def run(self):
"""
Run the downloader.
"""
while True:
if len(self.queue) > 0:
self.download()
else:
pass
# sleep for a second
time.sleep(1)
def download(self):
data = self.queue.pop(0)
"""
Download the video.
"""
path = f"/intermediate/{data['title']} - {data['artists'][0]['name']}.mp4"
path2 = f"/out/{data['title']} - {data['artists'][0]['name']}.mp3"
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': path,
'postprocessors': [],
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([f"https://youtu.be/{data['videoId']}"])
# convert to mp3
subprocess.Popen(["ffmpeg", "-i", path, "-codec:a", "libmp3lame", "-qscale:a", "2", path2])
# Add Metadata
audio = EasyID3(path2)
audio['title'] = data['title']
audio['artist'] = data['artists'][0]["name"]
audio['album'] = data['album']["name"]
audio.pprint()
audio.save()
os.remove(path)
except Exception as e:
# If it fails, add the video back to the queue
self.queue.append(data)