#!/bin/bash # Determine the directory of the current script SCRIPT_DIR="$(dirname "$(realpath "$0")")" # Determine the project root directory PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Load configuration and logger from the core directory source "${PROJECT_ROOT}/core/config.sh" source "${PROJECT_ROOT}/core/logger.sh" log "DEBUG" "Starting script: $(basename "$0")" usage() { log "INFO" "Usage: $0 [-d|--delete] [-r|--recursive ] [-q|--quality ] [-v|--verbose] ..." log "INFO" "Options:" log "INFO" " -d, --delete Delete the original zip file after conversion" log "INFO" " -r, --recursive Recursively find and convert zip files in the specified path" log "INFO" " -q, --quality Set OGG encoding quality (0=lowest, 10=highest, default: 3)" log "INFO" " -v, --verbose Print extra output" exit 1 } if [ "$#" -lt 1 ]; then usage fi DELETE_ORIGINAL=false RECURSIVE=false RECURSIVE_PATH="" OGG_QUALITY="" VERBOSE=false while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in -d | --delete ) DELETE_ORIGINAL=true log "DEBUG" "DELETE_ORIGINAL set to true" ;; -r | --recursive ) RECURSIVE=true shift RECURSIVE_PATH=$1 log "DEBUG" "RECURSIVE set to true with path: $RECURSIVE_PATH" ;; -q | --quality ) shift OGG_QUALITY=$1 log "DEBUG" "OGG_QUALITY set to $OGG_QUALITY" ;; -v | --verbose ) VERBOSE=true log "DEBUG" "VERBOSE set to true" ;; * ) usage ;; esac shift done if [ "$RECURSIVE" = false ] && [ "$#" -lt 1 ]; then usage fi TEMP_DIR=$(mktemp -d "${EMUBASEDIR}/temp/oggcue-XXXXXX") log "DEBUG" "Created temporary directory: $TEMP_DIR" # Initialize counters processed=0 converted=0 deleted=0 errors=0 cleanup() { log "DEBUG" "Cleaning up temporary directory: $TEMP_DIR" rm -rf "$TEMP_DIR" } trap cleanup EXIT convert_audio_to_ogg() { local infile="$1" local oggfile="$2" if [ "$VERBOSE" = true ]; then echo "Converting audio: $infile -> $oggfile" fi # For CD audio bin files, we need to specify the raw audio format # CD audio is typically 16-bit stereo PCM at 44.1kHz local is_bin_file=false if [[ "$infile" == *.bin ]]; then is_bin_file=true fi if command -v oggenc >/dev/null 2>&1 && [ "$is_bin_file" = false ]; then # oggenc can't handle raw CD audio, use for WAV files only if [ -n "$OGG_QUALITY" ]; then oggenc -Q -q "$OGG_QUALITY" -o "$oggfile" "$infile" else oggenc -Q -o "$oggfile" "$infile" fi # Check if conversion was successful if [ $? -ne 0 ]; then log "ERROR" "Failed to convert audio file with oggenc: $infile" return 1 fi # Verify output file was created and has content if [ ! -f "$oggfile" ] || [ ! -s "$oggfile" ]; then log "ERROR" "Output file missing or empty: $oggfile" return 1 fi elif command -v ffmpeg >/dev/null 2>&1; then if [ "$is_bin_file" = true ]; then # For raw CD audio bin files, specify the format explicitly if [ -n "$OGG_QUALITY" ]; then ffmpeg -loglevel error -y -f s16le -ar 44100 -ac 2 -i "$infile" -qscale:a "$OGG_QUALITY" "$oggfile" else ffmpeg -loglevel error -y -f s16le -ar 44100 -ac 2 -i "$infile" -q:a 3 "$oggfile" fi else # For regular audio files if [ -n "$OGG_QUALITY" ]; then ffmpeg -loglevel error -y -i "$infile" -qscale:a "$OGG_QUALITY" "$oggfile" else ffmpeg -loglevel error -y -i "$infile" "$oggfile" fi fi # Check if conversion was successful if [ $? -ne 0 ]; then log "ERROR" "Failed to convert audio file: $infile" return 1 fi # Verify output file was created and has content if [ ! -f "$oggfile" ] || [ ! -s "$oggfile" ]; then log "ERROR" "Output file missing or empty: $oggfile" return 1 fi else log "ERROR" "Neither oggenc nor ffmpeg found for audio conversion" return 1 fi } update_cue_to_ogg_multi() { local cuefile="$1" local outcue="$2" local dir=$(dirname "$cuefile") local -A audio_files local current_file="" local current_track_type="" # First pass: identify which files contain audio tracks while IFS= read -r line; do if [[ $line =~ ^FILE\ \"([^\"]+)\"\ ([A-Z0-9]+) ]]; then current_file="${BASH_REMATCH[1]}" elif [[ $line =~ ^[[:space:]]*TRACK\ [0-9]+\ AUDIO ]]; then if [[ -n "$current_file" ]]; then audio_files["$current_file"]=1 log "DEBUG" "Marked $current_file as containing audio track" fi fi done < "$cuefile" # Second pass: process the file and convert only audio tracks > "$outcue" # Clear output file while IFS= read -r line; do log "DEBUG" "Processing CUE line: $line" if [[ $line =~ ^FILE\ \"([^\"]+)\"\ ([A-Z0-9]+) ]]; then log "DEBUG" "Matched FILE line: ${line}" origfile="${BASH_REMATCH[1]}" ftype="${BASH_REMATCH[2]}" if [[ "${audio_files[$origfile]}" == "1" ]]; then ext="${origfile##*.}" base="${origfile%.*}" oggname="${base}.ogg" infile="$dir/$origfile" oggfile="$dir/$oggname" log "DEBUG" "Converting audio file: $infile -> $oggfile" if [ ! -f "$oggfile" ]; then if ! convert_audio_to_ogg "$infile" "$oggfile"; then log "ERROR" "Audio conversion failed for: $origfile" return 1 fi fi echo "FILE \"$oggname\" OGG" >> "$outcue" else log "DEBUG" "Non-audio FILE line, copying as-is." echo "$line" >> "$outcue" fi else log "DEBUG" "Non-FILE line, copying as-is." echo "$line" >> "$outcue" fi done < "$cuefile" } process_zip() { local zipfile=$1 local zipdir=$(dirname "$zipfile") ((processed++)) log "INFO" "Processing zip file: $zipfile" unzip -q "$zipfile" -d "$TEMP_DIR" if [ $? -ne 0 ]; then log "ERROR" "Failed to unzip $zipfile" ((errors++)) return 1 fi # Check if any CUE files exist in the extracted content local cue_files_found=0 for cuefile in "$TEMP_DIR"/*.cue; do [ -f "$cuefile" ] || continue cue_files_found=1 # Use the same name as the original cue file, but create temp output first original_cue_name="$(basename "$cuefile")" temp_outcue="$cuefile.temp.cue" final_outcue="$TEMP_DIR/$original_cue_name" update_cue_to_ogg_multi "$cuefile" "$temp_outcue" if [ $? -ne 0 ]; then log "ERROR" "Failed to update cue file $cuefile" ((errors++)) return 1 fi # Rename temp file to final name mv "$temp_outcue" "$final_outcue" # Move all ogg files and the updated cue file for oggfile in "$TEMP_DIR"/*.ogg; do [ -f "$oggfile" ] && mv "$oggfile" "$zipdir/" done mv "$final_outcue" "$zipdir/" done # Only increment converted if we actually found and processed CUE files if [ $cue_files_found -eq 1 ]; then ((converted++)) else log "WARN" "No CUE files found in $zipfile" fi if [ "$DELETE_ORIGINAL" = true ]; then log "DEBUG" "Deleting original zip file: $zipfile" if rm -f "$zipfile"; then ((deleted++)) else log "ERROR" "Failed to delete original zip file: $zipfile" fi fi } if [ "$RECURSIVE" = true ]; then log "INFO" "Recursively finding and converting zip files in: $RECURSIVE_PATH" find "$RECURSIVE_PATH" -type f -name "*.zip" | while read -r zipfile; do process_zip "$zipfile" || true # Continue on error done else for zipfile in "$@"; do if [ -f "$zipfile" ]; then process_zip "$zipfile" || true # Continue on error else log "ERROR" "File $zipfile does not exist" ((errors++)) fi done fi cleanup echo "" echo "════════════════════════════════════════════════════════════════" echo "📋 Summary:" echo " ZIP files processed: $processed" echo " Successfully converted: $converted" echo " Archives deleted: $deleted" echo " Errors: $errors" echo "════════════════════════════════════════════════════════════════" log "INFO" "Summary: $processed processed, $converted converted, $deleted deleted, $errors errors" if [ $errors -gt 0 ]; then log "WARN" "Conversion completed with $errors errors" exit 1 else log "INFO" "Conversion completed successfully" exit 0 fi