Guest User

ConformMusic

a guest
Apr 15th, 2024
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. import os
  2. import re
  3. from pydub import AudioSegment
  4.  
  5. def convert_mp3_to_wav(mp3_file, output_folder):
  6.     # Load the MP3 file
  7.     mp3_sound = AudioSegment.from_mp3(mp3_file)
  8.    
  9.     # Create the output filename (replace .mp3 with .wav)
  10.     folder_name = os.path.basename(os.path.dirname(mp3_file))
  11.     wav_count = len([f for f in os.listdir(output_folder) if f.endswith('.wav')])
  12.     output_filename = f"{folder_name}{'{:02d}'.format(wav_count + 1)}.wav"
  13.    
  14.     # Export the audio to WAV format
  15.     mp3_sound.export(os.path.join(output_folder, output_filename), format="wav")
  16.    
  17.     # Move the original MP3 file to the backup folder
  18.     backup_folder = os.path.join(output_folder, "backup")
  19.     if not os.path.exists(backup_folder):
  20.         os.mkdir(backup_folder)
  21.     mp3_backup_filename = re.sub(r'[^\w\s.-]', '', os.path.basename(mp3_file))
  22.     os.rename(mp3_file, os.path.join(backup_folder, mp3_backup_filename))
  23.  
  24. def process_directory(directory):
  25.     # Iterate through all files in the specified directory
  26.     for filename in os.listdir(directory):
  27.         filepath = os.path.join(directory, filename)
  28.         if os.path.isfile(filepath) and filename.lower().endswith('.mp3'):
  29.             convert_mp3_to_wav(filepath, directory)
  30.             print(f"Converted {filename} to WAV format and backed up the MP3 file.")
  31.  
  32. def main():
  33.     current_directory = os.getcwd()
  34.    
  35.     # Process the current directory
  36.     process_directory(current_directory)
  37.    
  38.     # Process immediate subdirectories
  39.     for folder in os.listdir(current_directory):
  40.         folder_path = os.path.join(current_directory, folder)
  41.         if os.path.isdir(folder_path):
  42.             process_directory(folder_path)
  43.  
  44. if __name__ == "__main__":
  45.     main()
  46.  
Advertisement
Add Comment
Please, Sign In to add comment