import sys
import threading
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QFileDialog, QLabel, QLineEdit, QMessageBox
from pydub import AudioSegment
import os

class MP3SpeedUpApp(QMainWindow):
    def __init__(self):
        super().__init__()
        
        self.setWindowTitle('MP3 Speed Up')
        self.setGeometry(100, 100, 400, 250)

        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()

        self.input_label = QLabel('Input folder not selected', self)
        layout.addWidget(self.input_label)

        self.output_label = QLabel('Output folder not selected', self)
        layout.addWidget(self.output_label)

        self.select_input_button = QPushButton('Select Input Folder', self)
        self.select_input_button.clicked.connect(self.select_input_folder)
        layout.addWidget(self.select_input_button)

        self.select_output_button = QPushButton('Select Output Folder', self)
        self.select_output_button.clicked.connect(self.select_output_folder)
        layout.addWidget(self.select_output_button)

        self.speed_factor_input = QLineEdit(self)
        self.speed_factor_input.setPlaceholderText('Enter speed factor (e.g., 1.2)')
        layout.addWidget(self.speed_factor_input)

        self.process_button = QPushButton('Process MP3 Files', self)
        self.process_button.clicked.connect(self.start_processing)
        layout.addWidget(self.process_button)

        self.status_label = QLabel('', self)
        layout.addWidget(self.status_label)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.input_folder = None
        self.output_folder = None

    def select_input_folder(self):
        folder = QFileDialog.getExistingDirectory(self, 'Select Input Folder')
        if folder:
            self.input_folder = folder
            self.input_label.setText(f'Input folder: {folder}')

    def select_output_folder(self):
        folder = QFileDialog.getExistingDirectory(self, 'Select Output Folder')
        if folder:
            self.output_folder = folder
            self.output_label.setText(f'Output folder: {folder}')

    def start_processing(self):
        if not self.input_folder or not self.output_folder:
            self.show_message('Error', 'Please select both input and output folders.')
            return

        try:
            speed_factor = float(self.speed_factor_input.text())
        except ValueError:
            self.show_message('Error', 'Invalid speed factor. Please enter a numeric value.')
            return

        self.status_label.setText('Processing...')
        self.process_thread = threading.Thread(target=self.process_files, args=(speed_factor,))
        self.process_thread.start()

    def process_files(self, speed_factor):
        self.process_folder(self.input_folder, self.output_folder, speed_factor)
        self.status_label.setText('Processing completed.')

    # def speed_up_audio(self, input_path, output_path, speed_factor):
    #     audio = AudioSegment.from_mp3(input_path)
    #     new_audio = audio._spawn(audio.raw_data, overrides={"frame_rate": int(audio.frame_rate * speed_factor)})
    #     new_audio = new_audio.set_frame_rate(audio.frame_rate)
    #     new_audio.export(output_path, format="mp3")

    def speed_up_audio(self, input_path, output_path, speed_factor):
        audio = AudioSegment.from_mp3(input_path)
        new_audio = audio.speedup(playback_speed=speed_factor)
        new_audio.export(output_path, format="mp3")   

    def process_folder(self, input_folder, output_folder, speed_factor):
        os.makedirs(output_folder, exist_ok=True)
        for filename in os.listdir(input_folder):
            if filename.endswith(".mp3"):
                input_path = os.path.join(input_folder, filename)
                output_path = os.path.join(output_folder, filename)
                self.speed_up_audio(input_path, output_path, speed_factor)

    def show_message(self, title, message):
        QMessageBox.critical(self, title, message)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MP3SpeedUpApp()
    window.show()
    sys.exit(app.exec_())

speed play without chipmunk