입력받은 url을 다운로드 받고, MP3로 변환하고 싶으면 변환버튼.
다운 받은 파일 리스트 페이지 에서 파일 더블 클릭시 실행.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QLineEdit, QScrollArea, QMessageBox
import yt_dlp
import threading
from moviepy.editor import AudioFileClip
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("YouTube Downloader & MP4 to MP3 Converter")
self.setGeometry(300, 300, 600, 400)
self.main_layout = QVBoxLayout()
button_layout = QHBoxLayout()
self.youtube_button = QPushButton("Download YouTube Video", self)
self.youtube_button.clicked.connect(self.show_youtube_download_form)
button_layout.addWidget(self.youtube_button)
self.convert_button = QPushButton("Convert MP4 to MP3", self)
self.convert_button.clicked.connect(self.confirm_convert_form)
button_layout.addWidget(self.convert_button)
self.show_files_button = QPushButton("Show Downloaded Files", self)
self.show_files_button.clicked.connect(self.show_downloaded_files)
button_layout.addWidget(self.show_files_button)
self.main_layout.addLayout(button_layout)
self.url_input = QLineEdit(self)
self.url_input.setPlaceholderText("Enter YouTube URL here...")
self.url_input.setVisible(False)
self.main_layout.addWidget(self.url_input)
self.result_label = QLabel(self)
self.result_label.setWordWrap(True)
scroll_area = QScrollArea(self)
scroll_area.setWidgetResizable(True)
scroll_area.setWidget(self.result_label)
self.main_layout.addWidget(scroll_area)
self.setLayout(self.main_layout)
def show_youtube_download_form(self):
self.url_input.setVisible(True)
self.url_input.setPlaceholderText("Enter YouTube URL here...")
self.url_input.clear()
self.result_label.setText("Enter a YouTube URL to download:")
def download_video(self):
url = self.url_input.text().strip()
if url:
self.result_label.setText("Downloading... Please wait.")
threading.Thread(target=self.youtube_download_process, args=(url,)).start()
else:
self.result_label.setText("Please enter a valid YouTube URL.")
def youtube_download_process(self, url):
ydl_opts = {
'format': 'best',
'outtmpl': './downloads/%(title)s.%(ext)s',
'progress_hooks': [self.progress_hook]
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
def progress_hook(self, d):
if d['status'] == 'downloading':
percent = d['_percent_str']
speed = d.get('speed', 'Unknown')
eta = d.get('eta', 'Unknown')
self.result_label.setText(f"Downloading: {percent} - Speed: {speed} - ETA: {eta}s")
elif d['status'] == 'finished':
self.result_label.setText(f"Download complete!")
def confirm_convert_form(self):
reply = QMessageBox.question(self, 'Convert MP4 to MP3',
"Are you sure you want to convert all MP4 files in the folder to MP3?",
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
self.show_convert_form()
def show_convert_form(self):
self.url_input.setVisible(False)
self.result_label.setText("Converting all MP4 files in the folder to MP3...")
threading.Thread(target=self.convert_mp4_to_mp3).start()
def convert_mp4_to_mp3(self):
folder_path = './downloads'
for filename in os.listdir(folder_path):
if filename.endswith(".mp4"):
mp4_path = os.path.join(folder_path, filename)
mp3_path = os.path.join(folder_path, f"{os.path.splitext(filename)[0]}.mp3")
if os.path.exists(mp3_path):
self.result_label.setText(self.result_label.text() + f"\nSkipping {filename}: MP3 already exists.")
continue
audio_clip = AudioFileClip(mp4_path)
audio_clip.write_audiofile(mp3_path)
audio_clip.close()
self.result_label.setText(self.result_label.text() + f"\nConverted: {filename} to MP3.")
self.result_label.setText(self.result_label.text() + "\nAll MP4 files converted to MP3.")
def show_downloaded_files(self):
self.url_input.setVisible(False)
folder_path = './downloads'
if not os.path.exists(folder_path):
self.result_label.setText("No files found. The downloads folder does not exist.")
return
files = os.listdir(folder_path)
if files:
file_list = "\n".join(files)
self.result_label.setText(f"Files in {folder_path}:\n{file_list}")
else:
self.result_label.setText("No files found in the downloads folder.")
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())