Skip to content

Commit

Permalink
Update ao app
Browse files Browse the repository at this point in the history
*Fila, agora vc pode enviar mais arquivos de uma vez
*Id agora substitui espaços por _
Um novo py e bat para converter mp3 para ogg
  • Loading branch information
cosmosgc committed Feb 10, 2025
1 parent 43d7cb9 commit d2c850e
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 53 deletions.
90 changes: 37 additions & 53 deletions Tools/Jukebox Manager/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
import subprocess

def load_yaml(file_path):
with open(file_path, 'r') as file:
return yaml.safe_load(file)
if os.path.exists(file_path):
with open(file_path, 'r') as file:
return yaml.safe_load(file) or []
return []

def save_yaml(file_path, data):
with open(file_path, 'w') as file:
yaml.safe_dump(data, file)

def select_ogg_file():
file_path = filedialog.askopenfilename(
title="Select an .ogg file",
def select_ogg_files():
file_paths = filedialog.askopenfilenames(
title="Select .ogg files",
filetypes=[("OGG files", "*.ogg")]
)
return file_path
return file_paths

def update_listbox():
listbox.delete(0, tk.END)
Expand All @@ -36,58 +38,42 @@ def update_attributions(file_name):
"source": "Unknown"
}

if os.path.exists(attributions_path):
attributions_data = load_yaml(attributions_path)
attributions_data.append(default_attribution)
else:
attributions_data = [default_attribution]

attributions_data = load_yaml(attributions_path)
attributions_data.append(default_attribution)
save_yaml(attributions_path, attributions_data)

def move_file_and_update_yaml():
ogg_file = select_ogg_file()
if not ogg_file:
def move_files_and_update_yaml():
ogg_files = select_ogg_files()
if not ogg_files:
return

try:
# Extract filename and construct destination path
file_name = os.path.basename(ogg_file)
dest_dir = os.path.abspath("../../Resources/Audio/Jukebox")
dest_path = os.path.join(dest_dir, file_name)

# Ensure destination directory exists
os.makedirs(dest_dir, exist_ok=True)

# copy the file
shutil.copy(ogg_file, dest_path)

