-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktopYouTubeToMP3.py
More file actions
68 lines (56 loc) · 2.76 KB
/
desktopYouTubeToMP3.py
File metadata and controls
68 lines (56 loc) · 2.76 KB
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
import os
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox
from pytube import Playlist
from moviepy.editor import VideoFileClip
class YouTubeDownloader(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('YouTube Playlist Downloader')
layout = QVBoxLayout()
self.playlist_url_label = QLabel('Enter the URL of the YouTube playlist:')
self.playlist_url_input = QLineEdit()
self.save_directory_label = QLabel('Enter the directory where you want to save the downloaded files:')
self.save_directory_input = QLineEdit()
self.resolution_label = QLabel('Enter the desired resolution (e.g., 720p, 480p):')
self.resolution_input = QLineEdit()
self.download_button = QPushButton('Download Playlist')
layout.addWidget(self.playlist_url_label)
layout.addWidget(self.playlist_url_input)
layout.addWidget(self.save_directory_label)
layout.addWidget(self.save_directory_input)
layout.addWidget(self.resolution_label)
layout.addWidget(self.resolution_input)
layout.addWidget(self.download_button)
self.download_button.clicked.connect(self.download_playlist)
self.setLayout(layout)
def download_playlist(self):
playlist_url = self.playlist_url_input.text()
save_directory = self.save_directory_input.text()
resolution = self.resolution_input.text()
if not playlist_url or not save_directory:
QMessageBox.warning(self, 'Warning', 'Please enter both playlist URL and save directory.')
return
try:
playlist = Playlist(playlist_url)
if not os.path.exists(save_directory):
os.makedirs(save_directory)
for video in playlist.videos:
try:
stream = video.streams.filter(only_audio=True, progressive=True, resolution=resolution).first()
if not stream:
stream = video.streams.filter(only_audio=True, progressive=True).order_by('resolution').desc().first()
stream.download(output_path=save_directory, filename=f"{video.title}.mp3")
except Exception as e:
print(f"Error downloading {video.title}: {e}")
continue
QMessageBox.information(self, 'Information', 'Playlist downloaded successfully!')
except Exception as e:
QMessageBox.warning(self, 'Error', f'Error downloading playlist: {e}')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = YouTubeDownloader()
window.show()
sys.exit(app.exec_())