You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量替换同名不同扩展名音频文件并保留原目录结构的方法

Solution to Map MP3s to Original WAV Directory Structure

Hey there, dealing with 20k files manually is a total nightmare—let's fix this with Python since you have some experience with it. Here's a straightforward, safe approach to get those MP3s into their correct directories matching your original WAV structure:

Python Implementation

This script will walk through your entire WAV directory tree, find the corresponding MP3 in your central MP3 folder, and copy it to the same directory as the original WAV file.

import os
import shutil

# Update these paths to match your actual directories
WAV_SOURCE_DIR = "/path/to/your/wav/folder"
MP3_CENTRAL_DIR = "/path/to/your/mp3/folder"

# Optional: Handle case-insensitive filename matches (uncomment if needed)
# mp3_filename_map = {
#     os.path.splitext(f)[0].lower(): f 
#     for f in os.listdir(MP3_CENTRAL_DIR) 
#     if f.lower().endswith(".mp3")
# }

# Traverse every directory in the WAV structure
for current_dir, _, files in os.walk(WAV_SOURCE_DIR):
    for wav_file in files:
        # Skip non-WAV files
        if not wav_file.lower().endswith(".wav"):
            continue
            
        # Get the base name without extension to match the MP3
        file_base = os.path.splitext(wav_file)[0]
        target_mp3_name = f"{file_base}.mp3"
        
        # Path to the MP3 in your central folder
        source_mp3_path = os.path.join(MP3_CENTRAL_DIR, target_mp3_name)
        # Uncomment below and comment above if using case-insensitive map
        # source_mp3_path = os.path.join(MP3_CENTRAL_DIR, mp3_filename_map.get(file_base.lower()))
        
        # Path where the MP3 should go (same dir as the WAV)
        target_mp3_path = os.path.join(current_dir, target_mp3_name)
        
        # Only copy if the source MP3 exists
        if os.path.exists(source_mp3_path):
            shutil.copy2(source_mp3_path, target_mp3_path)
            print(f"Successfully copied: {source_mp3_path} → {target_mp3_path}")
        else:
            print(f"Warning: No matching MP3 found for {wav_file} (looked for {source_mp3_path})")

Key Details:

  • shutil.copy2 preserves file metadata (like creation/modification times) which is nicer than a basic copy.
  • The optional case-insensitive map handles situations where your MP3 filenames might have different capitalization than the WAVs (e.g., Sound1.mp3 vs sound1.wav).
  • The script prints progress and warnings so you can track issues (like missing MP3s) without digging through logs.

Pre-Run Checklist

  1. Backup first: Make copies of both your WAV and MP3 directories before running the script—better safe than sorry!
  2. Test small: Run the script on a subset of your directories first to confirm it works as expected.
  3. Update paths: Double-check that WAV_SOURCE_DIR and MP3_CENTRAL_DIR point to your actual folders.

Alternative: Command-Line (for Linux/macOS)

If you prefer a bash script instead, here's a quick variant:

WAV_DIR="/path/to/wav"
MP3_DIR="/path/to/mp3"

find "$WAV_DIR" -name "*.wav" | while read wav_path; do
    mp3_name=$(basename "$wav_path" .wav).mp3
    cp "$MP3_DIR/$mp3_name" "$(dirname "$wav_path")/$mp3_name" 2>/dev/null || echo "Missing MP3 for $(basename "$wav_path")"
done

This does the same job but without Python—useful if you don't want to fire up a script.

内容的提问来源于stack exchange,提问作者Nicholas Fitzpatrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:41:49