#!/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 ] [-c|--chd] [-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" " -c, --chd Convert resulting flac/cue to chd format" log "INFO" " -q, --quality Set FLAC encoding compression level (0=fastest, 8=best, default: 5)" log "INFO" " -v, --verbose Print extra output" exit 1 } if [ "$#" -lt 1 ]; then usage fi DELETE_ORIGINAL=false RECURSIVE=false RECURSIVE_PATH="" CONVERT_CHD=false FLAC_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" ;; -c | --chd ) CONVERT_CHD=true log "DEBUG" "CONVERT_CHD set to true" ;; -q | --quality ) shift FLAC_QUALITY=$1 log "DEBUG" "FLAC_QUALITY set to $FLAC_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/flaccue-XXXXXX") log "DEBUG" "Created temporary directory: $TEMP_DIR" cleanup() { log "DEBUG" "Cleaning up temporary directory: $TEMP_DIR" rm -rf "$TEMP_DIR" } trap cleanup EXIT convert_audio_to_flac() { local infile="$1" local flacfile="$2" if [ "$VERBOSE" = true ]; then echo "Converting audio: $infile -> $flacfile" 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 flac >/dev/null 2>&1 && [ "$is_bin_file" = false ]; then # flac can't handle raw CD audio, use for WAV files only if [ -n "$FLAC_QUALITY" ]; then flac -f -$FLAC_QUALITY -o "$flacfile" "$infile" else flac -f -o "$flacfile" "$infile" fi if [ $? -ne 0 ]; then log "ERROR" "Failed to convert audio file with flac: $infile" return 1 fi if [ ! -f "$flacfile" ] || [ ! -s "$flacfile" ]; then log "ERROR" "Output file missing or empty: $flacfile" return 1 fi elif command -v ffmpeg >/dev/null 2>&1; then if [ "$is_bin_file" = true ]; then if [ -n "$FLAC_QUALITY" ]; then ffmpeg -loglevel error -y -f s16le -ar 44100 -ac 2 -i "$infile" -compression_level "$FLAC_QUALITY" "$flacfile" else ffmpeg -loglevel error -y -f s16le -ar 44100 -ac 2 -i "$infile" "$flacfile" fi else if [ -n "$FLAC_QUALITY" ]; then ffmpeg -loglevel error -y -i "$infile" -compression_level "$FLAC_QUALITY" "$flacfile" else ffmpeg -loglevel error -y -i "$infile" "$flacfile" fi fi if [ $? -ne 0 ]; then log "ERROR" "Failed to convert audio file: $infile" return 1 fi if [ ! -f "$flacfile" ] || [ ! -s "$flacfile" ]; then log "ERROR" "Output file missing or empty: $flacfile" return 1 fi else log "ERROR" "Neither flac nor ffmpeg found for audio conversion" return 1 fi } update_cue_to_flac_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%.*}" flacname="${base}.flac" infile="$dir/$origfile" flacfile="$dir/$flacname" log "DEBUG" "Converting audio file: $infile -> $flacfile" if [ ! -f "$flacfile" ]; then if ! convert_audio_to_flac "$infile" "$flacfile"; then log "ERROR" "Audio conversion failed for: $origfile" return 1 fi fi echo "FILE \"$flacname\" WAVE" >> "$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") log "INFO" "Processing zip file: $zipfile" unzip -q "$zipfile" -d "$TEMP_DIR" if [ $? -ne 0 ]; then log "ERROR" "Failed to unzip $zipfile" exit 1 fi for cuefile in "$TEMP_DIR"/*.cue; do [ -f "$cuefile" ] || continue outcue="$cuefile.flac.cue" update_cue_to_flac_multi "$cuefile" "$outcue" if [ $? -ne 0 ]; then log "ERROR" "Failed to update cue file $cuefile" exit 1 fi if [ "$CONVERT_CHD" = true ]; then chd_name="$(basename "${cuefile%.cue}").chd" log "DEBUG" "Converting $outcue to CHD: $TEMP_DIR/$chd_name" chdman createcd -i "$outcue" -o "$TEMP_DIR/$chd_name" --force if [ $? -ne 0 ]; then log "ERROR" "Failed to convert $outcue to CHD" exit 1 fi log "DEBUG" "Moving $TEMP_DIR/$chd_name to $zipdir/" mv "$TEMP_DIR/$chd_name" "$zipdir/" else # Move all flac files and the new cue for flacfile in "$TEMP_DIR"/*.flac; do [ -f "$flacfile" ] && mv "$flacfile" "$zipdir/" done mv "$outcue" "$zipdir/" fi done if [ "$DELETE_ORIGINAL" = true ]; then log "DEBUG" "Deleting original zip file: $zipfile" rm -f "$zipfile" 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" done else for zipfile in "$@"; do if [ -f "$zipfile" ]; then process_zip "$zipfile" else log "ERROR" "File $zipfile does not exist" exit 1 fi done fi cleanup log "INFO" "Conversion completed successfully" exit 0