# Get new item details
item_id = file_name.rsplit(".", 1)[0] # Filename without extension
item_name = file_name.replace("_", " ").rsplit(".", 1)[0]
new_item = {
"type": "jukebox",
"id": item_id,
"name": item_name,
"path": {
"path": f"/Audio/Jukebox/{file_name}"
for ogg_file in ogg_files:
try:
file_name = os.path.basename(ogg_file)
dest_dir = os.path.abspath("../../Resources/Audio/Jukebox")
dest_path = os.path.join(dest_dir, file_name)
os.makedirs(dest_dir, exist_ok=True)
shutil.copy(ogg_file, dest_path)

item_id = file_name.rsplit(".", 1)[0].replace(" ", "_")
item_name = file_name.replace("_", " ").rsplit(".", 1)[0]
new_item = {
"type": "jukebox",
"id": item_id,
"name": item_name,
"path": {"path": f"/Audio/Jukebox/{file_name}"}
}
}

# Load YAML, append new item, and save
yaml_data = load_yaml(yaml_file_path)
yaml_data.append(new_item)
save_yaml(yaml_file_path, yaml_data)

# Update attributions
update_attributions(file_name)

# Update listbox
update_listbox()
yaml_data = load_yaml(yaml_file_path)
yaml_data.append(new_item)
save_yaml(yaml_file_path, yaml_data)
update_attributions(file_name)

messagebox.showinfo("Success", f"File moved and YAML updated!\n\nNew ID: {item_id}\nPath: {dest_path}")
except Exception as e:
messagebox.showerror("Error", f"Error processing {file_name}: {str(e)}")

except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
update_listbox()
messagebox.showinfo("Success", "All files moved and YAML updated!")

def open_folder(path):
abs_path = os.path.abspath(path)
Expand All @@ -102,7 +88,6 @@ def open_folder(path):
root.geometry("400x500")

yaml_file_path = "../../Resources/Prototypes/Catalog/Jukebox/Standard.yml"

if not os.path.exists(yaml_file_path):
messagebox.showerror("Error", f"YAML file not found at {yaml_file_path}")
root.destroy()
Expand All @@ -119,10 +104,9 @@ def open_folder(path):
listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

scrollbar.config(command=listbox.yview)

update_listbox()

tk.Button(root, text="Add OGG File", command=move_file_and_update_yaml, font=("Arial", 12)).pack(pady=10)
tk.Button(root, text="Add OGG Files", command=move_files_and_update_yaml, font=("Arial", 12)).pack(pady=10)
tk.Button(root, text="Open Catalog Folder", command=lambda: open_folder("../../Resources/Prototypes/Catalog/Jukebox"), font=("Arial", 12)).pack(pady=5)
tk.Button(root, text="Open Audio Folder", command=lambda: open_folder("../../Resources/Audio/Jukebox"), font=("Arial", 12)).pack(pady=5)

Expand Down
12 changes: 12 additions & 0 deletions Tools/Jukebox Manager/mp3toOgg.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@echo off

:: Ensure pip is up to date
python -m pip install --upgrade pip

:: Install required dependencies
pip install -r requirements.txt

:: Run the script
call python mp3toOgg.py

pause
81 changes: 81 additions & 0 deletions Tools/Jukebox Manager/mp3toOgg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import os
import tkinter as tk
from tkinter import filedialog, messagebox, scrolledtext
from pydub import AudioSegment
from mutagen.mp3 import MP3
from mutagen.oggvorbis import OggVorbis
from mutagen.id3 import ID3

def select_mp3_files():
"""Open a file dialog to select multiple MP3 files."""
file_paths = filedialog.askopenfilenames(
title="Select MP3 Files",
filetypes=[("MP3 Files", "*.mp3")]
)
return file_paths

def copy_metadata(mp3_file, ogg_file):
"""Copy metadata from MP3 to OGG file."""
try:
mp3_metadata = MP3(mp3_file, ID3=ID3)
ogg_metadata = OggVorbis(ogg_file)

for tag in mp3_metadata:
if tag in ["TIT2", "TPE1", "TALB"]: # Title, Artist, Album
ogg_metadata[tag] = mp3_metadata[tag].text[0]

ogg_metadata.save()
except Exception as e:
print(f"Failed to copy metadata: {e}")

def convert_mp3_to_ogg(mp3_file, log_text):
"""Convert an MP3 file to OGG format and save it in the same directory."""
try:
audio = AudioSegment.from_mp3(mp3_file)
file_dir = os.path.dirname(mp3_file)
output_dir = os.path.join(file_dir, "ogg")
os.makedirs(output_dir, exist_ok=True)

file_name = os.path.splitext(os.path.basename(mp3_file))[0] + ".ogg"
output_path = os.path.join(output_dir, file_name)
audio.export(output_path, format="ogg")

copy_metadata(mp3_file, output_path)

log_text.insert(tk.END, f"Converted: {file_name} -> {output_dir}\n")
log_text.yview(tk.END)
except Exception as e:
log_text.insert(tk.END, f"Failed to convert {mp3_file}: {str(e)}\n")
log_text.yview(tk.END)

def batch_convert(log_text):
"""Handle file selection and conversion process with a queue."""
mp3_files = select_mp3_files()
if not mp3_files:
messagebox.showwarning("No Files Selected", "No MP3 files were selected.")
return

log_text.insert(tk.END, "Starting conversion...\n")
log_text.yview(tk.END)

for mp3_file in mp3_files:
convert_mp3_to_ogg(mp3_file, log_text)

log_text.insert(tk.END, "Conversion complete!\n")
log_text.yview(tk.END)

# UI Setup
root = tk.Tk()
root.title("MP3 to OGG Converter")
root.geometry("500x300")

tk.Label(root, text="MP3 to OGG Converter", font=("Arial", 14)).pack(pady=10)

tk.Button(root, text="Select and Convert MP3 Files", command=lambda: batch_convert(log_text), font=("Arial", 12)).pack(pady=10)

log_text = scrolledtext.ScrolledText(root, width=60, height=10, font=("Arial", 10))
log_text.pack(pady=10)

tk.Button(root, text="Exit", command=root.quit, font=("Arial", 12)).pack(pady=5)

root.mainloop()
2 changes: 2 additions & 0 deletions Tools/Jukebox Manager/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pyyaml
pydub
mutagen

0 comments on commit d2c850e

Please sign in to comment.