working
This commit is contained in:
43
backend/convert-to-mp3.py
Normal file
43
backend/convert-to-mp3.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# convert_to_mp3.py
|
||||
import os
|
||||
|
||||
from pydub import AudioSegment
|
||||
|
||||
# Directory relative to this script
|
||||
MUSIC_DIR = os.path.join(os.path.dirname(__file__), "music")
|
||||
|
||||
# Supported input formats
|
||||
INPUT_FORMATS = [".flac", ".wav", ".m4a", ".ogg"]
|
||||
|
||||
|
||||
def convert_to_mp3(file_path):
|
||||
file_root, ext = os.path.splitext(file_path)
|
||||
ext = ext.lower()
|
||||
|
||||
if ext not in INPUT_FORMATS:
|
||||
return # skip unsupported formats
|
||||
|
||||
mp3_path = f"{file_root}.mp3"
|
||||
|
||||
print(f"Converting {file_path} → {mp3_path}")
|
||||
|
||||
# Load audio
|
||||
audio = AudioSegment.from_file(file_path)
|
||||
|
||||
# Export as MP3
|
||||
audio.export(mp3_path, format="mp3", bitrate="320k")
|
||||
|
||||
# Delete original file
|
||||
os.remove(file_path)
|
||||
print(f"Deleted original: {file_path}")
|
||||
|
||||
|
||||
def main():
|
||||
for root, _, files in os.walk(MUSIC_DIR):
|
||||
for file in files:
|
||||
full_path = os.path.join(root, file)
|
||||
convert_to_mp3(full_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user