bdill

wav_to_flac.py

Sep 30th, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.87 KB | Music | 0 0
  1. !pip install pydub
  2. !pip install tqdm
  3. !pip install mutagen
  4.  
  5. from pydub import AudioSegment
  6. from pathlib import Path
  7. from tqdm import tqdm
  8. from mutagen.flac import FLAC
  9.  
  10. # Base folder containing WAV files
  11. base_folder = Path(r"K:\Audio\_WAV EAC")
  12. # Output folder for FLAC files
  13. output_base = Path(r"K:\Audio\_FLAC")
  14. output_base.mkdir(exist_ok=True)
  15.  
  16. # Loop through all folders under base folder
  17. for album_folder in base_folder.iterdir():
  18.     if not album_folder.is_dir():
  19.         continue
  20.  
  21.     try:
  22.         artist, album = album_folder.name.split(" - ", 1)
  23.     except ValueError:
  24.         print(f"Skipping folder {album_folder}: folder name format not 'Artist - Album'")
  25.         continue
  26.  
  27.     # Find all WAV files in this folder
  28.     wav_files = list(album_folder.glob("*.wav"))
  29.     if not wav_files:
  30.         continue
  31.  
  32.     for wav_file in tqdm(wav_files, desc=f"Processing {album_folder.name}"):
  33.         file_stem = wav_file.stem  # e.g., "03_Romeo and Juliet"
  34.         if len(file_stem) < 3 or "_" not in file_stem:
  35.             print(f"Skipping {wav_file}: filename format not '<track>_<title>'")
  36.             continue
  37.  
  38.         track_num = file_stem[:2]
  39.         title = file_stem[3:]
  40.  
  41.         # Build output path
  42.         output_folder = output_base / f"{artist} - {album}"
  43.         output_folder.mkdir(parents=True, exist_ok=True)
  44.         output_path = output_folder / f"{track_num} {title}.flac"
  45.  
  46.         # Convert WAV to FLAC
  47.         audio = AudioSegment.from_wav(str(wav_file))
  48.         audio.export(str(output_path), format="flac")
  49.  
  50.         # Add metadata tags
  51.         flac_file = FLAC(str(output_path))
  52.         flac_file["artist"] = artist
  53.         flac_file["album"] = album
  54.         flac_file["tracknumber"] = track_num
  55.         flac_file["title"] = title
  56.         flac_file.save()
  57.  
  58.     print(f"Completed: {album_folder.name}")
  59.  
  60. print("All conversions complete!")
  61.  
Tags: convert FLAC wav
Advertisement
Add Comment
Please, Sign In to add comment