c47089aa0d
- Add support for WAV files with automatic conversion to MP3 - Save converted MP3 files in the same directory as WAV files - Reuse existing MP3 files if already converted - Update documentation and requirements
54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
# Helper functions for mknotes
|
|
|
|
import os
|
|
import tempfile
|
|
from pydub import AudioSegment
|
|
|
|
def find_audio_files(directory, extensions=(".mp3", ".m4a", ".wav")):
|
|
"""
|
|
Recursively find all audio files in the given directory with the specified extensions.
|
|
Returns a list of file paths.
|
|
"""
|
|
audio_files = []
|
|
for root, _, files in os.walk(directory):
|
|
for file in files:
|
|
if file.lower().endswith(extensions):
|
|
audio_files.append(os.path.join(root, file))
|
|
return audio_files
|
|
|
|
def ensure_directory_exists(directory):
|
|
"""
|
|
Create the directory if it does not exist.
|
|
"""
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
def convert_wav_to_mp3(wav_path):
|
|
"""
|
|
Convert a WAV file to MP3 format using pydub if an MP3 version doesn't already exist.
|
|
Saves the MP3 file in the same directory as the WAV file.
|
|
|
|
Args:
|
|
wav_path: Path to the WAV file
|
|
|
|
Returns:
|
|
Path to the MP3 file
|
|
"""
|
|
# Determine the output MP3 path (same directory, same name, different extension)
|
|
base_name = os.path.splitext(wav_path)[0]
|
|
mp3_path = base_name + ".mp3"
|
|
|
|
# Check if MP3 version already exists
|
|
if os.path.exists(mp3_path):
|
|
print(f"Using existing MP3 file: {mp3_path}")
|
|
return mp3_path
|
|
|
|
# Convert WAV to MP3
|
|
try:
|
|
audio = AudioSegment.from_wav(wav_path)
|
|
audio.export(mp3_path, format="mp3")
|
|
print(f"Converted {wav_path} to MP3 format at {mp3_path}")
|
|
return mp3_path
|
|
except Exception as e:
|
|
print(f"Error converting {wav_path} to MP3: {e}")
|
|
return None
|