No diretório ´test-fix´ tem várias pastas e em cad...

Criado em: 30 de maio de 2025

Respondido usando O3 por Chat01

Pergunta

No diretório ´test-fix´ tem várias pastas e em cada pasta tem 2 arquivos: Lead Vocal.flac e Backing Vocals.flac

  • Preciso de um script python que mixe o arquivo Lead Vocal.flac no seu respectivo Backing Vocals.flac e salve o resultado como mixture.wav, após isso renomeie o arquivo Lead Vocal.flac para vocals.flac e o arquivo Backing Vocals.flac para other.flac e por fim converta o arquivo .flac .wav e exclua o arquivo flac

Pensamento

Clarifying the user request

The user wants a Python script for audio manipulation in a directory with subfolders. The task includes: mixing "Lead Vocal.flac" into "Backing Vocals.flac" to create "mixture.wav", renaming the .flac files to "vocals.flac" and "other.flac", and converting the format to .wav, before deleting the original .flac files.

It seems clear, but mixing the files requires a good grasp of audio libraries like pydub or ffmpeg. I’ll want to structure this carefully.

Finalizing the script details

The user wants a Python script. After we mix the files with pydub, we need to make sure the audio lengths match. If one track is shorter, it stops overlaying, so it’s best to match lengths, possibly by padding the shorter track. The script should run using pathlib, remove unnecessary .flac files, and ensure the final files are mixtures and renamed .wav versions. I'll write clear instructions too, so the user knows where to place and run the script without extra dependencies. Let's put this all into code!

Thought for 24 seconds

Resposta

Aqui vai um script completo em Python que faz exatamente o que você pediu — mixa os dois .flac de cada pasta, gera mixture.wav, renomeia os originais e depois converte-os para .wav, apagando os .flac.

python
#!/usr/bin/env python3 """ Coloque este arquivo (por exemplo, mix_vocals.py) na mesma pasta que o diretório 'test-fix' e execute: python mix_vocals.py Requisitos: pip install pydub • FFmpeg precisa estar instalado e no PATH """ from pathlib import Path from pydub import AudioSegment import os ROOT = Path("test-fix") # diretório-raiz contendo as subpastas def mix_tracks(lead_path: Path, backing_path: Path, out_mix: Path): """Sobrepõe Lead em Backing e exporta para WAV.""" lead = AudioSegment.from_file(lead_path, format="flac") backing = AudioSegment.from_file(backing_path, format="flac") # Garante que ambos têm mesma taxa de amostragem e canalagem if lead.frame_rate != backing.frame_rate: backing = backing.set_frame_rate(lead.frame_rate) if lead.channels != backing.channels: backing = backing.set_channels(lead.channels) # Usa a faixa mais longa como base para evitar corte prematuro base, overlay = (backing, lead) if len(backing) >= len(lead) else (lead, backing) mixed = base.overlay(overlay) mixed.export(out_mix, format="wav") print(f"✓ Criado {out_mix.relative_to(ROOT.parent)}") def convert_flac_to_wav(flac_path: Path, wav_path: Path): """Converte FLAC para WAV e remove o FLAC.""" audio = AudioSegment.from_file(flac_path, format="flac") audio.export(wav_path, format="wav") os.remove(flac_path) print(f" └─ {flac_path.name}{wav_path.name} (FLAC apagado)") def process_folder(folder: Path): lead = folder / "Lead Vocal.flac" back = folder / "Backing Vocals.flac" if not (lead.exists() and back.exists()): print(f"• Pulando {folder.name}: arquivos FLAC não encontrados") return # Passo 1 – mixagem mix_tracks(lead, back, folder / "mixture.wav") # Passo 2 – renomeia FLACs vocals_flac = folder / "vocals.flac" other_flac = folder / "other.flac" lead.rename(vocals_flac) back.rename(other_flac) print(f" └─ Renomeado -> {vocals_flac.name}, {other_flac.name}") # Passo 3 – conversão FLAC→WAV e remoção dos FLACs convert_flac_to_wav(vocals_flac, folder / "vocals.wav") convert_flac_to_wav(other_flac, folder / "other.wav") def main(): if not ROOT.exists(): print(f"Diretório {ROOT} não encontrado.") return for sub in sorted(p for p in ROOT.iterdir() if p.is_dir()): print(f"\n▶ Processando pasta: {sub.name}") process_folder(sub) if __name__ == "__main__": main()

O que o script faz, passo a passo

  1. Percorre cada subpasta de test-fix.
  2. Carrega Lead Vocal.flac e Backing Vocals.flac com pydub.
  3. Ajusta taxa de amostragem/canais se forem diferentes.
  4. Sobrepõe a faixa Lead por cima da Backing (a mais longa vira base) e exporta como mixture.wav.
  5. Renomeia os FLACs para vocals.flac e other.flac.
  6. Converte cada FLAC renomeado para WAV (vocals.wav e other.wav) e exclui os FLACs originais.

Depois de rodar, em cada subpasta ficarão somente:

text
mixture.wav vocals.wav other.wav

Dica: Se preferir usar diretamente o FFmpeg em vez de pydub, basta trocar a função mix_tracks por um subprocess.run() chamando algo como
ffmpeg -i "Backing Vocals.flac" -i "Lead Vocal.flac" -filter_complex "[0:a][1:a]amix=inputs=2:normalize=0" mixture.wav.

Qualquer dúvida ou ajuste, é só falar!

Compartilhar esta Q&A