import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QComboBox, QRadioButton, QGroupBox
from PyQt6.QtCore import QThread, pyqtSignal
from pytube import YouTube

class Downloader(QThread):
    finished = pyqtSignal()

    def __init__(self, url, format, resolution, audio_format, audio_bitrate):
        super().__init__()
        self.url = url
        self.format = format
        self.resolution = resolution
        self.audio_format = audio_format
        self.audio_bitrate = audio_bitrate

    def run(self):
        yt = YouTube(self.url)
        if self.format == "mp4":
            stream = yt.streams.filter(file_extension='mp4', res=self.resolution).first()
        elif self.format == "mkv":
            stream = yt.streams.filter(file_extension='mkv', res=self.resolution).first()

        if stream is not None:
            stream.download()

        if self.audio_format == "mp3":
            audio_stream = yt.streams.filter(only_audio=True).first()
            if audio_stream is not None:
                audio_stream.download(filename_prefix="audio_", output_path="audio/")
        elif self.audio_format == "aac":
            audio_stream = yt.streams.filter(only_audio=True, file_extension='m4a').first()
            if audio_stream is not None:
                audio_stream.download(filename_prefix="audio_", output_path="audio/")
        
        self.finished.emit()

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()

        self.url_label = QLabel("URL:")
        layout.addWidget(self.url_label)

        self.url_input = QLineEdit()
        layout.addWidget(self.url_input)

        self.format_group = QGroupBox("Video Format")
        format_layout = QVBoxLayout()
        self.mp4_radio = QRadioButton("MP4")
        self.mkv_radio = QRadioButton("MKV")
        format_layout.addWidget(self.mp4_radio)
        format_layout.addWidget(self.mkv_radio)
        self.format_group.setLayout(format_layout)
        layout.addWidget(self.format_group)

        self.resolution_label = QLabel("Resolution:")
        layout.addWidget(self.resolution_label)

        self.resolution_combo = QComboBox()
        self.resolution_combo.addItem("360p")
        self.resolution_combo.addItem("480p")
        self.resolution_combo.addItem("720p")
        self.resolution_combo.addItem("1080p")
        layout.addWidget(self.resolution_combo)

        self.audio_group = QGroupBox("Audio Format")
        audio_layout = QVBoxLayout()
        self.mp3_radio = QRadioButton("MP3")
        self.aac_radio = QRadioButton("AAC")
        audio_layout.addWidget(self.mp3_radio)
        audio_layout.addWidget(self.aac_radio)
        self.audio_group.setLayout(audio_layout)
        layout.addWidget(self.audio_group)

        self.bitrate_label = QLabel("Audio Bitrate:")
        layout.addWidget(self.bitrate_label)

        self.bitrate_combo = QComboBox()
        self.bitrate_combo.addItem("128kbps")
        self.bitrate_combo.addItem("256kbps")
        self.bitrate_combo.addItem("320kbps")
        layout.addWidget(self.bitrate_combo)

        self.download_button = QPushButton("Download")
        self.download_button.clicked.connect(self.start_download)
        layout.addWidget(self.download_button)

        self.setLayout(layout)

    def start_download(self):
        url = self.url_input.text()
        format = "mp4" if self.mp4_radio.isChecked() else "mkv"
        resolution = self.resolution_combo.currentText()
        audio_format = "mp3" if self.mp3_radio.isChecked() else "aac"
        audio_bitrate = self.bitrate_combo.currentText()

        self.download_button.setEnabled(False)
        self.thread = Downloader(url, format, resolution, audio_format, audio_bitrate)
        self.thread.finished.connect(self.download_finished)
        self.thread.start()

    def download_finished(self):
        self.download_button.setEnabled(True)
        print('Download finished!')
        self.setWindowTitle('YouTube Downloader')

app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec())

이 코드를 비디오를 다운로드하면 정상적으로 오디오가 들리는 비디오를 다운로드받을수있게 해주고, 오디오 옵션만 선택한 경우엔 오디오 파일만 다운로드받을수있게 작동하도록 수정해줘