Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- !pip install pydub
- !pip install tqdm
- !pip install mutagen
- from pydub import AudioSegment
- from pathlib import Path
- from tqdm import tqdm
- from mutagen.flac import FLAC
- # Base folder containing WAV files
- base_folder = Path(r"K:\Audio\_WAV EAC")
- # Output folder for FLAC files
- output_base = Path(r"K:\Audio\_FLAC")
- output_base.mkdir(exist_ok=True)
- # Loop through all folders under base folder
- for album_folder in base_folder.iterdir():
- if not album_folder.is_dir():
- continue
- try:
- artist, album = album_folder.name.split(" - ", 1)
- except ValueError:
- print(f"Skipping folder {album_folder}: folder name format not 'Artist - Album'")
- continue
- # Find all WAV files in this folder
- wav_files = list(album_folder.glob("*.wav"))
- if not wav_files:
- continue
- for wav_file in tqdm(wav_files, desc=f"Processing {album_folder.name}"):
- file_stem = wav_file.stem # e.g., "03_Romeo and Juliet"
- if len(file_stem) < 3 or "_" not in file_stem:
- print(f"Skipping {wav_file}: filename format not '<track>_<title>'")
- continue
- track_num = file_stem[:2]
- title = file_stem[3:]
- # Build output path
- output_folder = output_base / f"{artist} - {album}"
- output_folder.mkdir(parents=True, exist_ok=True)
- output_path = output_folder / f"{track_num} {title}.flac"
- # Convert WAV to FLAC
- audio = AudioSegment.from_wav(str(wav_file))
- audio.export(str(output_path), format="flac")
- # Add metadata tags
- flac_file = FLAC(str(output_path))
- flac_file["artist"] = artist
- flac_file["album"] = album
- flac_file["tracknumber"] = track_num
- flac_file["title"] = title
- flac_file.save()
- print(f"Completed: {album_folder.name}")
- print("All conversions complete!")
Advertisement
Add Comment
Please, Sign In to add comment