Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- from pydub import AudioSegment
- def convert_mp3_to_wav(mp3_file, output_folder):
- # Load the MP3 file
- mp3_sound = AudioSegment.from_mp3(mp3_file)
- # Create the output filename (replace .mp3 with .wav)
- folder_name = os.path.basename(os.path.dirname(mp3_file))
- wav_count = len([f for f in os.listdir(output_folder) if f.endswith('.wav')])
- output_filename = f"{folder_name}{'{:02d}'.format(wav_count + 1)}.wav"
- # Export the audio to WAV format
- mp3_sound.export(os.path.join(output_folder, output_filename), format="wav")
- # Move the original MP3 file to the backup folder
- backup_folder = os.path.join(output_folder, "backup")
- if not os.path.exists(backup_folder):
- os.mkdir(backup_folder)
- mp3_backup_filename = re.sub(r'[^\w\s.-]', '', os.path.basename(mp3_file))
- os.rename(mp3_file, os.path.join(backup_folder, mp3_backup_filename))
- def process_directory(directory):
- # Iterate through all files in the specified directory
- for filename in os.listdir(directory):
- filepath = os.path.join(directory, filename)
- if os.path.isfile(filepath) and filename.lower().endswith('.mp3'):
- convert_mp3_to_wav(filepath, directory)
- print(f"Converted {filename} to WAV format and backed up the MP3 file.")
- def main():
- current_directory = os.getcwd()
- # Process the current directory
- process_directory(current_directory)
- # Process immediate subdirectories
- for folder in os.listdir(current_directory):
- folder_path = os.path.join(current_directory, folder)
- if os.path.isdir(folder_path):
- process_directory(folder_path)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment